parse.ex (2419B)
1 defmodule Absinthe.Phase.Document.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(%{normalized: nil} = node, _context) do 16 node 17 end 18 19 defp handle_node(%Input.Value{normalized: normalized} = node, context) do 20 case build_value(normalized, node.schema_node, context) do 21 {:ok, value} -> 22 %{node | data: value} 23 24 :not_leaf_node -> 25 node 26 27 {:error, flag} -> 28 %{node | normalized: normalized |> flag_invalid(flag)} 29 end 30 end 31 32 defp handle_node(node, _context), do: node 33 34 defp build_value(%Input.Null{}, %Type.NonNull{}, _) do 35 {:error, :non_null} 36 end 37 38 defp build_value(%{__struct__: struct} = normalized, %Type.Scalar{} = schema_node, context) 39 when struct in [Input.Boolean, Input.Float, Input.Integer, Input.String, Input.Null] do 40 case Type.Scalar.parse(schema_node, normalized, context) do 41 :error -> 42 {:error, :bad_parse} 43 44 {:ok, val} -> 45 {:ok, val} 46 end 47 end 48 49 defp build_value( 50 %Input.Object{} = normalized, 51 %Type.Scalar{open_ended: true} = schema_node, 52 context 53 ) do 54 case Type.Scalar.parse(schema_node, normalized, context) do 55 :error -> 56 {:error, :bad_parse} 57 58 {:ok, val} -> 59 {:ok, val} 60 end 61 end 62 63 defp build_value(_normalized, %Type.Scalar{}, _context) do 64 {:error, :bad_parse} 65 end 66 67 defp build_value(%{value: value} = _normalized, nil = _schema_node, _context) do 68 {:ok, value} 69 end 70 71 defp build_value(%Input.Null{}, %Type.Enum{}, _) do 72 {:ok, nil} 73 end 74 75 defp build_value(normalized, %Type.Enum{} = schema_node, _) do 76 case Type.Enum.parse(schema_node, normalized) do 77 {:ok, %{value: value}} -> 78 {:ok, value} 79 80 :error -> 81 {:error, :bad_parse} 82 end 83 end 84 85 defp build_value(normalized, %Type.NonNull{of_type: inner_type}, context) do 86 build_value(normalized, inner_type, context) 87 end 88 89 defp build_value(%{__struct__: struct}, %Type.InputObject{}, _) 90 when struct in [Input.Boolean, Input.Float, Input.Integer, Input.String] do 91 {:error, :bad_parse} 92 end 93 94 defp build_value(_, _, _) do 95 :not_leaf_node 96 end 97 end