Skip to content

Commit

Permalink
Add introspection for deprecated args/input fields
Browse files Browse the repository at this point in the history
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 graphql/graphql-spec#805 for the spec update
  • Loading branch information
maartenvanvliet committed Dec 20, 2020
1 parent a968eac commit 9998a11
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
43 changes: 41 additions & 2 deletions lib/absinthe/type/built_ins/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions test/absinthe/type/deprecation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 9998a11

Please sign in to comment.