Skip to content

Commit c602564

Browse files
committed
Add additional mutation example.
1 parent b83f1ca commit c602564

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/components/marked/swapi-schema.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const typeDefs = /* GraphQL */ `
3131
"The mutation type, represents all updates we can make to our data"
3232
type Mutation {
3333
createReview(episode: Episode, review: ReviewInput!): Review
34+
rateFilm(episode: Episode!, rating: FilmRating!): Film
3435
updateHumanName(id: ID!, name: String!): Human
3536
deleteStarship(id: ID!): ID
3637
}
@@ -47,6 +48,24 @@ const typeDefs = /* GraphQL */ `
4748
JEDI
4849
}
4950
51+
"A personal rating for a Star Wars episode"
52+
enum FilmRating {
53+
"Negative rating"
54+
THUMBS_DOWN
55+
56+
"Positive rating"
57+
THUMBS_UP
58+
}
59+
60+
"A film from the Star Wars trilogy"
61+
type Film {
62+
"The Star Wars episode portrayed in the film"
63+
episode: Episode!
64+
65+
"The authenticated user's rating of the film"
66+
viewerRating: FilmRating
67+
}
68+
5069
"A character from the Star Wars universe"
5170
interface Character {
5271
"The ID of the character"
@@ -367,6 +386,10 @@ const resolvers = {
367386
},
368387
Mutation: {
369388
createReview: (root, { episode, review }) => review,
389+
rateFilm: (root, { episode, rating }) => ({
390+
episode,
391+
viewerRating: rating,
392+
}),
370393
updateHumanName: (root, { id, name }) => {
371394
const human = humanData[id]
372395
if (!human) {

src/pages/learn/mutations.mdx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ import { Callout } from "nextra/components"
66

77
Most discussions of GraphQL focus on data fetching, but any complete data platform needs a way to modify server-side data as well.
88

9-
In REST, any request might cause 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 field resolver could be implemented to cause a data write—but the [GraphQL specification states](https://spec.graphql.org/draft/#sel-GANRNDAB6DBmMn6D):
9+
In REST, any request might cause 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 field resolver could be implemented to cause a data write—but the [GraphQL specification states](https://spec.graphql.org/draft/#sel-GANRNDAB6DBmMn6D) that "the resolution of fields other than top-level mutation fields must always be side effect-free and idempotent." Thus, for any spec-compliant GraphQL schemas, only the top-level fields in mutation operations are allowed to cause side effects.
1010

11-
> the resolution of fields other than top-level mutation fields must always be side effect-free and idempotent
12-
13-
Thus only the top-level fields in mutation operations are allowed to cause side effects; to do otherwise would make your schema GraphQL non-compliant.
14-
15-
On this page, you'll learn how to use mutation operations to create, update, and delete data using GraphQL.
11+
On this page, you'll learn how to use mutation operations to write data using GraphQL, and do so in a way that supports client use cases.
1612

1713
<Callout type="info">
1814
All of the features of GraphQL operations that apply to queries also apply to mutations, so review the [Queries](/learn/queries/) page first before proceeding.
@@ -99,7 +95,19 @@ This example demonstrates an important distinction from REST. To update a human'
9995

10096
Purpose-built mutation fields can help make a schema more expressive by allowing the input types for field arguments to be Non-Null types (a generic `updateHuman` mutation would likely need to accept many nullable arguments to handle different update scenarios). Defining this requirement in the schema also eliminates the need for other runtime logic to determine that the appropriate values were submitted to perform the client's desired write operation.
10197

102-
Schemas should be designed to help clients get the data that they need from the GraphQL API, so the fields defined in a schema should be informed by those use cases.
98+
GraphQL also allows us to express relationships between data that would be more difficult to model semantically with a basic CRUD-style request. For example, a user may wish to save a personal rating for a film. While the rating belongs to the user and doesn't modify anything related to a film itself, we can ergonomically associate it with a `Film` object as follows:
99+
100+
```graphql
101+
# { "graphiql": true, "variables": { "episode": "EMPIRE", "rating": "THUMBS_UP" } }
102+
mutation RateFilm($episode: Episode!, $rating: FilmRating!) {
103+
rateFilm(episode: $episode, rating: $rating) {
104+
episode
105+
viewerRating
106+
}
107+
}
108+
```
109+
110+
As a general rule, schemas should be designed to help clients get the data that they need from the GraphQL API, so the fields defined in a schema should be informed by those use cases.
103111

104112
## Remove existing data
105113

0 commit comments

Comments
 (0)