ExShopifySchema.EctoTypes.Interface (ExShopifySchema v2026.4.7)

View Source

An Ecto parameterized type for handling GraphQL interface types.

This type allows you to cast, load, and dump interface types from GraphQL schemas where data can be one of several possible implementing types. The type uses the __typename field to determine which specific implementing type to cast the data to, and falls back to the interface type's schema when __typename is not provided.

Usage

field :node, ExShopifySchema.EctoTypes.Interface,
  interface_type: MyApp.Node,
  implements: [MyApp.Product, MyApp.Customer, MyApp.Order]

Parameters

  • :interface_type - The interface module that defines the interface schema and can handle casting
  • :implements - A list of modules representing the possible implementing types. Each module should implement a new!/1 function for casting.

Data Format

The data can either include a __typename field (either as atom or string key) that matches the last segment of one of the configured implementing type module names, or it can be cast directly using the interface type's schema.

For example, if implements are [MyApp.Product, MyApp.Customer], valid __typename values would be "Product" and "Customer".

Examples

# Casting from map with __typename
%{"__typename" => "Product", "title" => "Widget"} # -> %MyApp.Product{title: "Widget"}

# Casting from map without __typename (uses interface type)
%{"id" => "123", "title" => "Widget"} # -> %MyApp.Node{id: "123"}

# Dumping struct to map
%MyApp.Product{title: "Widget"} # -> %{__typename: "Product", title: "Widget"}

# Dumping struct to map (from interface type)
%MyApp.Node{id: "123"} # -> %{title: "Widget"}

Summary

Types

t()

@type t() :: struct()