|
3 | 3 | GraphQL queries are composed of: |
4 | 4 |
|
5 | 5 | * Queries |
6 | | -* Fields |
7 | 6 | * Arguments |
| 7 | +* Fields |
8 | 8 |
|
9 | 9 | Each Class in TerminusDB automatically generates a top-level Query. Each property of the class automatically generates both arguments and fields. |
10 | 10 |
|
@@ -360,3 +360,101 @@ query { |
360 | 360 | } |
361 | 361 | } |
362 | 362 | ``` |
| 363 | + |
| 364 | +## Fields |
| 365 | + |
| 366 | +Each TerminusDB class has associated with it, some number of |
| 367 | +fields. These fields include each field that is defined in the class. |
| 368 | + |
| 369 | +For instance, given the TerminusDB class: |
| 370 | + |
| 371 | +```json |
| 372 | +{ "@type" : "Class", |
| 373 | + "@id" : "Person", |
| 374 | + "name" : "xsd:string", |
| 375 | + "dob" : "xsd:dateTime", |
| 376 | + "friend" : {"@type" : "Set", "@class" : "Person" }} |
| 377 | +``` |
| 378 | + |
| 379 | +We have a query field for each of `name`, `dob` and `friend`. However |
| 380 | +we also have the following specially defined fields: |
| 381 | + |
| 382 | +### `_id` |
| 383 | + |
| 384 | +This returns the fully qualified URI of the given instance of the |
| 385 | +`Person` class being returned. |
| 386 | + |
| 387 | +### `_type` |
| 388 | + |
| 389 | +This returns the class at which this instance is instantiated. This is |
| 390 | +useful when a super-class is queried, as we can obtain what concrete |
| 391 | +subclass it corresponds to. |
| 392 | + |
| 393 | +### Backlinks: `_PROPERTY_of_CLASS` |
| 394 | + |
| 395 | +The *backlink* is a way to find all instances that *point* to a given |
| 396 | +class. The backlink is generated automatically for every edge which |
| 397 | +terminates at the current class. |
| 398 | + |
| 399 | +For example, with the Person class: |
| 400 | + |
| 401 | +```json |
| 402 | +{ "@type" : "Class", |
| 403 | + "@id" : "Person", |
| 404 | + "name" : "xsd:string", |
| 405 | + "dob" : "xsd:dateTime", |
| 406 | + "friend" : {"@type" : "Set", "@class" : "Person" }} |
| 407 | +``` |
| 408 | + |
| 409 | +We automatically get the backlink `_friend_of_Person` that says which |
| 410 | +people view us as their friends. |
| 411 | + |
| 412 | +For instance, we can construct the following query: |
| 413 | + |
| 414 | +```graphql |
| 415 | +{ |
| 416 | + Person{ |
| 417 | + name |
| 418 | + _friend_of_Person{ |
| 419 | + name |
| 420 | + } |
| 421 | + } |
| 422 | +} |
| 423 | +``` |
| 424 | + |
| 425 | +This will find the name of every person who views the top level |
| 426 | +`Person` us as their friend (i.e. has a `friend` link to the current |
| 427 | +person). |
| 428 | + |
| 429 | +### Path Queries: `_path_to_CLASS` |
| 430 | + |
| 431 | +A path query allows us to use regular graph expressions to follow |
| 432 | +links from the current object to another object of `CLASS`. |
| 433 | + |
| 434 | +Using the `Person` example: |
| 435 | + |
| 436 | +```json |
| 437 | +{ "@type" : "Class", |
| 438 | + "@id" : "Person", |
| 439 | + "name" : "xsd:string", |
| 440 | + "dob" : "xsd:dateTime", |
| 441 | + "friend" : {"@type" : "Set", "@class" : "Person" }} |
| 442 | +``` |
| 443 | + |
| 444 | +We can find everyone within 2-degrees of separation with the following |
| 445 | +path query: |
| 446 | + |
| 447 | +```json |
| 448 | +{ |
| 449 | + Person{ |
| 450 | + name |
| 451 | + _path_to_Person(path: "friend{1,3}"){ |
| 452 | + name |
| 453 | + } |
| 454 | + } |
| 455 | +} |
| 456 | +``` |
| 457 | + |
| 458 | +See the [complete syntax for path |
| 459 | +queries](../../../guides/how-to-guides/query-data/path-queries.md) for |
| 460 | +more details on the semantics of the path argument. |
0 commit comments