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.

Similar Reads

What are Enumeration Types in GraphQL?

Enumeration types referred to as enums, are a valuable feature in GraphQL schemas. An enum is a schema in GraphQL that represents a predefined list of possible values. It allows developers to define a specific set of values that a field can have. Enums are useful when a field should only have a limited number of possible values, such as status fields (for examples: “PENDING“, “IN_PROGRESS“, “COMPLETED“) or type fields (for examples: “USER“, “ADMIN“, “GUEST“)....

How to Use Enumeration Types

Thus, we can create an enumeration type in GraphQL by enumerating all the possibilities we wish to permit for a given field. For instance, we may see choices like To Do, In Progress, or Done for a task’s state. Once the “enum” keyword has been used, we place these options inside curly braces....

Integrating Enum Types

We can utilize our defined enum type for various fields in our GraphQL schema. Suppose we want to monitor the status of every task that belongs to the “Task” type in our schema. The “status” field would be created, and we would specify that it could only contain one of the values we listed in our TaskStatus enum....

Querying with Enums

Now, we may include a task’s status in our query when we want to know more about it. When GraphQL displays a status, it will ensure that it corresponds with one of the options we have configured in our enum. Thus, we don’t need to be concerned about receiving strange or unexpected data back....

Integrating Enumeration Types in Fields

To integrate enumeration types into GraphQL schemas, developers define the enum using the enum keyword followed by the enum’s name and its possible values within curly braces. For instance, defining an enum for order status might include values like PENDING, SHIPPED, DELIVERED, and CANCELLED....

Querying Enumeration Fields

Once enumeration types are integrated into the schema, they can be used as field types within other types. For example, in a schema for managing tasks, an enum type TaskStatus might include values such as TODO, IN_PROGRESS, and DONE. These values can then be used as the type for a field representing the status of a task within the Task type....

Why Use Enum Types?

Enum types make our GraphQL API easier to understand and use. They keep things organized and prevent mistakes by limiting the choices users can make. This way, everyone knows what to expect, which is super important when we’re dealing with lots of data and different parts of an app....

Enum Resolvers

In GraphQL implementations, resolvers are functions responsible for fetching the data for a field. While enumeration types themselves do not typically require resolvers, in some cases, custom logic may be necessary to handle how enum values are resolved or processed....

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.”...

Conclusion

Enumeration types in GraphQL offer a powerful way to define a fixed set of values for fields ensuring data integrity and clarity in API interactions. By using enumeration types developers can easily restrict the possible values that can be assigned to a field, making the API easier to understand and use. It enumeration types play a important role in enhancing the readability, predictability and maintainability of GraphQL APIs....

Contact Us