known_fragment_names.ex (1228B)
1 defmodule Absinthe.Phase.Document.Validation.KnownFragmentNames do 2 @moduledoc false 3 4 # Validates document to ensure that only fragment spreads reference named 5 # fragments that exist. 6 7 alias Absinthe.{Blueprint, Phase} 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 = Blueprint.prewalk(input, &handle_node(&1, input)) 18 {:ok, result} 19 end 20 21 # Find the root and check for multiple anonymous operations 22 @spec handle_node(Blueprint.node_t(), Blueprint.t()) :: Blueprint.node_t() 23 defp handle_node(%Blueprint.Document.Fragment.Spread{} = node, blueprint) do 24 case Blueprint.fragment(blueprint, node.name) do 25 nil -> 26 node 27 |> flag_invalid(:bad_fragment_name) 28 |> put_error(error(node)) 29 30 _ -> 31 node 32 end 33 end 34 35 defp handle_node(node, _) do 36 node 37 end 38 39 # Generate the error for the node 40 @spec error(Blueprint.node_t()) :: Phase.Error.t() 41 defp error(node) do 42 %Phase.Error{ 43 phase: __MODULE__, 44 message: ~s(Unknown fragment "#{node.name}"), 45 locations: [node.source_location] 46 } 47 end 48 end