github.com/cayleygraph/cayley@v0.7.7/docs/graphql.md (about) 1 # GraphQL Guide 2 3 **Disclaimer:** Cayley's GraphQL implementation is not strictly a GraphQL, but only a query language with the same syntax and mostly the same rules. 4 5 We will use [this simple dataset](https://github.com/cayleygraph/cayley/tree/87c9c341848b59924a054ebc2dd0f2bf8c57c6a9/data/testdata.nq) for our examples. 6 7 Every query is represented by tree-like structure of nested objects and properties, similar to [MQL](mql.md). 8 9 ```graphql 10 { 11 nodes{ 12 id 13 } 14 } 15 ``` 16 17 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. 18 19 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. 20 21 Our example returns the following result: 22 23 ```javascript 24 { 25 "data": { 26 "nodes": [ 27 {"id": "bob"}, 28 {"id": "status"}, 29 {"id": "cool_person"}, 30 {"id": "alice"}, 31 {"id": "greg"}, 32 {"id": "emily"}, 33 {"id": "smart_graph"}, 34 {"id": "predicates"}, 35 {"id": "dani"}, 36 {"id": "fred"}, 37 {"id": "smart_person"}, 38 {"id": "charlie"}, 39 {"id": "are"}, 40 {"id": "follows"} 41 ] 42 } 43 } 44 ``` 45 46 First level of JSON object corresponds to a request itself, thus either `data` field or `errors` will be present. 47 48 Any nested objects will correspond to fields defined in query, including top-level name \(`nodes`\). 49 50 ## Limit and pagination 51 52 Maximal number of results can be limited using `first` keyword: 53 54 ```graphql 55 { 56 nodes(first: 10){ 57 id 58 } 59 } 60 ``` 61 62 Pagination can be done with `offset` keyword: 63 64 ```graphql 65 { 66 nodes(offset: 5, first: 3){ 67 id 68 } 69 } 70 ``` 71 72 This query returns objects 5-7. 73 74 _Note: Values might be sorted differently, depending on what backend is used._ 75 76 ## Properties 77 78 Predicates \(or properties\) are added to the object to specify additional fields to load: 79 80 ```graphql 81 { 82 nodes{ 83 id, status 84 } 85 } 86 ``` 87 88 Results: 89 90 ```javascript 91 { 92 "data": { 93 "nodes": [ 94 {"id": "bob", "status": "cool_person"}, 95 {"id": "greg", "status": "cool_person"}, 96 {"id": "dani", "status": "cool_person"}, 97 {"id": "greg", "status": "smart_person"}, 98 {"id": "emily", "status": "smart_person"} 99 ] 100 } 101 } 102 ``` 103 104 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>`. 105 106 Properties are required to be present by default and can be set to optional with `@opt` or `@optional` directive: 107 108 ```graphql 109 { 110 nodes{ 111 id 112 status @opt 113 } 114 } 115 ``` 116 117 Results: 118 119 ```javascript 120 { 121 "data": { 122 "nodes": [ 123 {"id": "bob", "status": "cool_person"}, 124 {"id": "status"}, 125 {"id": "cool_person"}, 126 {"id": "alice"}, 127 {"id": "greg", "status": ["cool_person", "smart_person"]}, 128 {"id": "emily", "status": "smart_person"}, 129 {"id": "smart_graph"}, 130 {"id": "predicates"}, 131 {"id": "dani", "status": "cool_person"}, 132 {"id": "fred"}, 133 {"id": "smart_person"}, 134 {"id": "charlie"}, 135 {"id": "are"}, 136 {"id": "follows"} 137 ] 138 } 139 } 140 ``` 141 142 _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._ 143 144 ## Nested objects 145 146 Objects and properties can be nested: 147 148 ```graphql 149 { 150 nodes{ 151 id 152 follows { 153 id 154 } 155 } 156 } 157 ``` 158 159 All operations available on root also works for nested object, for example the limit: 160 161 ```graphql 162 { 163 nodes(first: 10){ 164 id 165 follows(first: 1){ 166 id 167 } 168 } 169 } 170 ``` 171 172 ## Reversed predicates 173 174 Any predicate can be reversed with `@rev` or `@reverse` directive \(search for "in" links instead of "out"\): 175 176 ```graphql 177 { 178 nodes{ 179 id 180 followed: <follows> @rev { 181 id 182 } 183 } 184 } 185 ``` 186 187 ## Filters 188 189 Objects can be filtered by specific values of properties: 190 191 ```graphql 192 { 193 nodes(id: <bob>, status: "cool_person"){ 194 id 195 } 196 } 197 ``` 198 199 Only exact match is supported for now. 200 201 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. 202 203 ## Labels 204 205 Any fields and traversals can be filtered by quad label with `@label` directive: 206 207 ```graphql 208 { 209 nodes{ 210 id 211 follows @label(v: <fb>) { 212 id, name 213 follows @label { 214 id, name 215 } 216 } 217 } 218 } 219 ``` 220 221 Label will be inherited by child objects. To reset label filter add `@label` directive without parameters. 222 223 ## Expanding all properties 224 225 To expand all properties of an object, `*` can be used instead of property name: 226 227 ```graphql 228 { 229 nodes{ 230 id 231 follows {*} 232 } 233 } 234 ``` 235 236 ## Un-nest objects 237 238 The following query will return objects with `{id: x, status: {name: y}}` structure: 239 240 ```graphql 241 { 242 nodes{ 243 id 244 status { 245 name 246 } 247 } 248 } 249 ``` 250 251 It is possible to un-nest `status` field object into parent: 252 253 ```graphql 254 { 255 nodes{ 256 id 257 status @unnest { 258 status: name 259 } 260 } 261 } 262 ``` 263 264 Resulted objects will have a flat structure: `{id: x, status: y}`. 265 266 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\): 267 268 ```graphql 269 { 270 nodes{ 271 id 272 statuses(first: 1) @unnest { 273 status: name 274 } 275 } 276 } 277 ``` 278