normalize.ex (1620B)
1 defmodule Absinthe.Phase.Document.Arguments.Normalize do 2 @moduledoc false 3 4 # Populate all arguments in the document with their provided values: 5 # 6 # - If a literal value is provided for an argument, set the `Argument.t`'s 7 # `normalized_value` field to that value. 8 # - If a variable is provided for an argument, set the `Argument.t`'s 9 # `normalized_value` to the reconciled value for the variable 10 # (Note: this requires the `Phase.Document.Variables` phase as a 11 # prerequisite). 12 # 13 # Note that no validation occurs in this phase. 14 15 use Absinthe.Phase 16 alias Absinthe.Blueprint 17 alias Absinthe.Blueprint.Input 18 19 @spec run(Blueprint.t(), Keyword.t()) :: {:ok, Blueprint.t()} 20 def run(input, _options \\ []) do 21 provided_values = get_provided_values(input) 22 node = Blueprint.prewalk(input, &handle_node(&1, provided_values)) 23 {:ok, node} 24 end 25 26 @spec get_provided_values(Blueprint.t()) :: map 27 defp get_provided_values(input) do 28 case Blueprint.current_operation(input) do 29 nil -> %{} 30 operation -> operation.provided_values 31 end 32 end 33 34 defp handle_node( 35 %Input.RawValue{content: %Input.Variable{name: variable_name}} = node, 36 provided_values 37 ) do 38 %Input.Value{ 39 normalized: Map.get(provided_values, variable_name), 40 raw: node 41 } 42 end 43 44 # Argument not using a variable: Set provided value from the raw value 45 defp handle_node(%Input.RawValue{} = node, _provided_values) do 46 %Input.Value{ 47 normalized: node.content, 48 raw: node 49 } 50 end 51 52 defp handle_node(node, _provided_values) do 53 node 54 end 55 end