selected_current_operation.ex (1586B)
1 defmodule Absinthe.Phase.Document.Validation.SelectedCurrentOperation do 2 @moduledoc false 3 4 # Validates an operation name was provided when needed. 5 6 alias Absinthe.{Blueprint, Phase} 7 8 use Absinthe.Phase 9 use Absinthe.Phase.Validation 10 11 @doc """ 12 Run the validation. 13 """ 14 @spec run(Blueprint.t(), Keyword.t()) :: Phase.result_t() 15 def run(input, options \\ []) do 16 result = 17 case {Blueprint.current_operation(input), length(input.operations)} do 18 {nil, count} when count >= 1 -> 19 operation_name = Keyword.get(options, :operation_name) 20 21 input 22 |> flag_invalid(:no_current_operation) 23 |> put_error(error(operation_name, count)) 24 25 _ -> 26 input 27 end 28 29 {:ok, result} 30 end 31 32 # Generate the error 33 @spec error(String.t(), integer()) :: Phase.Error.t() 34 defp error(operation_name, operation_count) do 35 %Phase.Error{ 36 phase: __MODULE__, 37 message: error_message(operation_name, operation_count) 38 } 39 end 40 41 def error_message(nil, _) do 42 """ 43 Must provide a valid operation name if query contains multiple operations. 44 45 No operation name was given. 46 """ 47 end 48 49 def error_message(operation_name, 1) do 50 """ 51 The provided operation name did not match the operation in the query. 52 53 The provided operation name was: #{inspect(operation_name)} 54 """ 55 end 56 57 def error_message(operation_name, _) do 58 """ 59 Must provide a valid operation name if query contains multiple operations. 60 61 The provided operation name was: #{inspect(operation_name)} 62 """ 63 end 64 end