This course has been updated! We now recommend you take the Server-Side GraphQL in Node.js course.

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

Students are instructed to create resolvers for the Queries and Mutations on the product type.

Get Unlimited Access Now

Transcript from the "Resolvers Exercise" Lesson

[00:00:00]
>> Scott Moss: Let's hop into the exercise then, cuz this one's probably gonna be the most challenging one cuz resolvers is not easy, but I made it really simple for you all. So what we're gonna do is, in this exercise, you'll be creating resolvers for the Queries and Mutations on the product type.

[00:00:14] In the last exercise, you created the Queries and the Mutations in the schema, now you're actually gonna write the resolvers for them. And you're gonna use the Mongoose models to perform CRUD in those resolvers. So if you've taken the REST course, it's basically some of the same CRUD operations that we did in the REST course with the Mongoose models.

[00:00:29] But they're actually just gonna be a lot simpler in this course because GraphQL makes it simpler. As in you don't have to deal with a lot of the stuff you had to do with the REST, like parameters and stuff like that. So first thing is check out the lesson-3 branch, and then you're gonna create resolvers for the product queries.

[00:00:46] So let me just check out and show you what I'm talking about.
>> Scott Moss: So if we head over to,
>> Scott Moss: Let's bring this up, let's look at the product GraphQL side by side. If we scroll down to the queries, we got two queries here, we have products and we have product, okay?

[00:01:12] So you're gonna make the resolvers for both of these. So if you head over to product.resolvers, that means, because it's this query.products and query.product, you have to make them inside of here. You can also just create them outside of here and then just add them here, it's however you wanna do it.

[00:01:33] But eventually you have to go inside this query object with the same names. The keys in this query object have to be the same name as the keys here. And that's how they match, right? So you gotta have queries, you gotta have the resolvers for those queries. Now, remember, so if you look at them, products is expecting an array of products, product is taking an ID and turning one product, and you have a product model up here up top.

[00:01:56] You don't really need to understand how that model works, what fields are in it because you don't need to, you just need to find one or find meaning, that's it. You don't really need to know what the fields do, that subtracts away from you. So that's one thing you gotta do.

[00:02:10] And then you gotta create the resolvers for the product mutations. So if you look at these mutations, we have a new product. So you need to be able to create a products. And you can see, it takes an argument called input. So that means, in its resolver, you'll have a second argument called ergs.

[00:02:24] On that args object, there'll be an input. That input is gonna be a new product they wanna create, right? Cuz if you go look at the new input type, it looks like this. So you got an object that looks like this. We'll have at least these four properties and optionally these five properties.

[00:02:38] So you'll just take that, you'll use Mongoose, and you'll just say, make a new product given args.input, and you'll return a Product, that's it. UpdateProduct take an ID, and then the update that they wanna update to, and it uses Mongoose to mongoose.findById and update. Product.findById and update, update, and it returns that new product.

[00:02:59] And the remove product does the same thing, give it an ID, product.findById, and it remove and return that product. So you're going to be writing those five resolvers. The one thing to note, if you didn't notice when I was writing my resolvers, there is no request and response, so you can just return the value.

[00:03:17] You don't have to respond back with the status and then .send or .jsend, like you would in Express, to send back. And in resolvers, you just return the value, that's it. You don't have to respond back, GraphQL does the responding for you, you just resolve the data. So that's what you're gonna do.

[00:03:33] So if I were to walk through one of these very quick, I'm just gonna show you. So if I were gonna make a query for products, I like to make them out here. So I'll say products,
>> Scott Moss: There's no first argument cuz there's nothing set. Products doesn't take any argument, so I don't really care about them, and I don't really care about context.

[00:03:53] Actually, I don't care about any of these arguments, so I'm just gonna skip them. And what I need to do now is just return, oops, get that arrow function going, I just need to return a list of products. Well, I'll just say return Product.find({}).exec(), that's it. And then now I'll add it to,

[00:04:15]
>> Scott Moss: Products. Boom, that resolver's done,
>> Scott Moss: That's it. Like I said, it's much simpler than REST. All you can see, cuz the only thing this thing is concerned about is resolving data. It's not concerned about status codes and error messages, it's just resolve the data. And you know what data to resolve.

[00:04:33] If you get lost, like, I don't know what I'm supposed to return, go back and look at your schema, it's an array of products, okay, cool. What am I supposed to return here? It returns a product, got it. So you know exactly what to do by looking at the types that you define.

[00:04:48]
>> Scott Moss: And don't forget to add them below. And as far as your tests go, the command you wanna run is gonna be npm run, or yarn, test-resolvers.
>> Scott Moss: So you go ahead and you test these resolvers. You can see that the one that I wrote already passed the test.

[00:05:13]
>> Scott Moss: There is one more test in here that you're gonna have to do, and it's, and we talked about it, it's resolving other fields, for instance like a product. You're gonna have to write a resolver for the created by field on a product, which basically takes a product, gets to created by field, and then finds a user by the ID and returns it.

[00:05:34] So we're gonna have to do that. And remember, if you wanna resolve a field for a type, you put it on the type. So you would put that here, you would be created by here.
>> Scott Moss: Don't worry about resolve type, we'll talk about that later. But you would put created by here.

[00:05:48] So if you wanna resolve the created by field,
>> Scott Moss: For this product, you'd put it here. Remember, when you're on an s or resolver, that first argument is gonna be what was resolved before. So in this case, what was resolved before the created by field? Well, the entire product was resolved before the created by field.

[00:06:08] Because it could come from products or it could come from product, but either way, it's gonna be a product. It's gonna be one of that, so that first argument's gonna be that. So you might need that to get the .createdByID and look up the user for it. So, and you already have the user up here as well.

[00:06:24] So it can get pretty complicated, but I have faith.