What are GraphQL enums and interfaces and how do they differ?
Learn what GraphQL enums and interfaces are, how they differ, and when to use each — with schema examples, inline fragments, and polymorphic query patterns.
Expected Interview Answer
An enum is a scalar-like type restricted to a fixed set of named values (like ACTIVE or BANNED), while an interface is an abstract type that defines a set of fields multiple object types must implement. Enums constrain a single value; interfaces define a shared shape for polymorphic objects.
Enums are used when a field can only be one of a known, small list of options — the schema itself documents and validates the allowed values, preventing invalid strings. Interfaces express 'is-a' relationships: several concrete types (like Book and Movie) implement a common interface (like SearchResult) so a field can return any of them, and clients select shared fields directly plus type-specific fields via inline fragments. The core difference is that an enum describes the allowed values of one leaf field, whereas an interface describes a contract of fields shared across whole object types.
- Enums make illegal states unrepresentable at the schema level
- Enums are self-documenting and safer than free-form strings
- Interfaces enable polymorphic fields that return multiple types
- Interfaces guarantee a shared set of fields across implementers
- Both improve type safety and tooling autocompletion
AI Mentor Explanation
An enum is like the fixed set of ways a batter can be dismissed — bowled, caught, run out, LBW — a closed list the scorer must pick from, never inventing a new one. An interface is like the role 'fielder': every position, whether slip or cover, must implement the shared duties of catching and throwing, though each fields differently in its own spot.
Step-by-Step Explanation
Step 1
Define an enum for closed values
Use 'enum Role { ADMIN MEMBER GUEST }' when a field must be one of a fixed list.
Step 2
Use the enum on a field
Reference it like 'role: Role!' so the server rejects any value outside the set.
Step 3
Define an interface contract
Use 'interface Node { id: ID! }' to declare fields that implementers must provide.
Step 4
Implement the interface
Write 'type User implements Node { id: ID! name: String! }' so User satisfies the contract.
Step 5
Query polymorphic results
Select shared fields directly and use inline fragments (... on User) for type-specific fields.
What Interviewer Expects
- Enum = fixed set of allowed values for one field
- Interface = shared field contract across object types
- Enums constrain a leaf value; interfaces enable polymorphism
- Knows implementers must include all interface fields
- Can use inline fragments to resolve concrete types
Common Mistakes
- Confusing an interface with a union (unions share no fields)
- Thinking an enum can hold arbitrary strings at runtime
- Forgetting that every implementing type must define all interface fields
- Using an enum where an interface's polymorphism is needed
- Omitting inline fragments when querying type-specific fields
Best Answer (HR Friendly)
“An enum limits a field to a short, fixed list of allowed choices, like a status that can only be active, banned, or pending. An interface is a shared blueprint that several data types must follow, so one field can return different kinds of objects that all share some common fields.”
Code Example
enum Role {
ADMIN
MEMBER
GUEST
}
type User {
id: ID!
role: Role!
}interface SearchResult {
id: ID!
title: String!
}
type Book implements SearchResult {
id: ID!
title: String!
author: String!
}
type Movie implements SearchResult {
id: ID!
title: String!
director: String!
}
type Query {
search(term: String!): [SearchResult!]!
}query {
search(term: "matrix") {
id
title
... on Movie { director }
... on Book { author }
}
}Follow-up Questions
- How does an interface differ from a union type in GraphQL?
- How does the server resolve which concrete type an interface value is (__resolveType)?
- Can an object type implement more than one interface?
- How do you add a new value to an enum without breaking clients?
- When would you choose an enum over a set of boolean flags?
MCQ Practice
1. A GraphQL enum is best used when?
Enums restrict a field to a known, fixed list of named values, validated by the schema.
2. What must every type that implements an interface do?
An implementing type must include every field declared by the interface, keeping the shared contract.
3. How do you select a field that exists only on one concrete type of an interface?
Inline fragments like '... on Movie { director }' select fields specific to a concrete implementing type.
Flash Cards
What is a GraphQL enum? — A type restricted to a fixed set of named values, validated by the schema.
What is a GraphQL interface? — An abstract type defining fields that multiple object types must implement.
Key difference between them? — Enum constrains one leaf value; interface defines a shared field contract enabling polymorphism.
How do you query type-specific fields on an interface? — Use inline fragments, e.g. '... on Movie { director }'.