What if a tiny typo in a role name could break your whole app without you noticing?
Why Enum types in GraphQL? - Purpose & Use Cases
Imagine you have a list of user roles like 'admin', 'editor', and 'viewer' scattered as plain text in many places in your database and code.
Every time you want to check or assign a role, you type the role name manually.
This manual way is slow and risky because you might mistype a role name like 'admn' or 'editr'.
Such typos cause bugs that are hard to find and fix.
Also, adding or changing roles means hunting down every place where the role name appears.
Enum types let you define a fixed set of allowed values for a field, like user roles.
This means you can only use the predefined roles, preventing typos and mistakes.
It also makes your code and database clearer and easier to maintain.
role = 'admin' # manual string assignment
enum Role { ADMIN EDITOR VIEWER } role = Role.ADMINEnums make your data consistent and your code safer by allowing only valid, predefined values.
In a content management system, enum types ensure that user roles like 'admin', 'editor', and 'viewer' are always spelled correctly and used consistently everywhere.
Manual text values cause errors and are hard to maintain.
Enum types restrict values to a fixed set, preventing mistakes.
Using enums improves code clarity and data consistency.
Practice
enum types in GraphQL schemas?Solution
Step 1: Understand enum purpose
Enums limit a field to specific predefined values, ensuring data consistency.Step 2: Compare options
Only To restrict a field to a fixed set of allowed values describes restricting values to a fixed set, which is the enum's role.Final Answer:
To restrict a field to a fixed set of allowed values -> Option DQuick Check:
Enum = fixed allowed values [OK]
- Thinking enums allow any string
- Confusing enums with lists or objects
- Assuming enums define numeric ranges
Status with values ACTIVE and INACTIVE in GraphQL?Solution
Step 1: Recall GraphQL enum syntax
GraphQL enums are defined usingenum Name { VALUE1 VALUE2 }syntax.Step 2: Check each option
enum Status { ACTIVE INACTIVE } matches the correct syntax. Others use invalid or mixed syntax.Final Answer:
enum Status { ACTIVE INACTIVE } -> Option BQuick Check:
Correct enum syntax = enum Status { ACTIVE INACTIVE } [OK]
- Using equals sign or brackets for enums
- Confusing enum with type or array syntax
- Using colon or pipe symbols incorrectly
enum Role { ADMIN USER GUEST }and the query:
{ userRole }where
userRole returns ADMIN, what will be the output type of userRole?Solution
Step 1: Understand enum value representation
GraphQL enums return the enum value as a string matching one of the defined enum names.Step 2: Analyze the query output
The query returnsADMIN, which is output as the string "ADMIN".Final Answer:
"ADMIN" (a string matching enum value) -> Option CQuick Check:
Enum output = string value [OK]
- Expecting numeric or boolean output
- Thinking enum values cause errors
- Confusing enum with custom object types
enum Color { RED GREEN BLUE }Which of the following queries will cause a validation error?
Solution
Step 1: Check enum allowed values
The enumColorallows only RED, GREEN, and BLUE.Step 2: Identify invalid value
{ favoriteColor(color: YELLOW) } uses YELLOW, which is not in the enum, causing a validation error.Final Answer:
{ favoriteColor(color: YELLOW) } -> Option AQuick Check:
Invalid enum value = validation error [OK]
- Using values not in enum
- Ignoring case sensitivity in enum values
- Assuming enums accept any string
Priority with values LOW, MEDIUM, and HIGH to a GraphQL schema. You also want to ensure that a query tasksByPriority(priority: Priority) returns only tasks matching the selected priority. Which of the following schema snippets correctly defines the enum and query?Solution
Step 1: Define enum correctly
enum Priority { LOW MEDIUM HIGH } type Query { tasksByPriority(priority: Priority): [Task] } uses correct enum syntax without commas:enum Priority { LOW MEDIUM HIGH }.Step 2: Define query returning list of tasks
The query returns a list[Task], matching multiple tasks by priority.Step 3: Check other options
type Priority { LOW MEDIUM HIGH } type Query { tasksByPriority(priority: Priority): [Task] } wrongly defines Priority as a type, not enum. enum Priority = [LOW, MEDIUM, HIGH] type Query { tasksByPriority(priority: Priority): [Task] } uses invalid enum syntax with equals and brackets. enum Priority { LOW, MEDIUM, HIGH } type Query { tasksByPriority(priority: Priority): Task } uses commas in enum and returns a single Task, not list.Final Answer:
enum Priority { LOW MEDIUM HIGH } type Query { tasksByPriority(priority: Priority): [Task] } -> Option AQuick Check:
Correct enum + list query = enum Priority { LOW MEDIUM HIGH } type Query { tasksByPriority(priority: Priority): [Task] } [OK]
- Using commas inside enum definitions
- Defining enum as type instead of enum
- Returning single object instead of list for multiple results
Ready to prove it?
Pass the test and this pattern moves unknown
