Change Data with GraphQL Mutations

Eve Porcello
InstructorEve Porcello
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 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.

egghead
egghead
~ 20 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today