This course has been updated! We now recommend you take the Advanced GraphQL, v2 course.
Transcript from the "GraphQL Recap: Mutations" Lesson
[00:00:00]
>> Scott: No, okay, so the other thing about GraphQL is, cool, that's how you read data, how do you create data, right? What's the equivalent of a post or a put request, right? So the way you would do that is called a, anybody know?
>> Speaker 2: Mutation.
>> Scott: A mutation, exactly, it's a mutation.
[00:00:20] What's funny about a mutation is that there's nothing special about it, it's literally just a query with some work in front of it. Like, it's the same thing, so we're going to make a mutation which basically is the equivalent of like a post or a put request. It's like I need to mutate something on the database or somewhere, whatever your data source is.
[00:00:39] So we're going to make a Mutation room and call it newPerson. In this case, what we're going to do when we describe the arguments for this new person, instead of putting them in line here. Which can be actually not that bad because a person can only have a name, we're just going to make an input instead.
[00:00:57] So input is a special type that can only be used for arguments, that's all input is. It's a type that can only be used for arguments, you can't use it anywhere else, I'm going to make an input called NewPersonInput. And my thing is, whenever I make an input, I always put the word input after, so I don't get confused with a type and an input.
[00:01:21] Because if I just put like, if I didn't put this input at the end of it. I wouldn't know if that was an input or a type without explicitly looking at it, so just by the name I always do that. And let's say a new person takes a name and its type is going to be String.
[00:01:37] So now when I come here to newPerson, I'm going to say, yep, you take an input. I can put whatever I want here, I just like to call it input as well. And then its type is going to be NewPersonInput, non-null, and then it's going to return a Person, all right?
[00:01:53] So now, we need to resolve this mutation, so again, let's open this up on the side.
>> Scott: And what we'll do, again, each key is going to be one of these types, so we have the Query type, we have that here. We also have this Person type, but we're not resolving it, so we didn't need to write it, now we've got this mutation type.
[00:02:17] So, we need to make a key for that, so, we'll say Mutation.
>> Scott: And then we need to resolve the newPerson field on the Mutation, so, we'll say newPerson.
>> Scott: And we know that there's going to be an argument and we need to do something, we need to return a person.
[00:02:36] So, whenever you write a resolver, there's actually four arguments in a resolver. The first one is going to be the value passed down to this resolver, we'll get there in a minute. But basically, this is a top level resolver, so there is no value that's passed down, unless I pass the server a start value, which I did not.
[00:02:54] So I'm just going to put an underscore as a placeholder here, because this is actually like null, there's nothing here. The second argument is actually the argument that was passed in here, and I'm just going to reference it as args. The third argument is going to be what's called context, we are not going to use it here, but we will use it today.
[00:03:12] And, we'll see what that looks like, and the fourth argument is called info. You will almost never use this unless you're doing some crazy stuff, but we will talk about this today as well. This is basically the raw AST of the GraphQL query, so it's pretty advanced stuff here.
[00:03:26] But basically all we want to be able to do now is just take that name that you created, or that you passed in. And we're going to return an object with that name, so let's get the name from the Args. So the name is actually going to be args.input, it's going to be whatever the name is here, args.input.
[00:03:47]
>> Scott: And because input is type newPerson, it has a field on it called name. So it's args.input.name, that's where the name comes from. And now we're just going to return something that looks like a person, which is an object with a name property, so we'll do that. And we'll just say name.
[00:04:06]
>> Scott: So now let's run this, let's restart the server, go back to GraphQL Playground. If we hit Refresh, actually I think this updates in real time, but let's hit Refresh anyway. Now we have Mutations, and there's a newPerson here, so let's do that, let's do a mutation here.
[00:04:24] And I was going to write a long one out, we'll just call this NewPerson, and it's a newPerson. And basically we've got to pass this in variables, this is advanced, so I mean it's not advanced, but I'm going to cover it later. So I'm just going to fly through this.
[00:04:39] But basically I'm passing in a variable into this operation of GraphQL and its type is NewPersonInput, non-null. And it takes an input variable whose value is that variable, and then now I can actually add some variables down here. Called input, which is an object with a name property on it, and we can give it a name.
[00:05:08]
>> Scott: And then now I can ask for that name, and there we go, we just created a person called Bill. So I went through that really fast because we're going to cover it later. But yeah, that's just how you would use the GraphQL Playground. I just wrote a long mutation, there's a shorthand method for it, but I wanted to use variables, so that's basically GraphQL.
[00:05:31] If you would have known that, then you pretty much are where you need to be for this course. If you didn't know that, this is where you need to be for this course. [LAUGH] And it's fine, you're going to learn along the way, we're not going to leave anybody behind.
[00:05:46] But that's kind of where I left off on the last course, so we're going to pick up from here.