Check out a free preview of the full Server-Side GraphQL in Node.js course

The "Mutation Exercise" Lesson is part of the full, Server-Side GraphQL in Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to define a mutation type in the schema, add fields to the mutation type, create input types or mutation field arguments and create resolvers for the mutation fields.

Preview
Close

Transcript from the "Mutation Exercise" Lesson

[00:00:00]
>> Scott Moss: So you're gonna a define a mutation type in your schema. You're gonna add some fields on your mutation type. You're gonna create input types for your mutation field arguments. And you're gonna create resolvers for your mutation fields. So the mutation stuff that you're going to be creating, if you head over to the schema, we need to be able to create a pet.

[00:00:18]
So you're gonna make a mutation that allows us to create a new pet. It should take in all the necessary input that a pet needs. In this case, a pet just needs, don't worry about ID. ID is generated for you. So you don't have to ask for an ID.

[00:00:32]
That's generated. You did need to take in the. And you don't worry about createdAt, that should also eventually get generated for you. But you do need a name, a type, that's basically it. Don't worry about image either. All that's just there for you. Just a name and a type.

[00:00:50]
>> Scott Moss: So that's what the pet's gonna need. All this other stuff should get generated for you. You shouldn't have to generate the ID or the createdAt or any of that stuff. You should be good to go. And what you're going to want to use if you go look at the pet model, this should be a create function here.

[00:01:09]
And you can see this is where it creates the ID for you. It creates the createdAt now for you. And then this is the pet that you will pass it. So it does, like I said, all the stuff gets generated for you. So your input doesn't need these fields.

[00:01:20]
But your input is gonna need the other required fields. I guess you can make an image not required, in this case. Name and type, so you don't need a name and type here. So mutation. Make a mutation for creating a pet, make an input to handle the arguments for that mutation.

[00:01:37]
And then you need to write a resolver inside your resolvers file that actually handles the mutation for creating that pet. And if everything goes right, when you create it, you will actually be able to go into the database, this JSON file, and you'll see it pop up here.

[00:01:53]
When you do see that, write a query inside of graphical playground to query for that pet using either pets or pet and see if you can get it back. You should be able to get it back. And that's basically a whole cycle of mutations and queries.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now