Create a GraphQL Mutation in GraphiQL

Share this video with your friends

Social Share Links

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.

egghead
egghead
~ just now

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