Union

Description

Represents an object that could be one of a list of GraphQL object types, but provides for no guaranteed fields between those types. They also differ from interfaces in that object types declare what interfaces they implement, but are not aware of what unions contain them.

Absinthe Macro

union

Examples

In an Absinthe schema, defining the union and mapping results to the associated GraphQL type:

​ @desc ​"​​A search result"​
​ union ​:search_result​ ​do​
​  types [​:person​, ​:business​]
​  resolve_type ​fn​
​  %Person{}, _ ->
​  ​:person​
​  %Business{}, _ ->
​  ​:business​
​  _, _ ->
​  nil
​  ​end​
​ ​end​

Get Craft GraphQL APIs in Elixir with Absinthe now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.