Skip to content

Commit 9998a11

Browse files
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 graphql/graphql-spec#805 for the spec update
1 parent a968eac commit 9998a11

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

lib/absinthe/type/built_ins/introspection.ex

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,21 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
218218

219219
field :input_fields,
220220
type: list_of(:__inputvalue),
221+
args: [
222+
include_deprecated: [
223+
type: :boolean,
224+
default_value: false
225+
]
226+
],
221227
resolve: fn
222-
_, %{source: %Absinthe.Type.InputObject{fields: fields}} ->
228+
%{include_deprecated: show_deprecated},
229+
%{source: %Absinthe.Type.InputObject{fields: fields}} ->
223230
input_fields =
224231
fields
225232
|> Map.values()
233+
|> Enum.filter(fn %{deprecation: is_deprecated} ->
234+
!is_deprecated || (is_deprecated && show_deprecated)
235+
end)
226236
|> Enum.sort_by(& &1.identifier)
227237

228238
{:ok, input_fields}
@@ -253,10 +263,19 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
253263

254264
field :args,
255265
type: list_of(:__inputvalue),
256-
resolve: fn _, %{source: %{args: args}} ->
266+
args: [
267+
include_deprecated: [
268+
type: :boolean,
269+
default_value: false
270+
]
271+
],
272+
resolve: fn %{include_deprecated: show_deprecated}, %{source: %{args: args}} ->
257273
args =
258274
args
259275
|> Map.values()
276+
|> Enum.filter(fn %{deprecation: is_deprecated} ->
277+
!is_deprecated || (is_deprecated && show_deprecated)
278+
end)
260279
|> Enum.sort_by(& &1.identifier)
261280

262281
{:ok, args}
@@ -326,6 +345,26 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
326345
_, %{source: _} ->
327346
{:ok, nil}
328347
end
348+
349+
field :is_deprecated,
350+
type: :boolean,
351+
resolve: fn
352+
_, %{source: %{deprecation: nil}} ->
353+
{:ok, false}
354+
355+
_, _ ->
356+
{:ok, true}
357+
end
358+
359+
field :deprecation_reason,
360+
type: :string,
361+
resolve: fn
362+
_, %{source: %{deprecation: nil}} ->
363+
{:ok, nil}
364+
365+
_, %{source: %{deprecation: dep}} ->
366+
{:ok, dep.reason}
367+
end
329368
end
330369

331370
object :__enumvalue, name: "__EnumValue" do

test/absinthe/type/deprecation_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ defmodule Absinthe.Type.DeprecationTest do
3030

3131
field :address, :string, deprecate: true
3232
end
33+
34+
enum :colors do
35+
value :red, deprecate: true
36+
value :blue, deprecate: "This isn't supported"
37+
end
38+
39+
input_object :contact_input do
40+
field :email, non_null(:string), deprecate: true
41+
field :name, non_null(:string), deprecate: "This isn't supported"
42+
end
3343
end
3444

3545
describe "fields" do
@@ -51,4 +61,24 @@ defmodule Absinthe.Type.DeprecationTest do
5161
assert nil == field.args.source.deprecation.reason
5262
end
5363
end
64+
65+
describe "enum values" do
66+
test "can be deprecated" do
67+
enum_values = TestSchema.__absinthe_type__(:colors).values
68+
assert Type.deprecated?(enum_values.blue)
69+
assert "This isn't supported" == enum_values.blue.deprecation.reason
70+
assert Type.deprecated?(enum_values.red)
71+
assert nil == enum_values.red.deprecation.reason
72+
end
73+
end
74+
75+
describe "input fields values" do
76+
test "can be deprecated" do
77+
input = TestSchema.__absinthe_type__(:contact_input)
78+
assert Type.deprecated?(input.fields.name)
79+
assert "This isn't supported" == input.fields.name.deprecation.reason
80+
assert Type.deprecated?(input.fields.email)
81+
assert nil == input.fields.email.deprecation.reason
82+
end
83+
end
5484
end

0 commit comments

Comments
 (0)