Server-Side GraphQL in Next.js

Resolving Enums & Unions

Scott Moss

Scott Moss

Superfilter AI
Server-Side GraphQL in Next.js

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

The "Resolving Enums & Unions" Lesson is part of the full, Server-Side GraphQL in Next.js course featured in this preview video. Here's what you'd learn in this lesson:

Scott discusses how to resolve enums, which involves mapping the enum values to different values if needed. Then, he moves on to resolving unions, which requires determining the type of the object being resolved based on its properties. Scott also provides examples and code snippets to demonstrate the process of resolving enums and unions.


Transcript from the "Resolving Enums & Unions" Lesson

>> Scott Moss: This is an interesting one. So Resolving Enums, and then we'll talk about Resolving Unions and Interfaces, and then we'll actually get to that. So resolving enums, for the most part, you almost never need to resolve an enum, but sometimes you do because maybe the enum that you wrote doesn't map directly to the actual value that you wanna use in your UI or maybe to a database.

An app that we're gonna be making they don't map one for one, so we have to resolve it. So if I have an enum here in my schema,
>> Scott Moss: Or let's say enum and I call it colors, right? And I have colors and I say, I have one color that is blue.

I have one color that is purple.
>> Scott Moss: I have another color that is brown. I can have this, and I can use this enum wherever I want. So maybe now I'll say this person has a favorite color, and it's of value colors like that.
>> Scott Moss: But what if I don't want to actually have the values be stored this way?

Or maybe in the database, they actually map to a hexadecimal color and not the actual word color? So if I wanted to remap those, I can just also do that here. I just get, Grab the name of the enum, this case colors. I can go to my resolvers top level, say colors, like this, right?

And then all I have to do is just give it an object with those enums and the values on the right, on which they map to. So if this case, I could say, okay, blue is actually like this hexadecimal color or something. If I would have made a blue one, I would have flipped out.

That would have been crazy. [LAUGH] I don't like, wait, do I know a blue hexadecimal color off the top of my head? Purple, that's pretty close. Whatever, so, you get what I'm saying here. You can do that, you can map it to a different color. So, this is how you would resolve an enum.

So If someone were to ask for enum, they would actually resolve to this value versus the actual enum cuz you couldn't put a hexadecimal thing in your schema, so you can map your enums that way. It's very rare that I see people do that, but we will most likely be doing that.

The more interesting one and useful one is gonna be Resolving Unions. Okay, so this one's not as complicated since you already kind of know how unions work. It's very similar to what you would do on the query side where you do dot dot on, where each one of those is in life fragment which I consider like if statements.

It's pretty much the same thing and your resolver, right?. So for instance, let's go back to our schema, let me get rid of the enum right quick. And we have a person, we also have an animal. And it has a species, and it is a type string. But it also has an ID or also has a name like this.

So I'm gonna do that, and then I'll have a union called searchType and it's gonna be an animal or a person. And then I'll say search query like this, no arguments, just an array of searchType, not null. So I can do that. Now I gotta go write the resolver for the search and then I need to resolve which type I'm actually getting, how do I know something is animal, something is person, right?

So what I'll do, is I'll go to my resolvers. First, I need to make a query for a search,
>> Scott Moss: Like this, search is just gonna return an array of objects, one's gonna be a person. Name is Scott, and what else does a person have? A person has an id, so I'll just give it an id of whatever.

And then there's also gonna be an animal in here, because we can return a searchType, which is also an animal that's have a species and a name. So this will be Darryl is my cat, and it doesn't have id but it has a species. And I'll just say, Big cat, is a big cat.

So his species is Big cat. So now we have that and then what I wanna do is, I need to determine, how do I know when someone does an inline fragment, when they say hey, on a species, give me this on a person, give me this. The resolver needs to know which one's which.

So in order to do that, I can say hey, for that searchType, this is how you resolve them, right? So I can say okay, cool. For a searchType, which is what I called it in my schema, I'm gonna get the actual object here and I can say if object.species, if that's there, which it will be a hundred percent, be there.

If it's an animal, because it's non-null, it has to be there, then return the name of that type, which in this case is called Animal. Otherwise, it's gonna be a person. I mean, technically you should make another if statement for a person, but I'm just gonna assume if it's not an animal, it's a person.

>> Scott Moss: So I can resolve it. I'm sorry, I need to do a resolve type. I'm tripping, so I think you need to do objects here. Yeah, there we go, I'm sorry. Yeah, you need to do objects with __resolve type, and that is the function.
>> Scott Moss: So now that we have that and we have our search, we should be able to search for that and get back those specific types.

So let's go try that, go back to our app and I'm going to say oops
>> Scott Moss: Search, I want the,
>> Scott Moss: On Person, give me the name and id, and then on Animal, give me the name and Species, but it's just freaking out about,
>> Scott Moss: Okay, this is a great example of this.

Let's see if it actually runs before I talk about it. That's great, so what it's saying here is, hey, you have a union. They both have a name property on them, but one of them is required and the other one is not. So this is gonna cost some inconsistencies with the data you get back, so we don't allow it.

So please either rename the field to something else or make them the same. So in this case, I have to just do that in order for our contract to be satisfied. So if I do that, here we go. We have an animal here, and just to confirm, we can say typename.

Don't say typename to run that. This is a person clearly, this is an animal clearly, if we didn't resolve it, we didn't have resolve type. I don't think it's gonna know which ones which. All right, so right here, you have an abstract type called search type. It must resolve to an object type at runtime, we don't know what it resolves cuz you didn't tell us.

So can you resolve it for us, yeah here you go, it's this

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