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 "Implimenting a Resolver" 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 live codes several resolvers to demonstrate how the data is called.

Get Unlimited Access Now

Transcript from the "Implimenting a Resolver" Lesson

>> Scott Moss: That's like the basics of resolvers. Now we're actually gonna write some so we can see how they work. So what I'm gonna do is get rid of that. I'm going to just do a quick one here, and I'll just go back to what we had in serverJS, and I'll do some stuff here.

[00:00:16] So I'm gonna just clean this stuff up so it's not getting in the way and breaking our stuff, so. What I'm gonna do is, I'll go back and I'll make a type. And I really like cats, [LAUGH] I'll make a type called Cat. Name is String. And let's put the age back here again.

[00:00:36] Cool, so we got that. And then what I'll do is I'll make another type called Owner, and its name is a string. We'll also get a cat and allocate it a name. And it has a cat. And its type is gonna be a cat. And then cat also has an owner.

[00:00:58] And its type is gonna be an owner,
>> Scott Moss: So we got that. Let me get rid of this mutation here. And then for our query type, let's go ahead and do that. We'll make some queries. We'll say, get a cat by a name which is a string. And that's just gonna return a cat.

[00:01:24] And then we'll say, get an owner by a name, which is a string. That's just gonna return a owner.
>> Scott Moss: So now that we have that, what I'm gonna do is I'm just gonna go ahead and write a resolver so this thing actually works. So what do I absolutely have to write resolvers for in this scenario?

>> Speaker 2: The two queries.
>> Scott Moss: Right, the two queries, Cat and Owner, I have to write resolvers for those, otherwise this stuff's just not gonna work. So to do that, remember, I gotta write query, cuz they're on the query type. And then the name of the queries I want is gonna be cat, so that's one there.

[00:02:08] And the other one is gonna be owner, so there's one there. So I have to do those. So we'll do that, and then we'll see what other ones we can write. So for cat, if we look at the cat query, we know it takes a name which is a string, so we'll do that.

[00:02:23] So the first argument, like I said, it's almost always gonna be null once you set it. So I'm just gonna put a placeholder there. The second argument is gonna be args which we know is gonna be a name property in there. And then we have context which literally is nothing, so I'm not gonna even account for it.

[00:02:37] So then I'll just say, given this name of a cat, I'll just return a cat that has that name. So I'll just do that, I'll say return a name, and I'll just give the name that you passed me, We'll do that, and then we'll set an age of three.

[00:02:58] And then for owner, we don't have that yet so we'll come back to it, we'll just put empty object. So we got that query, and then for owner, we have a name which is a string. And it also takes an argument, so we'll do the same thing. So placeholder, nothing is gonna there.

[00:03:16] Arg, we know it's gonna be a name. So now we'll just return name. I'll give it the same name that you passed me, And then cat, we don't have that yet so we'll get back to it.
>> Scott Moss: So what I'm gonna do now is I'm just going to run some logs in here so you can see in what order stuff is happening.

[00:03:39] So console log and cat resolver.
>> Scott Moss: And then in owner resolver. Okay, everybody following me so far? Okay, so let's start the server.
>> Scott Moss: Cool, so 3,000, head back over here, refresh. Get a new schema, cool, so we got our cat and our owner. So let's go ahead and make some queries here.

[00:04:17] So I'm gonna go ahead and do the cat query. I have to pass in a name, so you can see that passing a variable inside of playground, you can just write them in line like I'm doing now. So I can give a name like Timmy, like this. I could also add them here, but we'll talk about that when we get to the mutation stuff in a minute because it makes more sense there.

[00:04:46] It goes away, there it is. Okay, so we will just write it in line. So cat, watch for the name, make sure that works. And that looks good, got the name, got the age. And then if I ask for owner it's going to error out here because owner is an object type.

[00:05:02] So what fields on the owner do you want? That's what it's asking for right now. It's freaking out, so these are scholar type so you don't have to expand them. This is an object type, so what do you want back from the owner? You can't just ask for the owner and get back the whole object.

[00:05:16] That's not graphically aware, so you have to be explicit about what you want. Owner dot what, what do you want? So that's why it's freaking out right now. If I try to run this, actually think Playground will automatically expand this for me and add fields. I think that's what it will do, let's see.

[00:05:31] No, it doesn't. Yes, so you'll tell me field, owner of type owner must have a selection of sub-fields. Did you mean owner...something here? So it's telling me you've got to put something here cuz I don't know what you want from owner. And I can't give you back a whole lot of it.

[00:05:45] So we gotta put something here. In this case, let's just say, name. But because we didn't really write a resolver for that, it gets null, right, cuz we didn't put anything in that object. So it's gonna be null.
>> Scott Moss: So if we go back and we look at our server output, you can see that.

[00:06:05] I'll just clear it, run it one time. I'm just gonna run it once. You can see that it only went into the cat resolver, just one time. It didn't do anything else. Did anybody expect it to do anything else, right? Maybe, yeah, okay, yeah. You've might have expected for me to go to the owners arg cuz it asks for an owner, but remember, this is just a query called owner.

[00:06:27] I didn't run this query. I ran this query, and I asked for a cat which is this entire object, and then I asked for the owner field on this cat, which is just this object. So it went here, and then because there's no name probably on it, it nulled out, all right?

[00:06:43] If I put a exclamation point here, it'll break because I'm asking for a name and it's null, right? So if I restart this.
>> Scott Moss: And I go back. And then I run this, it'll say, no, you can't do it. is non-nullable. You have to have a name there.

[00:07:06] So it's starting to freak out, right? So that's why I didn't do that cuz I knew that was gonna happen.