Enumeration Types
Explore how to implement enumeration types in GraphQL schemas to represent fixed sets of values like sorting orders. Learn to define enums, set default argument values, and handle user inputs to ensure flexible but controlled API queries.
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, andXL. - Color components:
RED,GREEN, andBLUE. - Ordering:
ASCandDESC.
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 ...