data.ex (2734B)
1 defmodule Absinthe.Phase.Document.Arguments.Data do 2 @moduledoc false 3 4 # Populate all arguments in the document with their provided data values: 5 # 6 # - If valid data is available for an argument, set the `Argument.t`'s 7 # `data_value` field to that value. 8 # - If no valid data is available for an argument, set the `Argument.t`'s 9 # `data_value` to `nil`. 10 # - When determining the value of the argument, mark any invalid nodes 11 # in the `Argument.t`'s `normalized_value` tree with `:invalid` and a 12 # reason. 13 # - If non-null arguments are not provided (eg, a `Argument.t` is missing 14 # from `normalized_value`), add a stub `Argument.t` and flag it as 15 # `:invalid` and `:missing`. 16 # - If non-null input fields are not provided (eg, an `Input.Field.t` is 17 # missing from `normalized_value`), add a stub `Input.Field.t` and flag it as 18 # `:invalid` and `:missing`. 19 # 20 # Note that the limited validation that occurs in this phase is limited to 21 # setting the `data_value` to `nil`, adding flags to the `normalized_value`, 22 # and building stub fields/arguments when missing values are required. Actual 23 # addition of errors is handled by validation phases. 24 25 alias Absinthe.Blueprint.Input 26 alias Absinthe.{Blueprint} 27 use Absinthe.Phase 28 29 def run(input, _options \\ []) do 30 # By using a postwalk we can worry about leaf nodes first (scalars, enums), 31 # and then for list and objects merely grab the data values. 32 result = Blueprint.postwalk(input, &handle_node/1) 33 {:ok, result} 34 end 35 36 def handle_node(%Blueprint.Document.Field{arguments: []} = node) do 37 node 38 end 39 40 def handle_node(%Blueprint.Document.Field{arguments: args} = node) do 41 %{node | argument_data: Input.Argument.value_map(args)} 42 end 43 44 def handle_node(%Input.Argument{input_value: input} = node) do 45 %{node | value: input.data} 46 end 47 48 def handle_node(%Input.Value{normalized: %Input.List{items: items}} = node) do 49 data_list = for %{data: data} = item <- items, Input.Value.valid?(item), do: data 50 %{node | data: data_list} 51 end 52 53 def handle_node(%Input.Value{normalized: %Input.Object{fields: fields}} = node) do 54 data = 55 for field <- fields, include_field?(field), into: %{} do 56 # Scalar child nodes will not have a schema_node 57 if field.schema_node != nil do 58 {field.schema_node.identifier, field.input_value.data} 59 else 60 {field.name, field.input_value.data} 61 end 62 end 63 64 %{node | data: data} 65 end 66 67 def handle_node(node) do 68 node 69 end 70 71 defp include_field?(%{input_value: %{normalized: %Input.Null{}}}), do: true 72 defp include_field?(%{input_value: %{data: nil}}), do: false 73 defp include_field?(_), do: true 74 end