⬡ 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.