Example
Consider a scenario where we are developing a GraphQL API for managing tasks. We want to define a type for the status of a task, which can be either “To Do,” “In Progress,” or “Done.”
enum TaskStatus {
TODO
IN_PROGRESS
DONE
}
type Task {
id: ID!
title: String!
description: String!
status: TaskStatus!
}
Explanation: In this example, we’ve declared an enum type TaskStatus with three possible values: TODO, IN_PROGRESS, and DONE. The Task type includes a field status of type TaskStatus, indicating the status of the task.
Now, let’s query for a task and specify the desired status:
query {
task(id: "123") {
id
title
status
}
}
Enumeration Types in GraphQL Schema
In the area of GraphQL schema design, enumeration types play an important role in defining a structured set of values for specific fields. Enumerations serve as a means to restrict the possible options for a field, ensuring data consistency and clarity throughout an API. In this article, we will explore the concept of enumeration types in GraphQL with their importance through practical examples and use cases.
Contact Us