ExShopifySchema.EctoTypes.Union (ExShopifySchema v2025.4.7)
View SourceAn Ecto parameterized type for handling GraphQL union types.
This type allows you to cast, load, and dump union types from GraphQL schemas
where data can be one of several possible struct types. The type uses the
__typename field to determine which specific type to cast the data to.
Usage
field :content, ExShopifySchema.EctoTypes.Union,
types: [MyApp.Post, MyApp.Comment, MyApp.Image]Parameters
:possible_types- A list of modules representing the possible union member types. Each module should implement anew!/1function for casting.
Data Format
The data must include a __typename field (either as atom or string key) that
matches the last segment of one of the configured type module names.
For example, if types are [MyApp.Post, MyApp.Comment], valid __typename
values would be "Post" and "Comment".
Examples
# Casting from map with __typename
%{"__typename" => "Post", "title" => "Hello"} # -> %MyApp.Post{title: "Hello"}
# Dumping struct to map
%MyApp.Post{title: "Hello"} # -> %{__typename: "Post", title: "Hello"}
Summary
Types
@type t() :: struct()