Bird
Raised Fist0
GraphQLquery~10 mins

Enum types in GraphQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Enum types
Define Enum Type
↓
Use Enum in Schema
↓
Client Queries with Enum
↓
Server Validates Enum Value
↓
Return Data or Error
This flow shows how an Enum type is defined, used in schema, queried by client, validated by server, and then data or error is returned.
Execution Sample
GraphQL
enum Role {
  ADMIN
  USER
  GUEST
}

type Query {
  user(id: ID!): User
}

type User {
  role: Role
}

query GetUserRole {
  user(id: 1) {
    role
  }
}
Defines an Enum 'Role' with three values and queries a user's role using that Enum.
Execution Table
StepActionInput/QueryValidationResult
1Define EnumRole: ADMIN, USER, GUESTN/AEnum type 'Role' created with 3 values
2Use Enum in Schemauser.role: RoleN/AField 'role' expects one of Role Enum values
3Client Queryquery GetUserRole { user(id:1) { role } }Check if 'role' value is in EnumValid query sent to server
4Server Resolves RoleUser role = ADMINADMIN is in Role EnumReturns role: ADMIN
5Client Receivesrole: ADMINN/AClient gets role as 'ADMIN'
6Client Query with Invalid Enumquery { user(id:2) { role } }User role = SUPERADMIN (invalid)Error: 'SUPERADMIN' not in Enum Role
7EndN/AN/AExecution stops due to invalid Enum value
💡 Execution stops when an invalid Enum value is encountered during validation.
Variable Tracker
VariableStartAfter Step 4After Step 6Final
Role EnumNot definedDefined with ADMIN, USER, GUESTNo changeDefined with ADMIN, USER, GUEST
user.roleN/AADMIN (valid)SUPERADMIN (invalid)SUPERADMIN (invalid)
Key Moments - 2 Insights
Why does the server reject a role value not listed in the Enum?
Because the Enum type only allows predefined values (see execution_table step 6), any value outside those causes a validation error.
Can the client send any string as an Enum value in the query?
No, the client must send one of the Enum's defined values; otherwise, the server returns an error (execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the role value returned by the server?
AGUEST
BADMIN
CUSER
DSUPERADMIN
💡 Hint
Check the 'Result' column in step 4 of the execution_table.
At which step does the server detect an invalid Enum value?
AStep 6
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where validation fails in the execution_table.
If the Enum 'Role' included 'SUPERADMIN', what would change in the execution_table?
AStep 4 would fail validation
BStep 6 would return an error
CStep 6 would return role SUPERADMIN successfully
DNo change
💡 Hint
Consider how validation depends on Enum values as shown in steps 4 and 6.
Concept Snapshot
Enum types define a fixed set of allowed values.
Use 'enum' keyword to declare.
Fields using Enum accept only these values.
Queries must use valid Enum values.
Server validates and rejects invalid values.
Helps keep data consistent and clear.
Full Transcript
Enum types in GraphQL are special types that list a set of allowed values. First, you define an Enum with the 'enum' keyword and list its values. Then you use this Enum in your schema for fields that should only accept those values. When a client queries such a field, the server checks if the value is one of the Enum's allowed values. If yes, it returns the value; if not, it returns an error. This ensures only valid, expected values are used, making data consistent and easier to understand.

Practice

(1/5)
1. What is the main purpose of using enum types in GraphQL schemas?
easy
A. To allow any string value for a field
B. To create a numeric range for a field
C. To define a list of objects
D. To restrict a field to a fixed set of allowed values

Solution

  1. Step 1: Understand enum purpose

    Enums limit a field to specific predefined values, ensuring data consistency.
  2. 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.
  3. Final Answer:

    To restrict a field to a fixed set of allowed values -> Option D
  4. Quick Check:

    Enum = fixed allowed values [OK]
Hint: Enums fix allowed values, not free text or ranges [OK]
Common Mistakes:
  • Thinking enums allow any string
  • Confusing enums with lists or objects
  • Assuming enums define numeric ranges
2. Which of the following is the correct syntax to define an enum type Status with values ACTIVE and INACTIVE in GraphQL?
easy
A. enum Status: ACTIVE | INACTIVE
B. enum Status { ACTIVE INACTIVE }
C. enum Status = ["ACTIVE", "INACTIVE"]
D. type Status = enum { ACTIVE, INACTIVE }

Solution

  1. Step 1: Recall GraphQL enum syntax

    GraphQL enums are defined using enum Name { VALUE1 VALUE2 } syntax.
  2. Step 2: Check each option

    enum Status { ACTIVE INACTIVE } matches the correct syntax. Others use invalid or mixed syntax.
  3. Final Answer:

    enum Status { ACTIVE INACTIVE } -> Option B
  4. Quick Check:

    Correct enum syntax = enum Status { ACTIVE INACTIVE } [OK]
Hint: Enums use curly braces with values, no equals or brackets [OK]
Common Mistakes:
  • Using equals sign or brackets for enums
  • Confusing enum with type or array syntax
  • Using colon or pipe symbols incorrectly
3. Given the enum definition:
enum Role { ADMIN USER GUEST }

and the query:
{ userRole }

where userRole returns ADMIN, what will be the output type of userRole?
medium
A. A boolean true
B. An integer representing ADMIN
C. "ADMIN" (a string matching enum value)
D. An error because ADMIN is not a string

Solution

  1. Step 1: Understand enum value representation

    GraphQL enums return the enum value as a string matching one of the defined enum names.
  2. Step 2: Analyze the query output

    The query returns ADMIN, which is output as the string "ADMIN".
  3. Final Answer:

    "ADMIN" (a string matching enum value) -> Option C
  4. Quick Check:

    Enum output = string value [OK]
Hint: Enum values output as strings, not numbers or booleans [OK]
Common Mistakes:
  • Expecting numeric or boolean output
  • Thinking enum values cause errors
  • Confusing enum with custom object types
4. Consider this enum definition:
enum Color { RED GREEN BLUE }

Which of the following queries will cause a validation error?
medium
A. { favoriteColor(color: YELLOW) }
B. { favoriteColor(color: GREEN) }
C. { favoriteColor(color: RED) }
D. { favoriteColor(color: BLUE) }

Solution

  1. Step 1: Check enum allowed values

    The enum Color allows only RED, GREEN, and BLUE.
  2. Step 2: Identify invalid value

    { favoriteColor(color: YELLOW) } uses YELLOW, which is not in the enum, causing a validation error.
  3. Final Answer:

    { favoriteColor(color: YELLOW) } -> Option A
  4. Quick Check:

    Invalid enum value = validation error [OK]
Hint: Only use enum values defined exactly, others cause errors [OK]
Common Mistakes:
  • Using values not in enum
  • Ignoring case sensitivity in enum values
  • Assuming enums accept any string
5. You want to add a new enum 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?
hard
A. enum Priority { LOW MEDIUM HIGH } type Query { tasksByPriority(priority: Priority): [Task] }
B. type Priority { LOW MEDIUM HIGH } type Query { tasksByPriority(priority: Priority): [Task] }
C. enum Priority = [LOW, MEDIUM, HIGH] type Query { tasksByPriority(priority: Priority): [Task] }
D. enum Priority { LOW, MEDIUM, HIGH } type Query { tasksByPriority(priority: Priority): Task }

Solution

  1. 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 }.
  2. Step 2: Define query returning list of tasks

    The query returns a list [Task], matching multiple tasks by priority.
  3. 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.
  4. Final Answer:

    enum Priority { LOW MEDIUM HIGH } type Query { tasksByPriority(priority: Priority): [Task] } -> Option A
  5. Quick Check:

    Correct enum + list query = enum Priority { LOW MEDIUM HIGH } type Query { tasksByPriority(priority: Priority): [Task] } [OK]
Hint: Enums use braces without commas; queries return list with [Type] [OK]
Common Mistakes:
  • Using commas inside enum definitions
  • Defining enum as type instead of enum
  • Returning single object instead of list for multiple results
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard