This course has been updated! We now recommend you take the Server-Side GraphQL in Node.js course.
Transcript from the "Field Type Resolvers" Lesson
[00:00:00]
>> Scott Moss: So now that we have the owner there, and then the same thing would happen with the cat. So now that we have the base setup, now we're gonna start linking to see in what order did they start delegating to each other, okay? So because I can't create a circle dependency here hard coded, I need to make this dynamic.
[00:00:14] So what I'm gonna do is, now, this is where writing resolvers for the type is gonna come in handy. So instead of resolving the entire cat object in the cat resolver or the entire owner object in owner resolver. I could have those objects be built up by resolving all of their fields independently and then graph key will build the object for me.
[00:00:40] Now, that sounds really crazy but let me know you what I'm talking about. So what I'm going to do instead is, for the cat query, I'm just going to return an empty object. Okay? Then for the owner query, I'm also just going to return an empty object. So if you remember what I said awhile ago, resolvers have to return their types or they have to delegate to another resolver that returns the type.
[00:01:03] We're about to delegate to other resolvers to return the type. Cuz I can't hard code a circular dependency here and I don't have a database. So instead what I'm gonna do is I'm gonna write resolvers for the fields of cat and owner and it will always be the same.
[00:01:15] That's what I'm going to do. Now if I want to write resolvers for fields on a type, first I put the types name, in this case it's going to be Cat with a capital C because that is what it's called up here, Cat. I'm going to write some resolvers for Cat with a capital C and I want to write resolvers for the owner, name, and age.
[00:01:35] So that's what I'm gonna do. So I'm gonna say, let's just do name and age right quick. So for name, it's the same thing, it's the same API as the resolvers for the query. It's the same thing. It takes four arguments, a first object, arguments, context, and info.
[00:01:50] It takes the exact same thing. The only difference is because this one's gonna be called second. If we look at the order of execution, we're gonna call it Query cat first which is gonna then return an object and that object is gonna be treated like a cat type.
[00:02:05] So then GraphQL is gonna see that our register resolvers for the cat type. So then, it's gonna go resolve those fields. So that means whatever, what was returned here, is gonna be passed here as the first argument. So in this case, it's an empty object. There's an empty object here, so that'd be the first argument here, yes?
[00:02:20]
>> Speaker 2: [INAUDIBLE] resolver for cat with lowercase.
>> Scott Moss: Yes.
>> Speaker 2: How does it know to look for Cat uppercase?
>> Scott Moss: Yep. So let's go back up. So the resolver for Cat for lowercase two things to note here. One, it is lowercase. But two what's more importantly is that it's a query.
[00:02:39] So if we go look here,
>> Speaker 2: Yeah.
>> Scott Moss: Cat is a query whereas cat is a type. Right, and so you can think of the type cat, the type owner, and the type query, they're siblings, they're all the same level. That's why we have them like this. So you got query right here and as a sibling we've got the cat type.
[00:02:57] But these operators here these are just fields. So again, this object that I return right here is going to be the first object that's here. Because it doesn't do anything I don't really need it. But just to know, that's what that object is, so if I return, if I put something here, you'll be able see this first object here.
[00:03:18] Okay, so I'm not gonna do anything here. And then the second argument's gonna be args.namehas, which it doesn't. Any context that it has, which we're not using any, so I'm not gonna even supply any of that stuff. I'm not gonna supply anything, and I'm just going to return a name for our cat.
[00:03:30] We'll call it, my cat's name is actually Daryl, so I'm going to put Daryl here. So now we got a name for the cat. And then we'll do the same thing for age. For age we'll resolve to two. Right, pretty simple. And then what we'll do is make resolvers for owner.
[00:03:50] So for owner, we'll come in here and we'll say, okay. So for owner, we wanna go ahead and resolve the name field as well, and we'll always have the same name as me, Scott. So we got that.