union_type_definition.ex (2631B)
1 defmodule Absinthe.Blueprint.Schema.UnionTypeDefinition do 2 @moduledoc false 3 4 alias Absinthe.{Blueprint, Type} 5 6 @enforce_keys [:name] 7 defstruct [ 8 :identifier, 9 :name, 10 :module, 11 description: nil, 12 resolve_type: nil, 13 fields: [], 14 directives: [], 15 types: [], 16 source_location: nil, 17 # Added by phases 18 flags: %{}, 19 errors: [], 20 __reference__: nil, 21 __private__: [] 22 ] 23 24 @type t :: %__MODULE__{ 25 name: String.t(), 26 description: nil | String.t(), 27 directives: [Blueprint.Directive.t()], 28 types: [Blueprint.TypeReference.Name.t()], 29 source_location: nil | Blueprint.SourceLocation.t(), 30 # Added by phases 31 flags: Blueprint.flags_t(), 32 errors: [Absinthe.Phase.Error.t()] 33 } 34 35 def build(type_def, schema) do 36 %Type.Union{ 37 name: type_def.name, 38 description: type_def.description, 39 identifier: type_def.identifier, 40 types: type_def.types |> atomize_types(schema), 41 fields: build_fields(type_def, schema), 42 definition: type_def.module, 43 resolve_type: type_def.resolve_type 44 } 45 end 46 47 defp atomize_types(types, schema) do 48 types 49 |> Enum.map(&Blueprint.TypeReference.to_type(&1, schema)) 50 |> Enum.sort() 51 end 52 53 def build_fields(type_def, schema) do 54 for field_def <- type_def.fields, into: %{} do 55 field = %Type.Field{ 56 identifier: field_def.identifier, 57 middleware: field_def.middleware, 58 deprecation: field_def.deprecation, 59 description: field_def.description, 60 complexity: field_def.complexity, 61 config: field_def.complexity, 62 triggers: field_def.triggers, 63 name: field_def.name, 64 type: Blueprint.TypeReference.to_type(field_def.type, schema), 65 args: build_args(field_def, schema), 66 definition: field_def.module, 67 __reference__: field_def.__reference__, 68 __private__: field_def.__private__ 69 } 70 71 {field.identifier, field} 72 end 73 end 74 75 def build_args(field_def, schema) do 76 Map.new(field_def.arguments, fn arg_def -> 77 arg = %Type.Argument{ 78 identifier: arg_def.identifier, 79 name: arg_def.name, 80 description: arg_def.description, 81 type: Blueprint.TypeReference.to_type(arg_def.type, schema), 82 default_value: arg_def.default_value, 83 deprecation: arg_def.deprecation 84 } 85 86 {arg_def.identifier, arg} 87 end) 88 end 89 90 @doc false 91 def functions(), do: [:resolve_type] 92 93 defimpl Inspect do 94 defdelegate inspect(term, options), 95 to: Absinthe.Schema.Notation.SDL.Render 96 end 97 end