parse.ex (1704B)
1 defmodule Absinthe.Phase.Schema.Arguments.Parse do 2 @moduledoc false 3 4 # Parses Leaf Node inputs 5 6 alias Absinthe.Blueprint.Input 7 alias Absinthe.{Blueprint, Type} 8 use Absinthe.Phase 9 10 def run(input, options \\ []) do 11 result = Blueprint.prewalk(input, &handle_node(&1, options[:context] || %{})) 12 {:ok, result} 13 end 14 15 # defp handle_node(%{schema_node: nil} = node, _context) do 16 # {:halt, node} 17 # end 18 19 defp handle_node(%{normalized: nil} = node, _context) do 20 node 21 end 22 23 defp handle_node(%Input.Value{normalized: normalized} = node, context) do 24 case build_value(normalized, node.schema_node, context) do 25 {:ok, value} -> 26 %{node | data: value} 27 28 :not_leaf_node -> 29 node 30 31 {:error, flag} -> 32 %{node | normalized: normalized |> flag_invalid(flag)} 33 end 34 end 35 36 defp handle_node(node, _context), do: node 37 38 defp build_value(%Input.Null{}, %Type.NonNull{}, _) do 39 {:error, :non_null} 40 end 41 42 defp build_value(normalized, %Type.Scalar{} = schema_node, context) do 43 case Type.Scalar.parse(schema_node, normalized, context) do 44 :error -> 45 {:error, :bad_parse} 46 47 {:ok, val} -> 48 {:ok, val} 49 end 50 end 51 52 defp build_value(%Input.Null{}, %Type.Enum{}, _) do 53 {:ok, nil} 54 end 55 56 defp build_value(normalized, %Type.Enum{} = schema_node, _) do 57 case Type.Enum.parse(schema_node, normalized) do 58 {:ok, %{value: value}} -> 59 {:ok, value} 60 61 :error -> 62 {:error, :bad_parse} 63 end 64 end 65 66 defp build_value(normalized, %Type.NonNull{of_type: inner_type}, context) do 67 build_value(normalized, inner_type, context) 68 end 69 70 defp build_value(_, _, _) do 71 :not_leaf_node 72 end 73 end