0
0
GraphQLquery~5 mins

Enum types in GraphQL

Choose your learning style9 modes available
Introduction

Enum types let you define a list of fixed values for a field. This helps keep data clean and easy to understand.

When you want a field to only accept a few specific options, like colors or statuses.
When you want to avoid typos or wrong values in your data.
When you want to make your API easier to use by showing allowed values clearly.
When you want to improve validation by limiting possible inputs.
When you want to make your code more readable and maintainable.
Syntax
GraphQL
enum EnumName {
  VALUE1
  VALUE2
  VALUE3
}

Enum names and values are usually uppercase by convention.

Each value in the enum must be unique.

Examples
This enum defines three colors: RED, GREEN, and BLUE.
GraphQL
enum Color {
  RED
  GREEN
  BLUE
}
This enum defines user statuses to control account states.
GraphQL
enum Status {
  ACTIVE
  INACTIVE
  PENDING
}
This enum defines sizes for products or items.
GraphQL
enum Size {
  SMALL
  MEDIUM
  LARGE
  XLARGE
}
Sample Program

This example defines a Role enum with three roles. The Person type uses this enum for the role field. The query asks for a person's name and role.

GraphQL
enum Role {
  ADMIN
  USER
  GUEST
}

type Person {
  name: String!
  role: Role!
}

query {
  person {
    name
    role
  }
}
OutputSuccess
Important Notes

Enum values are case-sensitive and must match exactly when used.

GraphQL enums improve API clarity by showing allowed values in documentation.

Use enums to prevent invalid data and make your API safer.

Summary

Enums define a fixed set of allowed values for a field.

They help keep data clean and easy to understand.

Use enums to improve validation and API clarity.