In GraphQL, the server developer can also use a union.
While an interface defines a set of fields, a union defines a set of other types that could be returned.
For example, if Amazon.com had a GraphQL shopping API, when you search they could return a generic Product
union. But as a concrete type, you could get back a Book
, a Television
, a Grocery
item or something else!
On the server, it could be defined like this:
union Product {
Book | Television | Grocery
}
So unions are similar to interfaces. You query for something, and the type system will return an item of the interface or union type, a bit like an abstract type in object-oriented programming.