Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introspection support for deprecated directive arguments #3416

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/graphql/introspection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.query(include_deprecated_args: false)
name
description
locations
args {
args#{include_deprecated_args ? '(includeDeprecated: true)' : ''} {
...InputValue
}
}
Expand Down
10 changes: 7 additions & 3 deletions lib/graphql/introspection/directive_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ class DirectiveType < Introspection::BaseObject
field :name, String, null: false, method: :graphql_name
field :description, String, null: true
field :locations, [GraphQL::Schema::LateBoundType.new("__DirectiveLocation")], null: false
field :args, [GraphQL::Schema::LateBoundType.new("__InputValue")], null: false
field :args, [GraphQL::Schema::LateBoundType.new("__InputValue")], null: false do
argument :include_deprecated, Boolean, required: false, default_value: false
end
field :on_operation, Boolean, null: false, deprecation_reason: "Use `locations`.", method: :on_operation?
field :on_fragment, Boolean, null: false, deprecation_reason: "Use `locations`.", method: :on_fragment?
field :on_field, Boolean, null: false, deprecation_reason: "Use `locations`.", method: :on_field?

def args
@context.warden.arguments(@object)
def args(include_deprecated:)
args = @context.warden.arguments(@object)
args = args.reject(&:deprecation_reason) unless include_deprecated
args
end
end
end
Expand Down
56 changes: 56 additions & 0 deletions spec/graphql/introspection/directive_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
}
|}

let(:directive_with_deprecated_arg) do
Class.new(GraphQL::Schema::Directive) do
graphql_name "customTransform"
locations GraphQL::Schema::Directive::FIELD
argument :old_way, String, required: false, deprecation_reason: "Use the newWay"
argument :new_way, String, required: false
end
end

let(:schema) { Class.new(Dummy::Schema) }
let(:result) { schema.execute(query_string) }
before do
Expand Down Expand Up @@ -63,4 +72,51 @@
}}
assert_equal(expected, result)
end

it "hides deprecated arguments by default" do
schema.directive(directive_with_deprecated_arg)
result = schema.execute <<-GRAPHQL
{
__schema {
directives {
name
args {
name
}
}
}
}
GRAPHQL

directive_result = result["data"]["__schema"]["directives"].find { |d| d["name"] == "customTransform" }
expected = [
{"name" => "newWay"}
]
assert_equal(expected, directive_result["args"])
end

it "can expose deprecated arguments" do
schema.directive(directive_with_deprecated_arg)
result = schema.execute <<-GRAPHQL
{
__schema {
directives {
name
args(includeDeprecated: true) {
name
isDeprecated
deprecationReason
}
}
}
}
GRAPHQL

directive_result = result["data"]["__schema"]["directives"].find { |d| d["name"] == "customTransform" }
expected = [
{"name" => "oldWay", "isDeprecated" => true, "deprecationReason" => "Use the newWay"},
{"name" => "newWay", "isDeprecated" => false, "deprecationReason" => nil}
]
assert_equal(expected, directive_result["args"])
end
end
2 changes: 2 additions & 0 deletions spec/graphql/schema/build_from_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ def assert_schema_and_compare_output(definition)

it 'supports @deprecated' do
schema = <<-SCHEMA
directive @directiveWithDeprecatedArg(deprecatedArg: Boolean @deprecated(reason: "Don't use me!")) on OBJECT

enum MyEnum {
OLD_VALUE @deprecated
OTHER_VALUE @deprecated(reason: "Terrible reasons")
Expand Down
4 changes: 3 additions & 1 deletion spec/graphql/schema/printer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Subscription < GraphQL::Schema::Object
to the executor.
"""
type __Directive {
args: [__InputValue!]!
args(includeDeprecated: Boolean = false): [__InputValue!]!
description: String
locations: [__DirectiveLocation!]!
name: String!
Expand Down Expand Up @@ -858,6 +858,8 @@ class OddlyNamedQuery < GraphQL::Schema::Object

directive @customDirective on FIELD_DEFINITION

directive @directiveWithDeprecatedArg(deprecatedArg: Boolean @deprecated(reason: "Don't use me!")) on OBJECT

directive @intDir(a: Int!) on INPUT_FIELD_DEFINITION

directive @someDirective on OBJECT
Expand Down