Advanced GraphQL Advanced GraphQL

Interfaces & Unions Exercise

Check out a free preview of the full Advanced GraphQL course:
The "Interfaces & Unions Exercise" Lesson is part of the full, Advanced GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

In this exercise, students use fragments, unions, and interfaces to clean up repetitive queries and schema.

Get Unlimited Access Now

Transcript from the "Interfaces & Unions Exercise" Lesson

[00:00:00]
>> Scott Moss: Well, let's hop into our exercise which is going to be,
>> Scott Moss: What's this?, lesson 3, yeah, so let's check out lesson-3. And in this lesson, you're gonna be making unions and interfaces and you're gonna have to resolve them. So first things first, if you look in the task resolvers file, there's a resolve type with a comment in it.

[00:00:36] That's asking you to resolve this task and that's because if you look in a task GraphQL file, there is a type here called Task. But there are instructions above and that says there are two types of tasks indeed, cuz there is a type property on them. One is called DevTask, the other one is called DesignTask.

[00:00:59] You need to separate this test type into an interface and then have the dev task and the design task implement that interface. And here are the specific fields for each other tasks or each other type really. So for a dev task type it has a Repo field For a design task type that has an assetUrl field, but they both implement all of these similar fields from the task interface.

[00:01:26] So you need to convert this task type into a task interface, create two new types that implement these similar fields but also implement their own individual fields. One is called devTask, the other one is called designTask and then back into the task resolvers, you need to resolve that type.

[00:01:43] And you could tell GraphQL what type it is, by using this resolveType function. Hint, there's a type property on the task, so look at that and if you wanna know what those types are, it's pointing to an enum that says task type. Look at that, one says design and one says dev, so let's take a look at that, so that's one.

[00:02:03] The other thing you'll need to do is, there is now a search folder, if you're looking at search folder in the graphQL file, it's asking you to do something in here. It's saying you must create a union or interface here, think about it.
>> Scott Moss: Think about it, make sure to name it search result.

[00:02:27] Because I have a query down here called search that's expecting to return an array of the union or interface that you create call search result.
>> Scott Moss: And if you're in that same folder, the search folder, there's also a search resolver. You can see, it's asking you to resolve the type of that interface or union that you created.

[00:02:48] So you should look at that and know what to return. So a SearchResult should be able to return
>> Scott Moss: A project, a task, it can return either one of those, Sso you need to determine, on what field do you wanna look at to return that? And also remember, a task is now two different types now, cuz you're writing interface for that.

[00:03:15] So really, a search result is returning a project, a dev task and a design task. It’s actually the exact same example that I just showed you with the people, the cat and the dog. Where the cat and the dog were both animals on an interface and the person was separate type, it’s the exact same example.

[00:03:32] So that’s what you’ll be doing here.
>> Scott Moss: Any questions on that? So again, you're gonna be writing the unions or interfaces in the GraphQL files and then the appropriate resolver files, you'll be writing the function to resolve those types. Everything else is pretty much wired up, shouldn't have to touch anything else.