Introduction to GraphQL Query & Mutation Types
This course has been updated! We now recommend you take the Server-Side GraphQL in Node.js course.
Transcript from the "Query & Mutation Types" Lesson
>> Scott Moss: So we talked about types, scholars, and object types. Now we're going to talk about query and mutation types because those are also types too. All right, if we go back and look at the code we have here, I had to put the word type in front of query because it is a type.
[00:00:14] So we need to talk about those. Basically, this is how you do CRUD on your GraphQL API, you make queries and mutations, it's that simple, basically. All right, so query type describes the different queries your API is capable of, right? So if you go back to the code that we have here, I have a type query.
[00:00:32] And now I'm describing the different queries that my API can do, I can do a myCat query, I can do a hello query. Queries is like, ways to read data, so like getOne, getMany, those are queries. So, whatever I want to be able to do, publicly on my API, I need to define as a query.
[00:00:48] If I want to be able to get one cat, I should get one cat here, and put its return type as a cat. If I want to get many cats, I'll do something like cats, and I'll say you get back an array of cats, all right? If it's not on the query, you can't perform it, so it doesn't matter if you have a type up here for something, type Car.
[00:01:13] If it's not being referenced by a type that's eventually on a query, or if it's not on a query itself, it's inaccessible. It has to be, at some point, referenced by a query. Whether through the tree, because it's referenced by a cat and a cat that's on the query, or if it's on the query itself.
[00:01:28] It has to be there eventually, otherwise you might as well just delete it from the schema, it's not even being used. So you have to add it to your query eventually to be able to retrieve it.
>> Scott Moss: A query operation is just a name with the possible arguments that eventually returns the type.
[00:01:47] So basically what that is, is if we go back to our playground.
>> Scott Moss: So when you run a query like myCat, this is a query operation. The operation's name is called myCat, that's the name of the query, right? And we know that because, that's what we have in our schema, that is the name of it.
[00:02:09] So these fields are the names of the queries that we will be executing, so they have to match. So, it's literally whatever this name is, that's what you would put in the incoming request so that the names match up exactly. And again, their job is just to return some type of field, or I'm sorry, some type of type.
>> Scott Moss: A mutation is the exact same thing, but with the intent of mutating the database versus just reading. So, if queries are for getOne and getMany, mutations is for update, create, and delete. That's for mutating, so that's what you do for mutation. It's the exact same thing, like I said, the only difference is, it's going to be mutating in the database.
[00:02:47] And most of the time mutations, because you're trying to change something on the database. They're probably going to take arguments, most of the time they'll take arguments. Sometimes you'll have mutations that's just like, delete everything, you don't need an argument, yeah, you might have those. Most of the time, it's like, delete this one thing by this ID, update this one thing by this ID or create this new thing with this payload here.
[00:03:05] So you'll have arguments for mutations most likely, especially if you're just doing like CRUD stuff on your GraphQL API. And a mutation just looks very much like a query, you come here, you make a mutation.
>> Scott Moss: Call it type Mutation, and you start defining the different mutations that you can do the same way you did to the query.
[00:03:24] So if I had a mutation called newCat, I could say, I've got a mutation called newCat and it returns a newCat like this.
>> Scott Moss: You could say that, and then if you wanted to pass the arguments to it, you would have to say, call it whatever you want input.
[00:03:43] And then you've got to give it a special type called an input that we're going to talk about in a minute. Yeah, so another way of thinking of them is just like, thinking this is like, they're basically your routes. All right, if you had a REST API, and you had all these different routes, and verb combinations, that's what you could do.
[00:03:58] The queries and mutations are your routes, they're what your API can do, so think about it like that. If you're like, what, what the hell is a curating mutating? They're basically your routes that people can use.