Skip to content

Commit 0db42f0

Browse files
authored
Merge pull request #5320 from rmosolgo/resolver-deprecation-reason
Add Resolver.deprecation_reason
2 parents 6d473f5 + b5eb5a3 commit 0db42f0

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

lib/graphql/schema/directive/flagged.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def initialize(target, **options)
3737

3838
argument :by, [String], "Flags to check for this schema member"
3939

40+
repeatable(true)
41+
4042
module VisibleByFlag
4143
def self.included(schema_class)
4244
schema_class.extend(self)

lib/graphql/schema/field.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,24 @@ def resolver_method
4141
end
4242
end
4343

44+
# @return [String, nil]
45+
def deprecation_reason
46+
super || @resolver_class&.deprecation_reason
47+
end
48+
4449
def directives
4550
if @resolver_class && !(r_dirs = @resolver_class.directives).empty?
4651
if !(own_dirs = super).empty?
47-
own_dirs + r_dirs
52+
new_dirs = own_dirs.dup
53+
r_dirs.each do |r_dir|
54+
if r_dir.class.repeatable? ||
55+
( (r_dir_name = r_dir.graphql_name) &&
56+
(!new_dirs.any? { |d| d.graphql_name == r_dir_name })
57+
)
58+
new_dirs << r_dir
59+
end
60+
end
61+
new_dirs
4862
else
4963
r_dirs
5064
end

lib/graphql/schema/member/has_deprecation_reason.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ def deprecation_reason=(text)
1818
directive(GraphQL::Schema::Directive::Deprecated, reason: text)
1919
end
2020
end
21+
22+
def self.extended(child_class)
23+
super
24+
child_class.extend(ClassMethods)
25+
end
26+
27+
module ClassMethods
28+
def deprecation_reason(new_reason = NOT_CONFIGURED)
29+
if NOT_CONFIGURED.equal?(new_reason)
30+
super()
31+
else
32+
self.deprecation_reason = new_reason
33+
end
34+
end
35+
end
2136
end
2237
end
2338
end

lib/graphql/schema/resolver.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Resolver
2828
extend Schema::Member::HasPath
2929
extend Schema::Member::HasDirectives
3030
include Schema::Member::HasDataloader
31+
extend Schema::Member::HasDeprecationReason
3132

3233
# @param object [Object] The application object that this field is being resolved on
3334
# @param context [GraphQL::Query::Context]

spec/graphql/schema/resolver_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,10 +1200,17 @@ class GetThingWithoutDirective < GraphQL::Schema::Resolver
12001200
type String, null: false
12011201
end
12021202

1203+
class GetThingDeprecated < GraphQL::Schema::Resolver
1204+
type String, null: false
1205+
deprecation_reason("Use something else instead")
1206+
end
1207+
12031208
class Query < GraphQL::Schema::Object
12041209
field :get_thing, resolver: GetThing
12051210
field :get_thing_flagged, resolver: GetThing, directives: { GraphQL::Schema::Directive::Flagged => { by: "getThingFlagged" } }
12061211
field :get_thing_field, resolver: GetThingWithoutDirective, directives: { GraphQL::Schema::Directive::Flagged => { by: "getField" } }
1212+
field :get_thing_deprecated, resolver: GetThingDeprecated
1213+
field :get_thing_double_deprecated, resolver: GetThingDeprecated, deprecation_reason: "Overridden reason"
12071214
end
12081215

12091216
query(Query)
@@ -1220,15 +1227,19 @@ class Query < GraphQL::Schema::Object
12201227
Flags to check for this schema member
12211228
"""
12221229
by: [String!]!
1223-
) on ARGUMENT_DEFINITION | ENUM | ENUM_VALUE | FIELD_DEFINITION | INPUT_FIELD_DEFINITION | INPUT_OBJECT | INTERFACE | OBJECT | SCALAR | UNION
1230+
) repeatable on ARGUMENT_DEFINITION | ENUM | ENUM_VALUE | FIELD_DEFINITION | INPUT_FIELD_DEFINITION | INPUT_OBJECT | INTERFACE | OBJECT | SCALAR | UNION
12241231
12251232
type Query {
12261233
getThing(id: ID!): String! @flagged(by: ["getThing"])
1234+
getThingDeprecated: String! @deprecated(reason: "Use something else instead")
1235+
getThingDoubleDeprecated: String! @deprecated(reason: "Overridden reason")
12271236
getThingField(id: ID!): String! @flagged(by: ["getField"])
12281237
getThingFlagged(id: ID!): String! @flagged(by: ["getThingFlagged"]) @flagged(by: ["getThing"])
12291238
}
12301239
GRAPHQL
12311240

12321241
assert_equal expected_str, ResolverDirectivesSchema.to_definition(context: { flags: ["getField", "getThing", "getThingFlagged"] })
1242+
assert_equal "Use something else instead", ResolverDirectivesSchema.get_field("Query", "getThingDeprecated").deprecation_reason
1243+
assert_equal "Overridden reason", ResolverDirectivesSchema.get_field("Query", "getThingDoubleDeprecated").deprecation_reason
12331244
end
12341245
end

0 commit comments

Comments
 (0)