Create a GraphQL Mutation in GraphiQL

Share this video with your friends

Send Tweet
Published 2 years ago
Updated 2 years ago

Now we need to actually update our data. This is done in Graphql with "mutations". In this lesson, we design a mutation in GraphiQL.

Make sure to wrap your mutation in mutation() {}. You will also pass your variables as arguments to mutation and declare their type before you can use them in your mutations.

Instructor: [0:00] In GraphQL, we have two mutations available to us. We have updateCraft and addCraft. Let's look at updateCraft. To carry out a mutation, we declare as a mutation, and inside of that, we say which mutation we want to use. In this case, we want the updateCraft. We pass in the variables that we want to use and the things that we want to get back.

[0:21] When I update the craft, I'm going to update the name. I'm going to need to pass in a name variable. The type, we'll need to pass in a type variable. The brand, which is going to be variable brand. The price is going to be price. The age, which is going to be age, and the ID, which is going to be the ID.

[0:44] To receive these, we need to pass them into the mutation. As we pass them in, we have to type them. We can see here what types they need to be. For name, that's going to need to be a string. For a dollar type, that needs to be a string also. Brand, string. Price, string. Age, int, and ID, ID!

[1:07] The only one that's compulsory is the ID. If we go back to our application here, we can see the ID within our URL. Let's grab that. When we pass it in as JSON, we don't have the dollar, it's just ID. Let's pass in ID, and then let's change the name to this NewBoat.

[1:25] Let's send that, and we get the ID back again. If we go back to our application and we refresh this, we can see this NewBoat is what the API now has as the boat's name.