Skip to content

Commit 2ad2f83

Browse files
authored
Merge pull request #5255 from rmosolgo/enum-value-methods-optional
Make Enum value_methods optional
2 parents a539cd1 + f4cb972 commit 2ad2f83

File tree

8 files changed

+49
-22
lines changed

8 files changed

+49
-22
lines changed

lib/graphql/schema/build_from_definition.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ def build_enum_type(enum_type_definition, type_resolver)
298298
description: enum_value_definition.description,
299299
directives: builder.prepare_directives(enum_value_definition, type_resolver),
300300
ast_node: enum_value_definition,
301-
value_method: GraphQL::Schema::Enum.respond_to?(enum_value_definition.name.downcase) ? false : nil,
302301
)
303302
end
304303
end

lib/graphql/schema/enum.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def value(*args, value_method: nil, **kwargs, &block)
7070
kwargs[:owner] = self
7171
value = enum_value_class.new(*args, **kwargs, &block)
7272

73-
generate_value_method(value, value_method)
73+
if value_method || (value_methods && value_method != false)
74+
generate_value_method(value, value_method)
75+
end
7476

7577
key = value.graphql_name
7678
prev_value = own_values[key]
@@ -159,6 +161,18 @@ def enum_value_class(new_enum_value_class = nil)
159161
end
160162
end
161163

164+
def value_methods(new_value = NOT_CONFIGURED)
165+
if NOT_CONFIGURED.equal?(new_value)
166+
if @value_methods != nil
167+
@value_methods
168+
else
169+
find_inherited_value(:value_methods, false)
170+
end
171+
else
172+
@value_methods = new_value
173+
end
174+
end
175+
162176
def kind
163177
GraphQL::TypeKinds::ENUM
164178
end
@@ -220,6 +234,7 @@ def inherited(child_class)
220234
# because they would end up with names like `#<Class0x1234>::UnresolvedValueError` which messes up bug trackers
221235
child_class.const_set(:UnresolvedValueError, Class.new(Schema::Enum::UnresolvedValueError))
222236
end
237+
child_class.class_eval { @value_methods = nil }
223238
super
224239
end
225240

lib/graphql/schema/input_object.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def argument(*args, **kwargs, &block)
156156
def #{method_name}
157157
self[#{method_name.inspect}]
158158
end
159-
alias_method :#{method_name}, :#{method_name}
159+
alias_method #{method_name.inspect}, #{method_name.inspect}
160160
RUBY
161161
end
162162
argument_defn

lib/graphql/schema/loader.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ def define_type(type, type_resolver)
108108
enum_value["name"],
109109
description: enum_value["description"],
110110
deprecation_reason: enum_value["deprecationReason"],
111-
value_method: respond_to?(enum_value["name"].downcase) ? false : nil
112111
)
113112
end
114113
end

spec/graphql/schema/dynamic_members_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class Thing < LegacyThing
175175

176176
class Language < BaseEnum
177177
value "RUBY"
178-
value "PERL6", deprecation_reason: "Use RAKU instead", future_schema: true, value_method: false
178+
value "PERL6", deprecation_reason: "Use RAKU instead", future_schema: true
179179
value "PERL6", future_schema: false
180180
value "RAKU", future_schema: true
181181
value "COFFEE_SCRIPT", future_schema: false
@@ -1026,7 +1026,7 @@ class BaseEnumValue < GraphQL::Schema::EnumValue
10261026
class DuplicateEnumValue < GraphQL::Schema::Enum
10271027
enum_value_class(BaseEnumValue)
10281028
value "ONE", description: "second definition", allow_for: [2, 3]
1029-
value "ONE", description: "first definition", allow_for: [1, 2], value_method: false
1029+
value "ONE", description: "first definition", allow_for: [1, 2]
10301030
end
10311031

10321032
class DuplicateFieldObject < GraphQL::Schema::Object

spec/graphql/schema/enum_spec.rb

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@
1111
end
1212

1313
describe "value methods" do
14-
it "defines default methods to fetch graphql names" do
15-
assert_equal enum.string, "STRING"
16-
assert_equal enum.woodwind, "WOODWIND"
17-
assert_equal enum.brass, "BRASS"
18-
assert_equal enum.didgeridoo, "DIDGERIDOO"
19-
assert_equal enum.keys, "KEYS"
14+
class EnumWithValueMethods < GraphQL::Schema::Enum
15+
value_methods(true)
16+
value :SOMETHING
17+
value :SOMETHING_ELSE, value_method: false
18+
value :SOMETHING_CUSTOM, value_method: :custom
19+
end
20+
it "defines methods to fetch graphql names when configured" do
21+
assert_equal "SOMETHING", EnumWithValueMethods.something
22+
assert_equal "SOMETHING", EnumWithValueMethods.something
23+
end
24+
25+
it "inherits a value_methods config" do
26+
new_enum = Class.new(EnumWithValueMethods)
27+
new_enum.value(:NEW_VALUE)
28+
assert_equal "NEW_VALUE", new_enum.new_value
2029
end
2130

2231
describe "when value_method is configured" do
@@ -27,23 +36,29 @@
2736
end
2837

2938
describe "when value_method conflicts with existing method" do
39+
class ConflictEnum < GraphQL::Schema::Enum
40+
value_methods(true)
41+
end
3042
it "does not define method and emits warning" do
3143
expected_message = "Failed to define value method for :value, because ConflictEnum already responds to that method. Use `value_method:` to override the method name or `value_method: false` to disable Enum value method generation.\n"
3244
assert_warns(expected_message) do
33-
conflict_enum = Class.new(GraphQL::Schema::Enum)
34-
Object.const_set("ConflictEnum", conflict_enum)
35-
already_defined_method = conflict_enum.method(:value)
36-
conflict_enum.value "VALUE", "Makes conflict"
37-
assert_equal conflict_enum.method(:value), already_defined_method
45+
already_defined_method = ConflictEnum.method(:value)
46+
ConflictEnum.value "VALUE", "Makes conflict"
47+
assert_equal ConflictEnum.method(:value), already_defined_method
3848
end
3949
end
4050
end
4151

4252
describe "when value_method = false" do
4353
it "does not define method" do
44-
assert_equal enum.respond_to?(:silence), false
54+
assert_equal EnumWithValueMethods.respond_to?(:something_else), false
4555
end
4656
end
57+
it "doesn't define value methods by default" do
58+
enum = Class.new(GraphQL::Schema::Enum) { graphql_name("SomeEnum") }
59+
enum.value("SOME_VALUE")
60+
refute enum.respond_to?(:some_value)
61+
end
4762
end
4863

4964
describe "type info" do
@@ -145,7 +160,7 @@ def value
145160
class MultipleNameTestEnum < GraphQL::Schema::Enum
146161
value "A"
147162
value "B", value: :a
148-
value "B", value: :b, value_method: false
163+
value "B", value: :b
149164
end
150165

151166
it "doesn't allow it from enum_values" do

spec/graphql/schema/loader_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
value "FOO", value: :foo
2121
value "BAR", deprecation_reason: "Don't use BAR"
22-
value "NAME", value_method: false
23-
value "foo", value_method: false
22+
value "foo"
2423
end
2524

2625
sub_input_type = Class.new(GraphQL::Schema::InputObject) do

spec/support/jazz.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class Family < BaseEnum
246246
value "KEYS" do
247247
description "Neither here nor there, really"
248248
end
249-
value "SILENCE", "Makes no sound", value: false, value_method: false
249+
value "SILENCE", "Makes no sound", value: false
250250
end
251251

252252
class InstrumentType < BaseObject

0 commit comments

Comments
 (0)