zf

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

provided_non_null_arguments.ex (1449B)


      1 defmodule Absinthe.Phase.Document.Validation.ProvidedNonNullArguments do
      2   @moduledoc false
      3 
      4   # Validates document to ensure that all non-null arguments are provided.
      5 
      6   alias Absinthe.{Blueprint, Phase, Schema, Type}
      7 
      8   use Absinthe.Phase
      9 
     10   @doc """
     11   Run the validation.
     12   """
     13   @spec run(Blueprint.t(), Keyword.t()) :: Phase.result_t()
     14   def run(input, _options \\ []) do
     15     result =
     16       Blueprint.update_current(input, fn op ->
     17         Blueprint.prewalk(op, &handle_node(&1, input.schema))
     18       end)
     19 
     20     {:ok, result}
     21   end
     22 
     23   @spec handle_node(Blueprint.node_t(), Schema.t()) :: Blueprint.node_t()
     24   # Missing Arguments
     25   defp handle_node(%Blueprint.Input.Argument{value: nil, flags: %{missing: _}} = node, schema) do
     26     node = node |> put_error(error(node, node.schema_node.type, schema))
     27     {:halt, node}
     28   end
     29 
     30   defp handle_node(node, _) do
     31     node
     32   end
     33 
     34   # Generate the error for this validation
     35   @spec error(Blueprint.node_t(), Type.t(), Schema.t()) :: Phase.Error.t()
     36   defp error(node, type, schema) do
     37     type_name = Type.name(type, schema)
     38 
     39     %Phase.Error{
     40       phase: __MODULE__,
     41       message: error_message(node.name, type_name),
     42       locations: [node.source_location]
     43     }
     44   end
     45 
     46   @doc """
     47   Generate the argument error.
     48   """
     49   @spec error_message(String.t(), String.t()) :: String.t()
     50   def error_message(name, type_name) do
     51     ~s(In argument "#{name}": Expected type "#{type_name}", found null.)
     52   end
     53 end