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.


    Use an Input Type to Create an Account with a GraphQL Mutation

    Eve PorcelloEve Porcello

    Mutations are another type of GraphQL operation that are similar to queries, but they are used when you need to change any data on the backend. In this lesson, we will send mutations to register new users.

    To follow along with these queries, go to the Pet Library GraphQL Playground.



    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: To change data with GraphQL, we use a mutation. These are named just like queries and the schema. Within the schema, we have a mutation called createAccount. Let's write that mutation.

    We're going to use the mutation keyword. We're going to use the name of the mutation createAccount. It looks it takes in something called an input, which is createAccount input.

    If we scroll down a little bit and click on the input, we're going to see that createAccount is actually a wrapper around name, user name, and password. Every time I create an account, I'm going to need to provide those things.

    Now here's where the input comes in handy. Instead of sending all of these variables one at a time, I can wrap them in the input, and then I can send them as one thing.

    Here I am going to use the input, and I am going to pass in the input as a variable. We'll set this up at the top. Input is of type createAccount input, and is non-nullable. We'll use the exclamation point. We'll set up this argument to take in the input.

    I'll use the query variables panel to pass in these variables, but this time we are going to put everything on that input key. We're going to nest in object here with name, with user name, and with password. Now that I have these input values defined, I need to return something from this mutation.

    What this mutation returns is a customer object. This will give us access to all of the fields on the customer. Whenever I create my account, it's going to echo back those account details that I provided in the input. We'll ask for user name and name.

    When we send this mutation, we're going to send all of the values from the input object. We're going to get back the user name and the name for the customer that's just been created.

    CreatAccount takes in an input called createAccount input. We pass the variables in the query variables panel, and then the mutation returns a customer object so I can access those values that I've just supplied.