GraphQL Guide
Disclaimer: Cayley's GraphQL implementation is not strictly a GraphQL, but only a query language with the same syntax and mostly the same rules.
We will use this simple dataset for our examples.
Every query is represented by tree-like structure of nested objects and properties, similar to MQL.
This particular query is equivalent to all nodes in the graph, where id
is the special field name for the value of the node itself.
First root object in traditional GraphQL (named nodes
here) represents a method that will be called on the server to get results. In our current implementation this name serves only as a placeholder and will always execute the object search query.
Our example returns the following result:
First level of JSON object corresponds to a request itself, thus either data
field or errors
will be present.
Any nested objects will correspond to fields defined in query, including top-level name (nodes
).
Limit and pagination
Maximal number of results can be limited using first
keyword:
Pagination can be done with offset
keyword:
This query returns objects 5-7.
Note: Values might be sorted differently, depending on what backend is used.
Properties
Predicates (or properties) are added to the object to specify additional fields to load:
Results:
All predicates are interpreted as IRIs and can be written in plain text or with angle brackets: status
and <status>
are considered equal. Also, well-known namespaces like RDF, RDFS and Schema.org can be written in short form and will be expanded automatically: schema:name
and <schema:name>
will be expanded to <http://schema.org/name>
.
Properties are required to be present by default and can be set to optional with @opt
or @optional
directive:
Results:
Note: Since Cayley has no knowledge about property types and schema, it might decide to return a property as a single value for one object and as an array for another object. This behavior will be fixed in future versions.
Nested objects
Objects and properties can be nested:
All operations available on root also works for nested object, for example the limit:
Reversed predicates
Any predicate can be reversed with @rev
or @reverse
directive (search for "in" links instead of "out"):
Filters
Objects can be filtered by specific values of properties:
Only exact match is supported for now.
GraphQL names are interpreted as IRIs and string literals are interpreted as strings. Boolean, integer and float value are also supported and will be converted to schema:Boolean
, schema:Integer
and schema:Float
accordingly.
Labels
Any fields and traversals can be filtered by quad label with @label
directive:
Label will be inherited by child objects. To reset label filter add @label
directive without parameters.
Expanding all properties
To expand all properties of an object, *
can be used instead of property name:
Un-nest objects
The following query will return objects with {id: x, status: {name: y}}
structure:
It is possible to un-nest status
field object into parent:
Resulted objects will have a flat structure: {id: x, status: y}
.
Arrays fields cannot be un-nested. You can still un-nest such fields by providing a limit directive (will select the first value from array):
Last updated