input.ex (3107B)
1 defmodule Absinthe.Blueprint.Input do 2 @moduledoc false 3 4 alias Absinthe.Blueprint 5 alias __MODULE__ 6 7 import Kernel, except: [inspect: 1] 8 9 @type leaf :: 10 Input.Integer.t() 11 | Input.Float.t() 12 | Input.Enum.t() 13 | Input.String.t() 14 | Input.Variable.t() 15 | Input.Boolean.t() 16 | Input.Null.t() 17 18 @type collection :: 19 Blueprint.Input.List.t() 20 | Input.Object.t() 21 22 @type t :: leaf | collection | Input.Value.t() | Input.Argument.t() 23 24 @parse_types [ 25 Input.Boolean, 26 Input.Enum, 27 Input.Field, 28 Input.Float, 29 Input.Integer, 30 Input.List, 31 Input.Object, 32 Input.String, 33 Input.Null 34 ] 35 36 @spec parse(any) :: nil | t 37 def parse(%struct{} = value) when struct in @parse_types do 38 value 39 end 40 41 def parse(value) when is_integer(value) do 42 %Input.Integer{value: value} 43 end 44 45 def parse(value) when is_float(value) do 46 %Input.Float{value: value} 47 end 48 49 def parse(value) when is_nil(value) do 50 %Input.Null{} 51 end 52 53 # Note: The value may actually be an Enum value and may 54 # need to be manually converted, based on the schema. 55 def parse(value) when is_binary(value) do 56 %Input.String{value: value} 57 end 58 59 def parse(value) when is_boolean(value) do 60 %Input.Boolean{value: value} 61 end 62 63 def parse(value) when is_list(value) do 64 %Input.List{ 65 items: 66 Enum.map(value, fn item -> 67 %Input.RawValue{content: parse(item)} 68 end) 69 } 70 end 71 72 def parse(value) when is_map(value) do 73 %Input.Object{ 74 fields: 75 Enum.map(value, fn {name, field_value} -> 76 %Input.Field{ 77 name: name, 78 input_value: %Input.RawValue{content: parse(field_value)} 79 } 80 end) 81 } 82 end 83 84 @simple_inspect_types [ 85 Input.Boolean, 86 Input.Float, 87 Input.Integer, 88 Input.String 89 ] 90 91 @spec inspect(t) :: String.t() 92 def inspect(%str{} = node) when str in @simple_inspect_types do 93 Kernel.inspect(node.value) 94 end 95 96 def inspect(%Input.Enum{} = node) do 97 node.value 98 end 99 100 def inspect(%Input.List{} = node) do 101 contents = 102 node.items 103 |> Enum.map(&inspect/1) 104 |> Enum.join(", ") 105 106 "[#{contents}]" 107 end 108 109 def inspect(%Input.Object{} = node) do 110 contents = 111 node.fields 112 |> Enum.filter(fn %{input_value: input} -> 113 case input do 114 %Input.RawValue{content: content} -> content 115 %Input.Value{raw: nil} -> false 116 %Input.Value{raw: %{content: content}} -> content 117 end 118 end) 119 |> Enum.map(&inspect/1) 120 |> Enum.join(", ") 121 122 "{#{contents}}" 123 end 124 125 def inspect(%Input.Field{} = node) do 126 node.name <> ": " <> inspect(node.input_value) 127 end 128 129 def inspect(%Input.Value{raw: raw}) do 130 inspect(raw) 131 end 132 133 def inspect(%Input.RawValue{content: content}) do 134 inspect(content) 135 end 136 137 def inspect(%Input.Variable{} = node) do 138 "$" <> node.name 139 end 140 141 def inspect(%Input.Null{}) do 142 "null" 143 end 144 145 def inspect(nil) do 146 "null" 147 end 148 149 def inspect(other) do 150 Kernel.inspect(other) 151 end 152 end