Enums

Beginner · 1 min

Define a fixed set of allowed values with a GraphQL enum, see how names differ from internal values, how enums are validated, and how to deprecate a value.

Before thisScalar types and custom scalarsNon-null and lists

My Digital Diaries (fka My Sitecore Diaries) · 4 min · from 14:30

Enums

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by My Digital Diaries (fka My Sitecore Diaries)Report a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An enum is a type with a fixed set of named values, written in SCREAMING_SNAKE_CASE by convention, such as enum Genre { FICTION SCIENCE }.
  • In a query an enum value is written bare (FICTION), not quoted; passing a string literal like "FICTION" is a validation error.
  • On the wire an enum is the value's name as a JSON string; inside your resolver it can map to any internal value, such as the lowercase string fic.
  • A resolver that returns something that is not a value of the enum fails at that field and the field becomes null.
  • Enum values can be deprecated with a reason, and introspection exposes them only when asked with includeDeprecated.

A closed set

Some fields only make sense with a handful of possible values: an order status, a sort direction, a book’s genre. You could use a String, but then the schema promises nothing. Any string is acceptable, a typo goes through, and the documentation lives in someone’s head.

An enum is a scalar-like type with a fixed list of allowed values:

enum Genre {
  FICTION
  SCIENCE
  HISTORY
}

type Query {
  books(genre: Genre, limit: Int = 10): [Book!]!
}

The schema now says exactly what genre can be, GraphiQL autocompletes it, and the server rejects anything else before your code runs. Convention is SCREAMING_SNAKE_CASE for the values.

Writing an enum in a query

An enum value is written as a bare name, not a string:

{ books(genre: SCIENCE) { title } }

Run on graphql-js 17.1.0 against a small bookshelf, that returns only Cosmos. Now the two mistakes people make, both tested:

{ list(genre: "FICTION") }
# Enum "Genre" cannot represent non-enum value: "FICTION". Did you mean the enum value "FICTION"?

{ list(genre: fiction) }
# Value "fiction" does not exist in "Genre" enum. Did you mean the enum value "FICTION"?

The first message is odd. It suggests the very word you wrote, because the problem is the quotes. Case counts too. If the value comes from a variable, the variable JSON holds a string: {"g": "SCIENCE"} works for $g: Genre, because variables carry the name as JSON text. (A JSON "sci", the internal value, is rejected for not being a name.)

Names are not internal values

On the wire an enum is a name. Inside the server it can be anything. Building the schema with constructors makes that visible:

import { GraphQLEnumType } from 'graphql'

const Genre = new GraphQLEnumType({
  name: 'Genre',
  values: {
    FICTION: { value: 'fic' },
    SCIENCE: { value: 'sci' },
    OLD: { value: 'old', deprecationReason: 'Use FICTION.' },
  },
})

With a field list(genre: Genre = SCIENCE), a resolver that echoes its argument got:

Query Resolver saw Client got
{ list(genre: FICTION) } 'fic' "internal value: fic"
{ list } 'sci' (default) "internal value: sci"

A field g: Genre whose resolver returns 'fic' comes out as "FICTION". This mapping is useful when your database stores fic or 2 and you want clients to see words. If you use buildSchema and SDL alone with no mapping, the internal value is the name itself, and a resolver returning 'fiction' (lowercase) fails:

{ g } -> Enum "Genre" cannot represent value: "fiction"   (data: {"g": null})

That is the commonest enum bug in server code: a database value that does not match a defined name. The error lands on that field, and the field becomes null; if it was Genre!, the null propagates up as described in the previous lesson.

In SDL-first servers such as Yoga with createSchema, you map in the resolvers object (Genre: { FICTION: 'fic', ... }). Check your server’s docs for the exact shape; I verified the mapping above with graphql-js constructors only.

Deprecating a value

Enums grow. Removing a value can break a client that still sends it, so SDL lets you deprecate it first:

enum Genre {
  FICTION
  OLD @deprecated(reason: "Use FICTION.")
}

The value still works. Introspection hides deprecated values unless asked:

{ __type(name: "Genre") { enumValues(includeDeprecated: true) { name isDeprecated deprecationReason } } }

which returned OLD with isDeprecated: true and reason “Use FICTION.” (without includeDeprecated it would not appear in the list). The directives lesson covers @deprecated more broadly.

Good enum design

  • Use an enum only for a set that is small and stable. A list of countries or product SKUs changes too often. For sets like that, use a plain string or an object type.
  • Clients should handle an unknown value gracefully. Adding HISTORY to a schema is a non-breaking change on the server and can still break a client with a strict switch. Give a default branch.
  • Use enums as arguments for options like sort order. orderBy: PRICE_ASC is clearer than a free string.

What the video covers

This is a clip from My Digital Diaries’ “GraphQL Schema Explained” (EP03, November 2026). The embed runs 14:30 to 18:27, the chapter labelled “Enums and Lists” in its description, so it also touches lists. I judged it from the chapter list and description alone. The runner-up is a ten-minute video dedicated to enums from the Install Skill channel (2023, part of a numbered course); I could not check its quality.

Where this fits

Enums fix a type to a few values. The next two lessons are about types that can stand for several different object types at once. Next: interfaces.

Check yourself

  1. 01Why does { list(genre: "FICTION") } fail even though FICTION is a valid value?

    Inside a query an enum value is a bare name, not a string, so a quoted literal is rejected with Enum cannot represent non-enum value.

  2. 02How can a resolver receive 'sci' when the client sent SCIENCE?

    The enum definition maps each name to an internal value, and the executor converts the name to that value on input and back to the name on output.

  3. 03What happens if a resolver returns a value that is not in the enum?

    The field fails with a message like Enum cannot represent value and becomes null, with the error reported in the response.

  4. 04How do you retire an enum value without breaking old clients?

    Mark it with a deprecation reason, keep accepting it, and remove it after clients stop using it.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark