Union Types

GraphCMS provides first class support for GraphQL Union Types.

A Union Type field can return more than one schema object type from your project.

For example, the property field of a Booking type could resolve to a multitude of other GraphCMS types, each with their own unique fields.

{
bookings {
property {
__typename
... on Hotel {
pricePerNight
suite
}
... on House {
rentPerMonth
numberOfBeds
}
... on Flat {
pricePerMonth
numberOfBeds
allowsPets
}
}
}
}

The Union Type fields in that query would resolve to a response similar to the beneath payload. Notice how each Booking has a single property object, but of varying types.

{
"data": {
"bookings": [
{
"property": {
"__typename": "Hotel",
"pricePerNight": 500,
"suite": true
}
},
{
"property": {
"__typename": "House",
"rentPerMonth": 3000,
"numberOfBeds": 3
}
},
{
"property": {
"__typename": "Flat",
"pricePerMonth": 2400,
"numberOfBeds": 2,
"allowsPets": true
}
}
]
}
}

The __typename field is provided to help distinguish which type the object belongs to. This is automatically provided by the GraphCMS schema. You are not required to include this field in your query.

This site uses cookies to provide you with a better user experience. For more information, refer to our Privacy Policy