This course has been updated! We now recommend you take the Server-Side GraphQL in Node.js course.
Transcript from the "Interfaces Exercise" Lesson
>> Scott Moss: We already talked about resolving the interface and unions. You just need that resolve type property need to fill, so we'll move on to the exercise. So now that you know about the schema's and the resolver we gotta make some changes. Our product model in mongoose is split between three different product types.
[00:00:17] We need to make the product type an interface and then create types for each possible type in our mongoose model. And don't forget to create the resolver for the result type. So what am I talking about? If you head over to the mongoose model, and I talked about this earlier,
>> Scott Moss: A product can be three different types of products. There's literally a property called type, and it's one of these three things. It's a GAMING PC, it's a BIKE or it's a DRONE, right? They all have the exact same fields except for a GAMING_PC has a liquidCooled field that's required.
[00:00:46] A bike product has a bikeType field that's required. A drone has a range field that's required. So they are all very similar, except for one small property that's different. So, instead of having a product be a type which you've already created, you need to change the product to be an interface.
[00:01:01] And then you need to create three new types called BIKE, DRONE, and GAMING_PC that implement that interface. And then you need to resolve the type with the resolve type resolver. So again, check out the lesson four branch, change the product type to an interface, create the Bike type that implements the product, create the GamingPc type that implements the product.
[00:01:24] Create the Drone type that implements the product, then create a resolver for product interface that resolves the type and then just make sure all the tests pass, which they will if you did all that. And remember to get a clue of what to look for in that resolve type function.
[00:01:40] What is the thing, how do you know which type to return? Well remember, every part has a type property. That is one of these things. That's probably something you wanna look at in a resolve type function to figure out, what type of product is this? Well, product.type. What is it?
[00:01:53] Based on that, all you have to do is return the name of the type that you create in here. So GAMING_PC, DRONE or BIKE. So let's go ahead and check out to the branch, get check out.
>> Scott Moss: And just to show you what's happening here, there's a piece of code that's gonna help you I want you to take a look at to convert some things over.
[00:02:20] So if you look at the product resolvers there's going to be a products type matcher on here for you. Basically is what this does is it matches the enum of product types to the enum of product types in the database. So on the left is the enum that's in grab ko, on the right is the enum that's in a database, so this will help you match it, cuz you're gonna get one from graph QL and you gotta see if it's the one from the database.
[00:02:47] So this is what you're gonna use in the resolved type. And just to show you what I'm talking about, if you go to the product graph QL, you can see the enums is GAMING_PC, BIKE, DRONE, they're all capital, but in the database they're actually they're the same. [LAUGH] Wait, I did make them the same.
[00:03:05] Well why did I add that then? What is that for? No I'm sorry. I got it backwards. So you see the enums are GAMING_PC, BIKE, they're all capital. But the types that you're gonna make are not gonna be like this. They're not gonna be called GAMING_PC, all capital.
[00:03:21] They're gonna be like GamingPc. They're gonna be like that. You're gonna make a type like that. So that object is to help you convert the enum to the actual type name. Cuz you're gonna need that when you make your result type. You're gonna need that in here cuz you're gonna get this product that comes from the database that has a enum on it.
[00:03:44] That enum is gonna be this value, and you're gonna need to return this value cuz that's gonna be the name of your type, so that's how you check. That's the only other piece that I have for you, so you don't need anything else as far as the code.
[00:03:56] And then yeah, just create that interface, and create those types. And then get those tests to pass. And also the test command you want to run to actually run these tests are gonna be npm.run test-interfaces or npm-yarn test-interfaces and that should scope out the test and you'll see some broken tests so, go ahead and get those to pass.