Skip to content

Commit 2df5918

Browse files
author
Sashko Stubailo
committed
Add mutations and input type
1 parent ddb9e83 commit 2df5918

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

site/docs/Learn-Queries.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,31 +186,30 @@ Here's an example of a simple mutation:
186186
```graphql
187187
mutation CreateCharacterInEpisode($name: String!, $appearsIn: Episode!) {
188188
createCharacter(name: $name)
189-
addCharacterToEpisode(name: $name, episode: $appearsIn)
190189
}
191190
```
192191

193-
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.
198-
199192
#### Returning data from mutations
200193

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:
194+
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:
202195

203196
```graphql
204-
mutation IncrementCredits($characterId: ID!) {
205-
incrementCredits(characterId: $characterId) {
197+
mutation IncrementCredits($humanId: ID!) {
198+
incrementCredits(humanId: $humanId) {
206199
totalCredits
207200
}
208201
}
209202
```
210203

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.
204+
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.
205+
206+
#### Multiple fields in mutations
207+
208+
A mutation can contain multiple fields, just like a query. There's one important distinction between queries and mutations, other than the name:
209+
210+
**While query fields are executed in parallel, mutation fields run in series, one after the other.**
212211

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.
212+
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.
214213

215214
### Fragments and type conditions
216215

site/docs/_swapiSchema.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ schema {
2424
2525
type Query {
2626
hero(episode: Episode): Character
27-
search(query: String): [SearchResult]
27+
search(text: String): [SearchResult]
28+
}
29+
30+
type Mutation {
31+
createHuman(human: HumanInput!): ID!
32+
incrementCredits(humanId: ID!): Human
2833
}
2934
3035
# The episodes in the Star Wars trilogy
@@ -70,6 +75,9 @@ type Human implements Character {
7075
7176
# A list of starships this person has piloted, or an empty list if none
7277
starships: [Starship]
78+
79+
# The number of credits this human has
80+
totalCredits: Int
7381
}
7482
7583
# An autonomous mechanical character in the Star Wars universe
@@ -90,6 +98,15 @@ type Droid implements Character {
9098
primaryFunction: String
9199
}
92100
101+
# The object to be passed in when creating a new human
102+
input HumanInput {
103+
# The name of the new human
104+
name: String!
105+
106+
# The movies this human appears in
107+
appearsIn: [Episode]!
108+
}
109+
93110
# Units of length
94111
enum LengthUnit {
95112
# The standard unit around the world

0 commit comments

Comments
 (0)