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

Scott reviews enums, which are a set of constant values to validate fields on types and inputs.

Get Unlimited Access Now

Transcript from the "Enums" Lesson

[00:00:00]
>> Scott Moss: No, actually, we're gonna hop into enums. So who here knows what a enum is with GraphQL?. Okay, anybody else? Who here knows what a enum in a strict type language? Okay, it's the same thing. It's the exact same thing. It's basically what it is. So me not coming from a background of static type or strict type languages.

[00:00:23] I remember when I first saw the word enum a couple years ago, I was like, what? I don't understand what this is, I just don't get it. And I still didn't really understand what enum was, even after I did Java and Android and stuff. Until I used GraphQL, I was like, okay, I get it.

[00:00:39] Cuz in JavaScript, it doesn't really make sense to use enum. So I was like, I just don't understand what it is. But when I started using this, I got it. Even with typescript I don't get it. But it's really just a constant, right? It's just a constant, different types of causes you can use.

[00:00:56] So, let's make some enum's here. So let's say that a person could have a favorite food, okay, but because we’re on a diet, you can only have one of three favorite foods. You can’t have many favorite foods, it’s only one or three, between one or three. So, what we wanna do is we want to lockdown what favorite foods this person can like, and it has to be one of these options.

[00:01:26] So what we're gonna do is we're gonna make an enum for that. So we'll come up here and we'll say let's make an enum and we'll call it FoodTypes. And we're just gonna say burger, nachos and tacos, did somebody say something? Okay, I thought somebody said a food.

[00:01:53] I'm just hearing food.
>> Speaker 2: [LAUGH]
>> Scott Moss: So then we'll make that faveFood has to be one of the FoodTypes. And then now, because we restricted it to an enum type, when you go and resolve this person, if it doesnt resolve in one of those types, or one of those enums, then GraphQL's gonna freak out.

[00:02:16] So what actually is the value of these things? These are just strings, so basically faveFood needs to return a string of one of these, it has to be one of these. And we can test that out, so let me go to our resolvers. Here, I will put a favorite food, and we'll put something that's not that.

[00:02:37] We'll just put hot dog. Hot dog is not one of the things, so let's try that. And what we'll do is start this.
>> Scott Moss: Will refresh and we'll add faveFood here, and you notice that if we go and look at schema, it should update it where, there we go, faveFood FoodTypes, and will show you all for different options for that so you can verify.

[00:03:03] So now that's there, we can run this. And just like, we got error. It says "Expected a value of type \"FoodTypes\" but received: hotdog". So, can't do that, so let's go back and make sure that it's one of the faveFoods that they can actually select from, so instead of hotdog, we'll put burger and we'll try that.

[00:03:25] And hopefully this will satisfy GraphQL. And it does, so now we got a burger. Sweet, so the sweet thing to remember about enums is that they're one of the rare types in GraphQL. I guess I wouldn't call them type, but one of the rare entities or bits, or whatever you wanna call it, inside of a schema that can work for types and inputs.

[00:03:49] So you can use enums in inputs, you can also use them in types. So I guess they're scholars at that point. So it's like scholars, like String, Integer, ID, Float, Boolean. All those are scholar types, so you can think of enum as a scholar type. They work on types, like Person, and they also work on inputs as well.

[00:04:11] So I could put something that's an enum here. So when you make a Person, you can also give it a favorite food, whose FoodType must be one of those. And that would also work, too. I don't have to make this enum input, that would break, that wouldn't even work.

[00:04:29]
>> Scott Moss: Any questions on enums?
>> Scott Moss: No? Pretty straightforward? So I would say,
>> Scott Moss: The community likes to capitalize these because they're constants. So they'll do something like this. So it's kind of up to you, however you want. But at the end of the day, remember these are string values.

[00:04:50] So, you need to return whatever these enums are saying as you write them in this file. So, just remember that. You can transform them, but eventually they gotta look like that.