Skip to content

Commit

Permalink
Rename enum value_name: to value_method:, fix some test warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Jan 29, 2025
1 parent 3df9181 commit 52153a2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/graphql/schema/build_from_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def build_enum_type(enum_type_definition, type_resolver)
description: enum_value_definition.description,
directives: builder.prepare_directives(enum_value_definition, type_resolver),
ast_node: enum_value_definition,
value_method: GraphQL::Schema::Enum.respond_to?(enum_value_definition.name.downcase) ? false : nil,
)
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/graphql/schema/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class << self
# @option kwargs [::Object] :value the translated Ruby value for this object (defaults to `graphql_name`)
# @option kwargs [::Object] :value_method, the method name to fetch `graphql_name` (defaults to `graphql_name.downcase`)
# @option kwargs [String] :deprecation_reason if this object is deprecated, include a message here
# @param value_method [Symbol, false] A method to generate for this value, or `false` to skip generation
# @return [void]
# @see {Schema::EnumValue} which handles these inputs by default
def value(*args, value_method: nil, **kwargs, &block)
Expand Down Expand Up @@ -235,7 +236,8 @@ def generate_value_method(value, configured_value_method)

if respond_to?(value_method_name.to_sym)
warn "Failed to define value method for :#{value_method_name}, because " \
"#{value.owner.name} already responds to that method. Use `value_name:` to override the method name."
"#{value.owner.name || value.owner.graphql_name} already responds to that method. Use `value_method:` to override the method name " \
"or `value_method: false` to disable Enum value method generation."
return
end

Expand Down
10 changes: 5 additions & 5 deletions spec/graphql/dataloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,19 @@ class Mutation1 < GraphQL::Schema::Mutation
argument :argument_1, String, prepare: ->(val, ctx) {
raise FieldTestError
}

field :value, String
def resolve(argument_1:)
argument_1
{ value: argument_1 }
end
end

class Mutation2 < GraphQL::Schema::Mutation
argument :argument_2, String, prepare: ->(val, ctx) {
raise FieldTestError
}

field :value, String
def resolve(argument_2:)
argument_2
{ value: argument_2 }
end
end

Expand Down Expand Up @@ -1389,7 +1389,7 @@ def request_all

it "has proper context[:current_field]" do
res = FiberSchema.execute("mutation { mutation1(argument1: \"abc\") { __typename } mutation2(argument2: \"def\") { __typename } }")
assert_equal({"mutation1"=>nil, "mutation2"=>nil}, res["data"])
assert_equal({"mutation1"=>{ "__typename" => "Mutation1Payload" }, "mutation2"=>{ "__typename" => "Mutation2Payload"} }, res["data"])
expected_errors = [
"FieldTestError @ [\"mutation1\"], Mutation.mutation1 / Mutation.mutation1",
"FieldTestError @ [\"mutation2\"], Mutation.mutation2 / Mutation.mutation2",
Expand Down
4 changes: 2 additions & 2 deletions spec/graphql/schema/dynamic_members_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class Thing < LegacyThing

class Language < BaseEnum
value "RUBY"
value "PERL6", deprecation_reason: "Use RAKU instead", future_schema: true
value "PERL6", deprecation_reason: "Use RAKU instead", future_schema: true, value_method: false
value "PERL6", future_schema: false
value "RAKU", future_schema: true
value "COFFEE_SCRIPT", future_schema: false
Expand Down Expand Up @@ -1026,7 +1026,7 @@ class BaseEnumValue < GraphQL::Schema::EnumValue
class DuplicateEnumValue < GraphQL::Schema::Enum
enum_value_class(BaseEnumValue)
value "ONE", description: "second definition", allow_for: [2, 3]
value "ONE", description: "first definition", allow_for: [1, 2]
value "ONE", description: "first definition", allow_for: [1, 2], value_method: false
end

class DuplicateFieldObject < GraphQL::Schema::Object
Expand Down
6 changes: 2 additions & 4 deletions spec/graphql/schema/enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@

describe "when value_method conflicts with existing method" do
it "does not define method and emits warning" do
expected_message = "Failed to define value method for :value, because " \
"ConflictEnum already responds to that method. Use `value_name:` to override the method name.\n"

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"
assert_warns(expected_message) do
conflict_enum = Class.new(GraphQL::Schema::Enum)
Object.const_set("ConflictEnum", conflict_enum)
Expand Down Expand Up @@ -148,7 +146,7 @@ def value
class MultipleNameTestEnum < GraphQL::Schema::Enum
value "A"
value "B", value: :a
value "B", value: :b
value "B", value: :b, value_method: false
end

it "doesn't allow it from enum_values" do
Expand Down
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

end

require "undercover"

Bundler.require

# Print full backtrace for failures:
Expand Down

0 comments on commit 52153a2

Please sign in to comment.