Enumeration Types

Learn enumeration types and their use in GraphQL.

We'll cover the following

Using enumeration types

A GraphQL enumeration (or enum) is a special type of scalar that has a defined, finite set of possible values. Here are some examples of values that are well represented by enums:

  • Available shirt sizes: S, M, L, and XL.
  • Color components: RED, GREEN, and BLUE.
  • Ordering: ASC and DESC.

Enums are a good choice if the possible values are well-defined, unlikely to change, and are not a pair of values that would be clearly represented by a boolean flag.

Let’s use the ASC and DESC ordering examples for a list of menuItems that we’ll allow users to retrieve in ascending or descending order.

We’ll start by adding our enum type, :sort_order, using the enum and value macros. The enum macro works just like an object, but it defines an enumeration instead of an object. The value macro defines a possible value of the enum. For our use case, :asc and :desc will do in the schema:

Get hands-on with 1200+ tech skills courses.