Skip to content

Commit 632bf4f

Browse files
author
Robert Mosolgo
authored
Merge pull request #3416 from jturkel/feature/deprecated-directive-arguments
Introspection support for deprecated directive arguments
2 parents a952af5 + 9c57080 commit 632bf4f

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

lib/graphql/introspection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def self.query(include_deprecated_args: false)
1717
name
1818
description
1919
locations
20-
args {
20+
args#{include_deprecated_args ? '(includeDeprecated: true)' : ''} {
2121
...InputValue
2222
}
2323
}

lib/graphql/introspection/directive_type.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ class DirectiveType < Introspection::BaseObject
1212
field :name, String, null: false, method: :graphql_name
1313
field :description, String, null: true
1414
field :locations, [GraphQL::Schema::LateBoundType.new("__DirectiveLocation")], null: false
15-
field :args, [GraphQL::Schema::LateBoundType.new("__InputValue")], null: false
15+
field :args, [GraphQL::Schema::LateBoundType.new("__InputValue")], null: false do
16+
argument :include_deprecated, Boolean, required: false, default_value: false
17+
end
1618
field :on_operation, Boolean, null: false, deprecation_reason: "Use `locations`.", method: :on_operation?
1719
field :on_fragment, Boolean, null: false, deprecation_reason: "Use `locations`.", method: :on_fragment?
1820
field :on_field, Boolean, null: false, deprecation_reason: "Use `locations`.", method: :on_field?
1921

20-
def args
21-
@context.warden.arguments(@object)
22+
def args(include_deprecated:)
23+
args = @context.warden.arguments(@object)
24+
args = args.reject(&:deprecation_reason) unless include_deprecated
25+
args
2226
end
2327
end
2428
end

spec/graphql/introspection/directive_type_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
}
1919
|}
2020

21+
let(:directive_with_deprecated_arg) do
22+
Class.new(GraphQL::Schema::Directive) do
23+
graphql_name "customTransform"
24+
locations GraphQL::Schema::Directive::FIELD
25+
argument :old_way, String, required: false, deprecation_reason: "Use the newWay"
26+
argument :new_way, String, required: false
27+
end
28+
end
29+
2130
let(:schema) { Class.new(Dummy::Schema) }
2231
let(:result) { schema.execute(query_string) }
2332
before do
@@ -63,4 +72,51 @@
6372
}}
6473
assert_equal(expected, result)
6574
end
75+
76+
it "hides deprecated arguments by default" do
77+
schema.directive(directive_with_deprecated_arg)
78+
result = schema.execute <<-GRAPHQL
79+
{
80+
__schema {
81+
directives {
82+
name
83+
args {
84+
name
85+
}
86+
}
87+
}
88+
}
89+
GRAPHQL
90+
91+
directive_result = result["data"]["__schema"]["directives"].find { |d| d["name"] == "customTransform" }
92+
expected = [
93+
{"name" => "newWay"}
94+
]
95+
assert_equal(expected, directive_result["args"])
96+
end
97+
98+
it "can expose deprecated arguments" do
99+
schema.directive(directive_with_deprecated_arg)
100+
result = schema.execute <<-GRAPHQL
101+
{
102+
__schema {
103+
directives {
104+
name
105+
args(includeDeprecated: true) {
106+
name
107+
isDeprecated
108+
deprecationReason
109+
}
110+
}
111+
}
112+
}
113+
GRAPHQL
114+
115+
directive_result = result["data"]["__schema"]["directives"].find { |d| d["name"] == "customTransform" }
116+
expected = [
117+
{"name" => "oldWay", "isDeprecated" => true, "deprecationReason" => "Use the newWay"},
118+
{"name" => "newWay", "isDeprecated" => false, "deprecationReason" => nil}
119+
]
120+
assert_equal(expected, directive_result["args"])
121+
end
66122
end

spec/graphql/schema/build_from_definition_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,8 @@ def assert_schema_and_compare_output(definition)
785785

786786
it 'supports @deprecated' do
787787
schema = <<-SCHEMA
788+
directive @directiveWithDeprecatedArg(deprecatedArg: Boolean @deprecated(reason: "Don't use me!")) on OBJECT
789+
788790
enum MyEnum {
789791
OLD_VALUE @deprecated
790792
OTHER_VALUE @deprecated(reason: "Terrible reasons")

spec/graphql/schema/printer_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class Subscription < GraphQL::Schema::Object
152152
to the executor.
153153
"""
154154
type __Directive {
155-
args: [__InputValue!]!
155+
args(includeDeprecated: Boolean = false): [__InputValue!]!
156156
description: String
157157
locations: [__DirectiveLocation!]!
158158
name: String!
@@ -858,6 +858,8 @@ class OddlyNamedQuery < GraphQL::Schema::Object
858858
859859
directive @customDirective on FIELD_DEFINITION
860860
861+
directive @directiveWithDeprecatedArg(deprecatedArg: Boolean @deprecated(reason: "Don't use me!")) on OBJECT
862+
861863
directive @intDir(a: Int!) on INPUT_FIELD_DEFINITION
862864
863865
directive @someDirective on OBJECT

0 commit comments

Comments
 (0)