Problem
You have relationships between similar types but cannot use Unions. Fauna does not currently support user-defined Union types in GraphQL, but you still need to
- query for all entities of a given type,
- query for all entities with certain properties, regardless of their type
- query for all relationships of an entity, regardless of which type it points to
Solutions
Perhaps you have a schema in mind like the following, where users can collect their favorite movies, shows, and books. In this series of articles, we will provide example implementations to achieve your goals without user-defined Union types.
# Desired schema, but not valid for Fauna
type User {
name: String!
favorites: [MediaItem!]!
# ...
}
union MediaItem = Movie | Show | Book;
type Movie {
title: String!
# ...movie fields
}
type Show {
title: String!
# ...TV show fields
}
type Book {
title: String!
# ...book fields
}
In the series:
- Composable Types without Unions in GraphQL (1 of 4)
- Modeling Unions in GraphQL (2 of 4)
- Modeling Unions as Sparse fields in GraphQL (3 of 4)
- A "Component" Pattern for GraphQL (4 of 4)
The first two are simpler patterns that are easy to set up and maintain.
The third example of the component pattern is a more advanced example. It has the greatest flexibility but is more work to set up and maintain.