zf

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

provided_non_null_variables.ex (1729B)


      1 defmodule Absinthe.Phase.Document.Validation.ProvidedNonNullVariables do
      2   @moduledoc false
      3 
      4   # Validates document to ensure that all non-null variable definitions
      5   # are provided values.
      6 
      7   alias Absinthe.{Blueprint, Phase, Schema}
      8 
      9   use Absinthe.Phase
     10   use Absinthe.Phase.Validation
     11 
     12   @doc """
     13   Run the validation.
     14   """
     15   @spec run(Blueprint.t(), Keyword.t()) :: Phase.result_t()
     16   def run(input, _options \\ []) do
     17     result =
     18       Blueprint.update_current(input, fn op ->
     19         Blueprint.prewalk(op, &handle_node(&1, input.schema))
     20       end)
     21 
     22     {:ok, result}
     23   end
     24 
     25   # Find variable definitions
     26   @spec handle_node(Blueprint.node_t(), Schema.t()) :: Blueprint.node_t()
     27   defp handle_node(
     28          %Blueprint.Document.VariableDefinition{
     29            type: %Blueprint.TypeReference.NonNull{},
     30            provided_value: nil
     31          } = node,
     32          _
     33        ) do
     34     node
     35     |> put_error(error(node))
     36   end
     37 
     38   defp handle_node(
     39          %Blueprint.Document.VariableDefinition{
     40            type: %Blueprint.TypeReference.NonNull{},
     41            provided_value: %Blueprint.Input.Null{}
     42          } = node,
     43          _
     44        ) do
     45     node
     46     |> put_error(error(node))
     47   end
     48 
     49   defp handle_node(node, _) do
     50     node
     51   end
     52 
     53   # Generate an error for variable definition
     54   @spec error(Blueprint.Document.VariableDefinition.t()) :: Phase.Error.t()
     55   defp error(node) do
     56     %Phase.Error{
     57       phase: __MODULE__,
     58       message: error_message(node.name),
     59       locations: [node.source_location]
     60     }
     61   end
     62 
     63   @doc """
     64   Generate the error message.
     65   """
     66   @spec error_message(String.t()) :: String.t()
     67   def error_message(variable_name) do
     68     ~s(Variable "#{variable_name}": Expected non-null, found null.)
     69   end
     70 end