⬡ GraphQL
Intermediate
What is the difference between union types and interface types in GraphQL?
Answer
Both unions and interfaces represent a field that can return multiple types, but they differ in structure. An interface defines a set of fields that all implementing types must include: interface Node { id: ID! } — every type implementing Node must have an id field. This enables shared querying of common fields. A union is a set of possible types with no required common fields: union SearchResult = User | Post | Product. Use interfaces when types share fields; use unions when types are completely different but can appear in the same position. Both require inline fragments (... on Type) to query type-specific fields.
Previous
How does error handling work in GraphQL?
Next
What is schema stitching versus Apollo Federation in GraphQL?
More GraphQL Questions
View all →- Intermediate What is the N+1 problem in GraphQL and how does DataLoader solve it?
- Intermediate What are persisted queries in GraphQL?
- Intermediate What is the difference between schema-first and code-first GraphQL approaches?
- Intermediate What are the pagination strategies in GraphQL?
- Intermediate How is authentication handled in GraphQL?