Check out a free preview of the full Server-Side GraphQL in Node.js course

The "Relationships Exercise" 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:

Students are instructed to add new fields on types that reference other types and create resolvers for field types that point to other types.

Preview
Close

Transcript from the "Relationships Exercise" Lesson

[00:00:00]
>> Scott Moss: So now you're gonna add new fields on the types, the reference other types, and then create resolvers for those field types that point to types. That was super redundant. But basically what you're gonna be doing is if you go to the schema, a pet should have a user, and a user should have an array of pets.

[00:00:20]
So you need to make that happen. One thing you're gonna have to do is because the user is pretty much hard coded inside the database, you could just copy the ID and paste that whenever you make a pet yourself, or what you can do is if you go into the server file, you can get the user here, and add it to the context.

[00:00:41]
So if you did something like const user = models.user.get. That'll give you the user, and then you can just pass it here on the context object. And now every resolver has the user. That's a quick way to get the user. You're probably gonna need that when you're trying to resolve a user for a pet, basically when you're trying to resolve a user for a pet.

[00:01:03]
pet.user. You're gonna need that, and you can just return that thing. So your goal is to one, go into the schema, add a relationship to users on the pet object, and then add a relationship to pets on the user object. And then you need to write the resolvers for those fields, so that would be pet.user needs a resolver, and then user.pets needs a resolver, and yeah, go ahead and resolve those things.

[00:01:34]
So for the all pets have the same user, so you don't need to filter by give me all the pets for this user, there's only one using the database so you don't need to filter there, you can just get all the pets. Then for the user, there's only one user in the database, and you can use the trigger I showed you with the context, or you can just query the database for models dot, you can do it inside the resolver.

[00:01:54]
Either way, you can do it in the context or you can do it in the resolver, it's up to you, but you gotta write the same code. Cool, any questions on relationships, yes?
>> Speaker 2: How'd you get the user?
>> Scott Moss: Yeah, so I got the user using models dot user dot get.

[00:02:09]
>> Scott Moss: Or. Actually, let's a look. Now I'm second guessing myself. What did I put here? Yeah, it's find one. I standardize it at the last, cool. So find one, that'll give you the user. And then once you do that, it's just some queries and see if you can get your computer to crash.

[00:02:26]
Tell me how many levels you had to go to get it crash. That'll be cool.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now