only_one_subscription.ex (944B)
1 defmodule Absinthe.Phase.Document.Validation.OnlyOneSubscription do 2 @moduledoc false 3 4 # Validates document to ensure that the only variables that are used in a 5 # document are defined on the operation. 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 bp = 18 Blueprint.update_current(input, fn 19 %{type: :subscription} = op -> 20 check_op(op) 21 22 op -> 23 op 24 end) 25 26 {:ok, bp} 27 end 28 29 defp check_op(%{selections: [_, _ | _]} = op) do 30 error = %Phase.Error{ 31 phase: __MODULE__, 32 message: "Only one field is permitted on the root object when subscribing", 33 locations: [op.source_location] 34 } 35 36 op 37 |> flag_invalid(:too_many_fields) 38 |> put_error(error) 39 end 40 41 defp check_op(op), do: op 42 end