diff --git a/lib/graphql/schema/build_from_definition.rb b/lib/graphql/schema/build_from_definition.rb index 7db47a30a3..350f7faa0d 100644 --- a/lib/graphql/schema/build_from_definition.rb +++ b/lib/graphql/schema/build_from_definition.rb @@ -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 diff --git a/lib/graphql/schema/enum.rb b/lib/graphql/schema/enum.rb index 2129c7cf26..7459579e94 100644 --- a/lib/graphql/schema/enum.rb +++ b/lib/graphql/schema/enum.rb @@ -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) @@ -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 diff --git a/spec/graphql/dataloader_spec.rb b/spec/graphql/dataloader_spec.rb index 3b1d1d8091..005fbd3167 100644 --- a/spec/graphql/dataloader_spec.rb +++ b/spec/graphql/dataloader_spec.rb @@ -370,9 +370,9 @@ 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 @@ -380,9 +380,9 @@ 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 @@ -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", diff --git a/spec/graphql/schema/dynamic_members_spec.rb b/spec/graphql/schema/dynamic_members_spec.rb index a1bd6dd258..19a9903f4e 100644 --- a/spec/graphql/schema/dynamic_members_spec.rb +++ b/spec/graphql/schema/dynamic_members_spec.rb @@ -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 @@ -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 diff --git a/spec/graphql/schema/enum_spec.rb b/spec/graphql/schema/enum_spec.rb index 0a7c46053a..c07b3a5d4b 100644 --- a/spec/graphql/schema/enum_spec.rb +++ b/spec/graphql/schema/enum_spec.rb @@ -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) @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1e25e65d27..6412a04576 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,8 +18,6 @@ end -require "undercover" - Bundler.require # Print full backtrace for failures: