deprecated_directive_fields.ex (1741B)
1 defmodule Absinthe.Phase.Schema.DeprecatedDirectiveFields do 2 @moduledoc false 3 # The spec of Oct 2015 has the onOperation, onFragment and onField 4 # fields for directives (https://spec.graphql.org/October2015/#sec-Schema-Introspection) 5 # See https://github.com/graphql/graphql-spec/pull/152 for the rationale. 6 # These fields are deprecated and can be removed in the future. 7 alias Absinthe.Blueprint 8 9 use Absinthe.Schema.Notation 10 11 @behaviour Absinthe.Phase 12 13 def run(input, _options \\ []) do 14 blueprint = Blueprint.prewalk(input, &handle_node/1) 15 16 {:ok, blueprint} 17 end 18 19 defp handle_node(%Blueprint.Schema.ObjectTypeDefinition{identifier: :__directive} = node) do 20 [types] = __MODULE__.__absinthe_blueprint__().schema_definitions 21 22 new_node = Enum.find(types.type_definitions, &(&1.identifier == :deprecated_directive_fields)) 23 24 fields = node.fields ++ new_node.fields 25 26 %{node | fields: fields} 27 end 28 29 defp handle_node(node) do 30 node 31 end 32 33 object :deprecated_directive_fields do 34 field :on_operation, :boolean do 35 deprecate "Check `locations` field for enum value OPERATION" 36 37 resolve fn _, %{source: source} -> 38 {:ok, Enum.any?(source.locations, &Enum.member?([:query, :mutation, :subscription], &1))} 39 end 40 end 41 42 field :on_fragment, :boolean do 43 deprecate "Check `locations` field for enum value FRAGMENT_SPREAD" 44 45 resolve fn _, %{source: source} -> 46 {:ok, Enum.member?(source.locations, :fragment_spread)} 47 end 48 end 49 50 field :on_field, :boolean do 51 deprecate "Check `locations` field for enum value FIELD" 52 53 resolve fn _, %{source: source} -> 54 {:ok, Enum.member?(source.locations, :field)} 55 end 56 end 57 end 58 end