GraphQL Union Types for Media Library
📖 Scenario: You are building a media library database that stores different types of media items: Books and Movies. Each media type has its own unique fields.To efficiently query these different media types in one request, you will use GraphQL union types.
🎯 Goal: Create a GraphQL schema that defines Book and Movie types, then create a union type called MediaItem that can represent either a Book or a Movie. Finally, define a query mediaLibrary that returns a list of MediaItem.
📋 What You'll Learn
Define a
Book type with fields id (ID!), title (String!), and author (String!).Define a
Movie type with fields id (ID!), title (String!), and director (String!).Create a
union type called MediaItem that includes Book and Movie.Define a
Query type with a field mediaLibrary that returns a list of MediaItem.💡 Why This Matters
🌍 Real World
Union types let you query different types of related data in one request, useful in media libraries, e-commerce, or social networks.
💼 Career
Understanding union types is important for building flexible GraphQL APIs that handle multiple data types efficiently.
Progress0 / 4 steps