Schema with Union Types
Explore how to define and use union types in GraphQL schemas to allow queries that return multiple object types. Learn to implement unions with examples like Pizza, ChicagoPizza, and DominoPizza, and write queries using inline fragments to handle varied data. This lesson helps you deepen your knowledge of GraphQL type system flexibility and improves your practical skills in building versatile APIs.
In the previous lesson, we learned how the schema interface in GraphQL could be used to ensure that Pizza always represents a mandatory set of types. Union types are similar to interfaces, but they don’t specify any standard fields between the types.
How to implement union types in GraphQL server
To implement union in the GraphQL server, you need to follow these steps:
- Edit the
typeDefsvariable and add theVariousPizzaasenumwith typesPizza