diff --git a/lib/graphql/client/schema/object_type.rb b/lib/graphql/client/schema/object_type.rb index 4233f19..53881cf 100644 --- a/lib/graphql/client/schema/object_type.rb +++ b/lib/graphql/client/schema/object_type.rb @@ -19,6 +19,7 @@ def self.new(type, fields = {}) const_set(:READERS, {}) const_set(:PREDICATES, {}) + const_set(:METHODS_TO_UNDEF, []) end end @@ -27,6 +28,8 @@ class WithDefinition include ObjectType EMPTY_SET = Set.new.freeze + BASE_RUBY_METHODS = (Object.methods + Kernel.methods).uniq.filter { |m| m.to_s =~ /^[a-z_][a-z_0-9?]*$/ } + .map(&:to_s).to_set.freeze attr_reader :klass, :defined_fields, :definition @@ -58,6 +61,9 @@ def initialize(klass, defined_fields, definition, spreads) name = ActiveSupport::Inflector.underscore(attr) @klass::READERS[:"#{name}"] ||= attr @klass::PREDICATES[:"#{name}?"] ||= attr + + @klass::METHODS_TO_UNDEF << name if BASE_RUBY_METHODS.include?(name) + @klass::METHODS_TO_UNDEF << "#{name}?" if BASE_RUBY_METHODS.include?("#{name}?") end end @@ -184,6 +190,12 @@ def initialize(data = {}, errors = Errors.new, definer = nil) @definer = definer @enforce_collocated_callers = source_definition && source_definition.client.enforce_collocated_callers + + self.class::METHODS_TO_UNDEF.each do |method_name| + begin + singleton_class.undef_method(method_name) + rescue; end + end end # Public: Returns the raw response data diff --git a/test/test_client.rb b/test/test_client.rb index da38ff7..d255c9e 100644 --- a/test/test_client.rb +++ b/test/test_client.rb @@ -838,4 +838,45 @@ def test_spread_definition_defined_by_other_client } GRAPHQL end + + def test_field_names_that_match_ruby_built_in_methods + query_type = Class.new(GraphQL::Schema::Object) do + graphql_name "Query" + field :things, String, null: false + def things + "things" + end + + field :method, String, null: false + def method + "method" + end + + field :equal, String, null: false + def equal + "equal" + end + + field :test, String, null: false + def test + "test" + end + end + + schema = Class.new(GraphQL::Schema) do + query query_type + end + + @client = ::GraphQL::Client.new(schema: schema, execute: schema) + + Temp.const_set :Query, @client.parse("{ method things equal test }") + + result = @client.query(Temp::Query) + + assert_equal "method", result.data.method + assert_equal "things", result.data.things + assert_equal "equal", result.data.equal + assert_predicate result.data, :equal? + assert_equal "test", result.data.test + end end