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

The "Enums" 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 explains that enums are a set of discrete values that allow limiting a field to a few different options, and demonstrates how to add an enum to a type.

Preview
Close

Transcript from the "Enums" Lesson

[00:00:00]
>> Scott Moss: Cool, so advanced SDL, so advanced Schema Definition Language, let's talk about a few things. So someone, I think it was you, Seth, earlier mentioned something about enums. Cool, so now we're gonna talk about enums. So basically, enums are just like any other language that uses enums. It's a set of discrete values that can be used in place of scalars.

[00:00:18]
So an enum field must resolve to one of the values in the enum. Great for limiting a field to only a few different options. Basically, an enum is saying, here's some possible values that you can have, and I'm gonna add that in place of a scalar. So wherever you put a scalar, you can put an enum.

[00:00:33]
So that means on a type field, on an input type field, yeah, those are the only place you can put scalars. You can replace that with an enum, which is saying one of these things and that's basically what it is. So I'll just give you an example of what I'm talking about.

[00:00:47]
If I go to our schema, and I say, cool, I wanna make an enum. So I'll say, enum. Actually, I should go to demo, do it here. If I go to our schema and I say I wanna make an enum, you basically use the keyword enum, like this.

[00:01:02]
You give it a name, I'm gonna say this one is a ShoeType. Select an object thing here. And then usually the convention is people just put like all caps names here. So I might say something like JORDAN or NIKE. I don't know why that lowercased it for me.

[00:01:19]
ADIDAS, all right, so these are my enums. By default, these enums resolve to strings with the same value. So the response from this in the API is literally gonna be a string with this exact same value, with the same capitalization. It's the same thing. You can write custom resolvers for enums.

[00:01:40]
That's the sweet thing about GraphQL, you can literally write a custom resolver for anything. Not just fields, on queries and mutations and time split for enums. If you get to it, directives, scalars, you can literally write a resolver for pretty much anything you want. It's just by default, it's gonna be whatever you put here wrapped in a string.

[00:01:58]
So now I can go in the place of, for instance, Shoe, instead of putting brand I can put ShoeType, like that. So now brand is a ShoeType, which means it's gonna be one of these. And then on my input, I can also do the same thing. You are also gonna be a ShoeType.

[00:02:14]
And you are also gonna be a ShoeType, like that. And do I have anything else that has that? Nope, so if I were to run this,
>> Scott Moss: And we were to look at the docs here, you can see Shoe takes, let's just find, let's just find, Shoe, there we go.

[00:02:40]
So Shoe is a type, it takes a brand called ShoeType. If I click on ShoeType, you can see it's one of these things, right? So if I try to create a shoe here, let's see what happens. So I say, I need to type that's not a fragment, mutation, newShoe.

[00:02:56]
It takes an input of a size, whatever. And then if I put brand, you can see I get an autocomplete of the enums right here. I'm not able to, if I try to pass something that's not that, it's not gonna like it. So Expected type, ShoeType non-null, found not that.

[00:03:20]
It should have been a ShoeType, which is basically the enum that I created. So to pass the enum, I don't have to wrap it in a string. I can just type in the actual enum, one of the enum values, which in this case is one of these. So I wouldn't put ShoeType in there, I would put one of the values of ShoeType as the actual value.

[00:03:40]
>> Speaker 2: Spaces allowed in those? Or do you have to use a resolver for that?
>> Scott Moss: Spaces are not allowed in this. These follow the same rules as naming types basically, yeah. I don't think spaces are allowed anywhere in a name in a GraphQL schema, not that I can think of.

[00:03:57]
Unless you're doing something like comments which, by the way, comments are just using hashtags. That's a comment, if you didn't know that. You can also do documents like this. So if you do triple quotes like that, you can do things here that show up in tools. So basically, yeah, whatever you put there is commenting on the thing beneath it and it'll show it.

[00:04:26]
Okay, [INAUDIBLE], it'll show up in your tools. So where did I put that? ShoeType, do things here that show up in tools. So those are just like descriptions or things you wanna do to annotate stuff, it's really cool. But, yeah, you can't put spaces anywhere else other than that.

[00:04:46]
But now if I put JORDAN there, then boom, that works, right? And if I say brand, you'll see that it's just a string, the enum wrapped in a string, unless I resolve it differently.

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