Packages
absinthe
1.7.11
1.11.0
1.10.2
1.10.1
1.10.0
1.9.1
1.9.0
1.8.0
1.7.11
1.7.10
1.7.9
1.7.8
1.7.7
1.7.6
1.7.5
1.7.4
1.7.3
1.7.2
1.7.1
1.7.0
1.6.8
1.6.7
retired
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.6.0-rc.1
1.6.0-rc.0
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.5.0-rc.5
1.5.0-rc.4
1.5.0-rc.3
1.5.0-rc.2
1.5.0-rc.1
1.5.0-rc.0
1.5.0-beta.2
1.5.0-beta.1
1.5.0-beta.0
1.5.0-alpha.4
1.5.0-alpha.3
1.5.0-alpha.2
1.5.0-alpha.1
1.5.0-alpha.0
1.4.16
1.4.15
1.4.14
1.4.13
1.4.12
1.4.11
1.4.10
1.4.9
1.4.8
retired
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.3
1.4.0-rc.2
1.4.0-rc.1
1.4.0-rc.0
1.4.0-beta.5
1.4.0-beta.4
1.4.0-beta.3
1.4.0-beta.2
1.4.0-beta.1
1.3.2
1.3.1
1.3.0
1.3.0-rc.0
1.3.0-beta.2
1.3.0-beta.1
1.3.0-beta.0
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.2.0-rc.0
1.2.0-beta.0
1.2.0-alpha0
1.2.0-alpha.2
1.2.0-alpha.1
1.1.11
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
0.5.2
0.5.1
0.5.0
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.2.3
0.2.2
0.2.1
0.1.0
GraphQL for Elixir
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/absinthe/type/union.ex
defmodule Absinthe.Type.Union do
@moduledoc """
A unions is an abstract type made up of multiple possible concrete types.
No common fields are declared in a union. Compare to `Absinthe.Type.Interface`.
Because it's necessary for the union to determine the concrete type of a
resolved object, you must either:
* Provide a `:resolve_type` function on the union
* Provide a `:is_type_of` function on each possible concrete type
```
union :search_result do
description "A search result"
types [:person, :business]
resolve_type fn
%Person{}, _ -> :person
%Business{}, _ -> :business
end
end
```
"""
use Absinthe.Introspection.TypeKind, :union
alias Absinthe.{Schema, Type}
@typedoc """
* `:name` - The name of the union type. Should be a TitleCased `binary`. Set automatically.
* `:description` - A nice description for introspection.
* `:types` - The list of possible types.
* `:resolve_type` - A function used to determine the concrete type of a resolved object. See also `Absinthe.Type.Object`'s `:is_type_of`. Either `resolve_type` is specified in the union type, or every object type in the union must specify `is_type_of`
The `:resolve_type` function will be passed two arguments; the object whose type needs to be identified, and the `Absinthe.Resolution` struct providing the full resolution context.
The `__private__` and `:__reference__` keys are for internal use.
"""
@type t :: %__MODULE__{
name: binary,
description: binary,
types: [Type.identifier_t()],
identifier: atom,
fields: map,
__private__: Keyword.t(),
definition: module,
__reference__: Type.Reference.t()
}
defstruct name: nil,
description: nil,
identifier: nil,
resolve_type: nil,
types: [],
fields: nil,
__private__: [],
definition: nil,
__reference__: nil
@doc false
defdelegate functions, to: Absinthe.Blueprint.Schema.UnionTypeDefinition
@doc false
@spec member?(t, Type.t()) :: boolean
def member?(%{types: types}, %{identifier: ident}) do
ident in types
end
def member?(_, _) do
false
end
@doc false
@spec resolve_type(t, any, Absinthe.Resolution.t()) :: Type.t() | nil
def resolve_type(type, object, env, opts \\ [lookup: true])
def resolve_type(%{types: types} = union, obj, %{schema: schema} = env, opts) do
if resolver = Type.function(union, :resolve_type) do
case resolver.(obj, env) do
nil ->
nil
ident when is_atom(ident) ->
if opts[:lookup] do
Absinthe.Schema.lookup_type(schema, ident)
else
ident
end
end
else
type_name =
Enum.find(types, fn
%{is_type_of: nil} ->
false
type ->
type = Absinthe.Schema.lookup_type(schema, type)
Absinthe.Type.function(type, :is_type_of).(obj)
end)
if opts[:lookup] do
Schema.lookup_type(schema, type_name)
else
type_name
end
end
end
end