Change Data with GraphQL Mutations

Eve Porcello
InstructorEve Porcello
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 4 years ago

Queries get data from GraphQL services. If we want to change data, we will use GraphQL mutations. In this lesson, we’ll look at how to design GraphQL mutations to increment or decrement the number of ski days on record.

Instructor: [00:00] Let's create a variable called skiDays that we'll set to . Then we'll adjust our totalDays resolve to return skiDays. Next, in our schema we'll create a mutation type, and the two mutations I want to create are addDay which should return an int, and removeDay which should also return an int. Then we'll adjust the resolvers. We'll add a mutation object with two functions, one to increment or add a day, and one to decrement or remove a day.

[00:34] Now in the playground we can test the query and the mutations. First let's query totalDays. Then we want to test addDay. You'll notice that when I add this, we have a red underline. You're only allowed to have one anonymous operation in a query document, so I'll need to give each of these an operation name. Then I can execute the mutation by clicking play and choosing the operation name from the dropdown.

[01:02] Finally, let's test the removeDay mutation and we'll see that it is indeed removing days from our total. To recap, mutations are a type defined in the schema and mutations have resolver functions that match the names in that schema.