Check out a free preview of the full Introduction to GraphQL course:
The "Writing a Mutation Type" Lesson is part of the full, Introduction to GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

Scott demonstrates how to write a mutation with an input type.

Get Unlimited Access Now

Transcript from the "Writing a Mutation Type" Lesson

[00:00:00]
>> Scott Moss: We're talking about the types. We're talking about the queries, mutations. The only other thing that I wanna show you is a special type which is called an input type. So, you have types which are really cool. But, in order to be able to accept arguments on like a mutation, you need to create input types.

[00:00:19] Which are the exact same things as types, literally the exact same thing except for the own users inputs or arguments, that's it. You create an exact and the same way with the difference of just one word. So for instance, if I wanted to make a newCat who has this syntax, what I will do is, one, I know I need to create a mutation for anewCat.

[00:00:39] So what I'll do is I'll come down here to Mutation and I create newCat. I know it will return to newCat, I've got that part. But, I need to pass the fields of the newCat up to the server somehow and then use them. So, like, how would I do that?

[00:00:54] Well, the way I would do that is I need to accept arguments. So, I'd have to say, okay, this newCat takes arguments. And the syntax follows like this. The first part is, you can call this whatever you want, just like a JavaScript function. This is an argument place for it.

[00:01:07] You can call it whatever you want. I like to use input, that way inside of my resolvers I always can use argument.input, and I always use that. So I just like to use input for everything. And then, use a semicolon, you have to give it a type. What is this input's type?

[00:01:22] And this is what I was saying, you have to make an input type. So if we already have a type called Cat that has a name, an age and bestFriends, then I need to make an input type for Cat. And the way I could do that is just type in the word input, and it's the same thing as creating a type.

[00:01:39] In this case, I also like to use the word input for my input, so I'll say catInput. This is a thing that I like to do, so I know that this is an input type and not a regular type.
>> Scott Moss: So I'll say catInput for this input type, and then I'll say, you can create a cat with a name, just like the regular type up there.

[00:02:01]
>> Scott Moss: You can create a Cat that's an age, but it's required, because that's what it is up there. And then, you also have to give me a bestFriend.
>> Scott Moss: I was gonna say best friend.
>> Scott Moss: So now I just made an input for the Cat called catInput. And now I can go down to my mutation and say, all right, for this variable called input, it must be the catInput type.

[00:02:30] I'm gonna make that required.
>> Scott Moss: So now if you wanna do mutation, you can pass up an object that looks like this catInput and it will be accessible to you inside of the resolver for newCat. And that's how you would pass an argument. So again, an input type is the same thing as a type, except for it uses the word input, that's it.

[00:02:51] If it's the exact fields, which in this case it is, you still couldn't put cat down here.
>> Scott Moss: I couldn't put cat here. Yes, David?
>> Speaker 2: What is newCat?
>> Scott Moss: I'm sorry, you're right, it should be cat, there we go.