0
0
GraphQLquery~10 mins

Enum types in GraphQL - Step-by-Step Execution

Choose your learning style9 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.