Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Change Data with GraphQL Mutations

    Eve PorcelloEve Porcello

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: 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.

    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.

    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.