Introduction to GraphQL Nested Resolvers
This course has been updated! We now recommend you take the Server-Side GraphQL in Node.js course.
Transcript from the "Nested Resolvers" Lesson
>> Scott Moss: And the only thing we're missing now is that a cat also has an owner and an owner has a cat. So, how do we resolve those? Well we're gonna delegate to the resolution of the types for them. So how do we do that? Well kind of the same things, so for the owner what we'll do is we'll say, we'll just return an empty object like this.
>> Scott Moss: And I'll tell you why this should work and then for the cat, we'll also just returning an empty object. Reason this should work is because we know that owner.cat is a cat type, so when this values returned, graph field's gonna say, cool, this is a cat type.
[00:00:42] I didn't check it yet, but I'm sure it's a cat type, but you've already got some stuff registered for the cat type, so let me resolve those fields. So this is going to send these back for each one of those fields and it's going to keep going if I keep asking for it.
[00:00:54] So I'm gonna put some logs in here because I see a lot of confused faces because this part is really confusing, but logging definitely helps. So we'll say log in cat query and then we'll come in here and say, in cat name, oops.
>> Scott Moss: In cat age.
>> Scott Moss: And then, in cat owner.
[00:01:25] So we'll do that and then we'll come down here for owner and we'll say, in owner name, in owner cat.
>> Scott Moss: So again these revolvers have no idea how they are going to be queried, they have no idea they're just resolving a type. A name knows it has to return a string so it returned a string, an age knows it has to return a number it returns a number, a cat knows it has to return a name, age, owner so it returned that.
[00:01:57] That's it they have no idea how they're gonna be queried because it doesn't know. The query determines what is not happening to it until it actually comes to the server right so we're gonna write that query down. So we'll refresh it, so now let's just do, without the owner, let's just see what happens.
[00:02:13] So if I just do name, you can see I get Daryl. So if we go look at the logs for the server, you can see it fired off in cat query first, and then it fired off in cat name, so that means I ran the cat query resolver in cat query.
[00:02:30] It returned this blank object and because this blank object returns this query, cat returns a cat type. It then looked up to see that I actually had resolvers for the cat type, it was like you asked for a name and there is a resolver for it so let me run that function.
[00:02:48] So it ran this function because I asked for it, if I didn't ask for it it wouldn't run this function. If I go back and instead, as you can see the age function didn't run. We didn't see in cat age anywhere because I didn't ask for an age, if I'd asked for an age it would run this function.
[00:03:05] The resolvers only gets triggered when the query says so. All right, so if I go back and I add age, and I run this query, I get two and you can see that it says it's in cat age now because I asked for it so it actually ran this resolver and it actually logged it in there.