From 9998a1130aada8025b17b78ce2ce47cf3a15ab15 Mon Sep 17 00:00:00 2001 From: Maarten van Vliet Date: Sun, 20 Dec 2020 10:26:17 +0100 Subject: [PATCH] Add introspection for deprecated args/input fields Absinthe already supported deprecating args/input fields and this PR exposes that in the introspection query to match the spec. Also some tests were added for deprecating stuff. See https://github.com/graphql/graphql-spec/pull/805 for the spec update --- lib/absinthe/type/built_ins/introspection.ex | 43 +++++++++++++++++++- test/absinthe/type/deprecation_test.exs | 30 ++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/lib/absinthe/type/built_ins/introspection.ex b/lib/absinthe/type/built_ins/introspection.ex index 6d5625695d..e6e1005fa1 100644 --- a/lib/absinthe/type/built_ins/introspection.ex +++ b/lib/absinthe/type/built_ins/introspection.ex @@ -218,11 +218,21 @@ defmodule Absinthe.Type.BuiltIns.Introspection do field :input_fields, type: list_of(:__inputvalue), + args: [ + include_deprecated: [ + type: :boolean, + default_value: false + ] + ], resolve: fn - _, %{source: %Absinthe.Type.InputObject{fields: fields}} -> + %{include_deprecated: show_deprecated}, + %{source: %Absinthe.Type.InputObject{fields: fields}} -> input_fields = fields |> Map.values() + |> Enum.filter(fn %{deprecation: is_deprecated} -> + !is_deprecated || (is_deprecated && show_deprecated) + end) |> Enum.sort_by(& &1.identifier) {:ok, input_fields} @@ -253,10 +263,19 @@ defmodule Absinthe.Type.BuiltIns.Introspection do field :args, type: list_of(:__inputvalue), - resolve: fn _, %{source: %{args: args}} -> + args: [ + include_deprecated: [ + type: :boolean, + default_value: false + ] + ], + resolve: fn %{include_deprecated: show_deprecated}, %{source: %{args: args}} -> args = args |> Map.values() + |> Enum.filter(fn %{deprecation: is_deprecated} -> + !is_deprecated || (is_deprecated && show_deprecated) + end) |> Enum.sort_by(& &1.identifier) {:ok, args} @@ -326,6 +345,26 @@ defmodule Absinthe.Type.BuiltIns.Introspection do _, %{source: _} -> {:ok, nil} end + + field :is_deprecated, + type: :boolean, + resolve: fn + _, %{source: %{deprecation: nil}} -> + {:ok, false} + + _, _ -> + {:ok, true} + end + + field :deprecation_reason, + type: :string, + resolve: fn + _, %{source: %{deprecation: nil}} -> + {:ok, nil} + + _, %{source: %{deprecation: dep}} -> + {:ok, dep.reason} + end end object :__enumvalue, name: "__EnumValue" do diff --git a/test/absinthe/type/deprecation_test.exs b/test/absinthe/type/deprecation_test.exs index 539a455346..2b7e6e5dea 100644 --- a/test/absinthe/type/deprecation_test.exs +++ b/test/absinthe/type/deprecation_test.exs @@ -30,6 +30,16 @@ defmodule Absinthe.Type.DeprecationTest do field :address, :string, deprecate: true end + + enum :colors do + value :red, deprecate: true + value :blue, deprecate: "This isn't supported" + end + + input_object :contact_input do + field :email, non_null(:string), deprecate: true + field :name, non_null(:string), deprecate: "This isn't supported" + end end describe "fields" do @@ -51,4 +61,24 @@ defmodule Absinthe.Type.DeprecationTest do assert nil == field.args.source.deprecation.reason end end + + describe "enum values" do + test "can be deprecated" do + enum_values = TestSchema.__absinthe_type__(:colors).values + assert Type.deprecated?(enum_values.blue) + assert "This isn't supported" == enum_values.blue.deprecation.reason + assert Type.deprecated?(enum_values.red) + assert nil == enum_values.red.deprecation.reason + end + end + + describe "input fields values" do + test "can be deprecated" do + input = TestSchema.__absinthe_type__(:contact_input) + assert Type.deprecated?(input.fields.name) + assert "This isn't supported" == input.fields.name.deprecation.reason + assert Type.deprecated?(input.fields.email) + assert nil == input.fields.email.deprecation.reason + end + end end