type_reference.ex (1529B)
1 defmodule Absinthe.Blueprint.TypeReference do 2 @moduledoc false 3 4 alias __MODULE__ 5 6 @type t :: 7 TypeReference.List.t() 8 | TypeReference.Name.t() 9 | TypeReference.Identifier.t() 10 | TypeReference.NonNull.t() 11 12 @wrappers [TypeReference.List, TypeReference.NonNull] 13 14 @doc """ 15 Unwrap a type reference from surrounding NonNull/List type information. 16 """ 17 @spec unwrap(t) :: t 18 def unwrap(%TypeReference.Name{} = value) do 19 value 20 end 21 22 def unwrap(%TypeReference.Identifier{} = value) do 23 value 24 end 25 26 def unwrap(value) when is_atom(value), do: value 27 28 def unwrap(%struct{of_type: inner}) when struct in @wrappers do 29 unwrap(inner) 30 end 31 32 @doc """ 33 Get the GraphQL name for a (possibly wrapped) type reference. 34 35 """ 36 def name(%__MODULE__.NonNull{of_type: type}) do 37 name(type) <> "!" 38 end 39 40 def name(%__MODULE__.List{of_type: type}) do 41 "[" <> name(type) <> "]" 42 end 43 44 def name(%__MODULE__.Name{name: name}) do 45 name 46 end 47 48 def to_type(%__MODULE__.NonNull{of_type: type}, schema) do 49 %Absinthe.Type.NonNull{of_type: to_type(type, schema)} 50 end 51 52 def to_type(%__MODULE__.List{of_type: type}, schema) do 53 %Absinthe.Type.List{of_type: to_type(type, schema)} 54 end 55 56 def to_type(%__MODULE__.Name{name: name}, schema) do 57 Enum.find(schema.type_definitions, &(&1.name == name)).identifier 58 end 59 60 def to_type(%__MODULE__.Identifier{id: id}, _) when is_atom(id) do 61 id 62 end 63 64 def to_type(value, _) when is_atom(value) do 65 value 66 end 67 end