zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

missing_variables.ex (1543B)


      1 defmodule Absinthe.Phase.Document.MissingVariables do
      2   @moduledoc false
      3 
      4   # Fills out missing arguments and input object fields.
      5   #
      6   # Filling out means inserting a stubbed `Input.Argument` or `Input.Field` struct.
      7   #
      8   # Only those arguments which are non null and / or have a default value are filled
      9   # out.
     10   #
     11   # If an argument or input object field is non null and missing, it is marked invalid
     12 
     13   use Absinthe.Phase
     14   alias Absinthe.{Blueprint, Type}
     15 
     16   @spec run(Blueprint.t(), Keyword.t()) :: {:ok, Blueprint.t()}
     17   def run(input, _options \\ []) do
     18     node = Blueprint.prewalk(input, &handle_node/1)
     19     {:ok, node}
     20   end
     21 
     22   defp handle_node(
     23          %Blueprint.Input.Argument{schema_node: schema_node, input_value: %{normalized: nil}} =
     24            node
     25        ) do
     26     handle_defaults(node, schema_node)
     27   end
     28 
     29   defp handle_node(
     30          %Blueprint.Input.Field{schema_node: schema_node, input_value: %{normalized: nil}} = node
     31        ) do
     32     handle_defaults(node, schema_node)
     33   end
     34 
     35   defp handle_node(node), do: node
     36 
     37   defp handle_defaults(node, schema_node) do
     38     case schema_node do
     39       %{default_value: val} when not is_nil(val) ->
     40         fill_default(node, val)
     41 
     42       %{deprecation: %{}} ->
     43         node
     44 
     45       %{type: %Type.NonNull{}} ->
     46         node |> flag_invalid(:missing)
     47 
     48       _ ->
     49         node
     50     end
     51   end
     52 
     53   defp fill_default(%{input_value: input} = node, val) do
     54     input = %{input | data: val, normalized: %Blueprint.Input.Generated{by: __MODULE__}}
     55     %{node | input_value: input}
     56   end
     57 end