An Enum type in GraphQL is a special kind of scalar that restricts a field to a set of predefined constant values. It helps ensure only valid values are used.
Click to reveal answer
beginner
How do you define an Enum type in GraphQL schema?
You define an Enum type using the enum keyword followed by the name and a list of allowed values inside curly braces. For example:
enum Color { RED GREEN BLUE }
Click to reveal answer
intermediate
Why use Enum types instead of strings in GraphQL?
Enums provide a fixed set of allowed values, which helps catch errors early, improves API clarity, and makes client-server communication more predictable compared to free-form strings.
Click to reveal answer
intermediate
Can Enum types have descriptions for each value in GraphQL?
Yes, you can add descriptions to each Enum value using string comments above each value. This helps document the purpose of each option for API users.
Click to reveal answer
intermediate
How does GraphQL handle invalid Enum values in queries?
If a query provides a value not defined in the Enum, GraphQL returns an error and does not execute the query, ensuring only valid Enum values are accepted.
Click to reveal answer
Which keyword is used to define an Enum type in GraphQL?
Atype
Binterface
Cscalar
Denum
✗ Incorrect
The enum keyword is used to define Enum types in GraphQL.
What happens if a query uses a value not in the Enum type?
AGraphQL ignores the value
BGraphQL converts it to a string
CGraphQL returns an error
DGraphQL accepts it anyway
✗ Incorrect
GraphQL returns an error if a value outside the Enum's allowed set is used.
Why are Enum types useful in GraphQL?
AThey restrict values to a fixed set
BThey allow any string value
CThey store large text data
DThey define database tables
✗ Incorrect
Enums restrict values to a fixed set, improving validation and clarity.
Can Enum values have descriptions in GraphQL schema?
ANo, descriptions are not allowed
BYes, using comments above each value
COnly for the whole Enum, not values
DOnly in the client code
✗ Incorrect
You can add descriptions to Enum values using comments above them.
Which of these is a valid Enum definition?
Aenum Status { ACTIVE INACTIVE PENDING }
Benum Status = (ACTIVE, INACTIVE, PENDING)
Ctype Status { ACTIVE, INACTIVE, PENDING }
Dscalar Status { ACTIVE, INACTIVE, PENDING }
✗ Incorrect
Option A uses the correct GraphQL Enum syntax.
Explain what an Enum type is in GraphQL and why it is useful.
Think about how you limit choices to a list of options.
You got /3 concepts.
Describe how to define an Enum type in a GraphQL schema with an example.
Remember the syntax starts with 'enum' followed by the name.
You got /4 concepts.
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
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 D
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
Step 1: Recall GraphQL enum syntax
GraphQL enums are defined using enum 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 B
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
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 returns ADMIN, which is output as the string "ADMIN".
Final Answer:
"ADMIN" (a string matching enum value) -> Option C
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
Step 1: Check enum allowed values
The enum Color allows 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 A
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
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 A
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 unknownstrongwhile earning +1 Grip toward your certificate.