Check out a free preview of the full Server-Side GraphQL in Node.js course
The "Relationships Solution: Adding Fields on Types" Lesson is part of the full, Server-Side GraphQL in Node.js course featured in this preview video. Here's what you'd learn in this lesson:
Scott live codes the first part of the solution to the relationships exercise by first adding new fields to different types.
Transcript from the "Relationships Solution: Adding Fields on Types" Lesson
[00:00:00]
>> Scott Moss: Hoping everyone had some time to wrap their head around that one. I saved the best one for last. That was the last exercise of the server side stuff today. And I wanted that to be, yeah, there's a lot building up to that. But if you can get past that, you pretty much understand graph kill.
[00:00:16]
So what I'm gonna do now, is actually go through and build it. But I'll also explain it a little more in a way that I think what helped make sense of it, because it can be pretty confusing. But so let's do that. So what I'll do is I'll go to the schema here.
[00:00:31]
And first thing is a pet needs a user. So I'm gonna add a field on a pet called user. Okay, I'm going to set its type to a user. I'm gonna say it's required. In fact, I don't like pets having users, I like them having owners. I mean, pets don't have users.
[00:00:49]
[COUGH] They have I have owners. So I'm gonna call it an owner, but still keep it to the user type. And then I'm gonna go to the, actually we're not gonna focus that part right now. I'm gonna keep it very simple. So we're gonna focus on the pet part right now.
[00:01:02]
So, let's just think about this. A pet doesn't actually have an owner associated with it, stored in the database somewhere. It just doesn't exist. I mean, let's go look at our database .Json. There's nothing in here, that says anything about an owner or anything like that. And if it was.
[00:01:27]
>> Scott Moss: It would be at most. It would be an ID of something. It wouldn't be an actual owner object, like you don't store stuff in a database like that, like your database be so denormalized, I don't know how you would even query it. So you can't do that, mostly for references.
[00:01:43]
So let's imagine that there's a reference here, and the reason that you don't see it yet is we haven't created any. We haven't made any with any IDs, but for the sake of this exercise, I can just, I'll just put some in here for now. I have my user, and just put all these IDs in here and that will help us do some associations.
[00:02:02]
You didn't have to manually do this. But, this was going to help me get through the example a lot better, and make sense of it. So, I will put the user ID on all these pets, which is the only user the database. So that is gonna help us.
[00:02:13]
So, now that I have that, what I can do is. Let's go back to the schema. And we know that a pet has an owner and a user. But again if we go. But look at the database. There's no owner field here. So that means we have to virtualize it.
[00:02:28]
That means we have to write a resolver for that field on a pet. Because it's not gonna be automatically resolved for us, because there's no key in our pet object that's called owner. Or in your case called user or whatever you call it, it doesn't exist. So you have to resolve it because if a query from the client came in and asked for it, gravity is gonna break because it doesn't exist, especially if it's not know which I think in our case, I didn't make it not know, it's gonna break.
[00:02:55]
So that's the indication that okay, I need to write a resolver for this, because if it's not automatically gonna be resolved from a data source. Then you have to write a resolver for it. I told you, your your job is a person who's building a graphical server, is making sure all your fields are resolved.
[00:03:10]
That's it. If you make sure all your fields eventually get resolved either automatically because the database connects exactly what the types, or because you wrote manual resolvers, your job is done. You're not to do anything else.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops