You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: site/docs/Learn-Queries.md
+15-24Lines changed: 15 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,36 +181,27 @@ Most discussions of GraphQL focus on data fetching, but any complete data platfo
181
181
182
182
In REST, any request might end up causing some side-effects on the server, but by convention it's suggested that one doesn't use `GET` requests to modify data. GraphQL is similar - technically any query could be implemented to cause a data write. However, it's useful to establish a convention that any operations that cause writes should be sent explicitly via a mutation.
183
183
184
-
Here's an example of a simple mutation:
184
+
Just like in queries, if the mutation field returns an object type, you can ask for nested fields. This can be useful for fetching the new state of an object after an update. Let's look at a simple example mutation:
You can see that a mutation can contain multiple fields, just like a query. There's one important distinction between queries, and mutations, other than the name:
194
-
195
-
**While query fields are executed in parallel, mutation fields run in series, one after the other.**
196
-
197
-
This means that even though we sent `createCharacter` and `addCharacterToEpisode` in one request, the first is guaranteed to finish before the second begins, ensuring that we create the character before trying to add it an episode.
196
+
In this case, the `incrementCredits` mutation field returns a `Human` object, so we can query the new value of `totalCredits` after giving that character some more credits. Otherwise, we would have needed to send two requests - one to update the credits, and another to get the new value - or guess at the new amount based on outdated data.
198
197
199
-
#### Returning data from mutations
198
+
#### Multiple fields in mutations
200
199
201
-
Just like in queries, you can ask for nested fields in the mutation result. This can be useful for fetching the new state of an object after an update:
200
+
A mutation can contain multiple fields, just like a query. There's one important distinction between queries and mutations, other than the name:
202
201
203
-
```graphql
204
-
mutationIncrementCredits($characterId: ID!) {
205
-
incrementCredits(characterId: $characterId) {
206
-
totalCredits
207
-
}
208
-
}
209
-
```
210
-
211
-
In this case, the `incrementCredits` mutation field returns a `Character` object, so we can query the new value of `totalCredits` after giving that character some more credits. Otherwise, we would have needed to send two requests - one to update the credits, and another to get the new value - or guess at the new amount based on outdated data.
202
+
**While query fields are executed in parallel, mutation fields run in series, one after the other.**
212
203
213
-
That's all! Now you know everything you need to know about GraphQL queries and mutations to build a pretty good application. For more advanced features and tips, check out the advanced section.
204
+
This means that if we send two `incrementCredits` mutations in one request, the first is guaranteed to finish before the second begins, ensuring that we don't end up with a race condition with ourselves.
@@ -237,4 +228,4 @@ In this query, the `hero` field returns the type `Character`, which might be eit
237
228
238
229
To ask for a field on the concrete type, you need to use an _inline fragment_ with a type condition. Because the first fragment is labeled as `... on Droid`, the `primaryFunction` field will only be executed if the `Character` returned from `hero` is of the `Droid` type. Similarly for the `homePlanet` field for the `Human` type.
239
230
240
-
Named fragments can also be used in the same way, since a named fragment always has a type condition attached.
231
+
Named fragments can also be used in the same way, since a named fragment always has a type attached.
The `hero` field returns the type `Character`, which means it might be either a `Human` or a `Droid` depending on the `episode` argument. In the query above, you can only ask for fields that exist on the `Character` interface, and to ask for a field on a specific object type, you need to use an inline fragment:
266
+
The `hero` field returns the type `Character`, which means it might be either a `Human` or a `Droid` depending on the `episode` argument. In the query above, you can only ask for fields that exist on the `Character` interface, which doesn't include `primaryFunction`.
267
+
268
+
To ask for a field on a specific object type, you need to use an inline fragment:
@@ -275,20 +285,30 @@ Learn more about this in the [inline fragments](XXX) section in the query guide.
275
285
276
286
Union types are very similar to interfaces, but they don't get to specify any common fields between the types.
277
287
278
-
XXX no example in SWAPI
279
-
280
288
```graphql
281
-
unionSearchResult = Photo | Person
289
+
unionSearchResult = Human | Droid | Starship
290
+
```
282
291
283
-
typePerson {
284
-
name: String
285
-
age: Int
286
-
}
292
+
Whereverwereturna `SearchResult` typeinourschema, we might get a `Human`, a `Droid`, or a `Starship`. Note that members of a union type need to be concrete object types; you can't create a union type out of interfaces or other unions.
287
293
288
-
typePhoto {
289
-
height: Int
290
-
width: Int
294
+
In this case, if you query a field that returns the `SearchResult` union type, you need to use a conditional fragment to be able to query any fields at all:
295
+
296
+
```graphql
297
+
# { "graphiql": true}
298
+
{
299
+
search(text: "an") {
300
+
...onHuman {
301
+
name
302
+
height
303
+
}
304
+
...onDroid {
305
+
name
306
+
primaryFunction
307
+
}
308
+
...onStarship {
309
+
name
310
+
length
311
+
}
312
+
}
291
313
}
292
314
```
293
-
294
-
Inthiscase, ifyouqueryafieldthatreturnsthe `SearchResult` uniontype, youneedtouseaconditionalfragmentto be able to query any fields at all.
0 commit comments