Skip to content

Commit

Permalink
undef the methods as late as possible and scope it to the object only
Browse files Browse the repository at this point in the history
  • Loading branch information
ElvinEfendi committed Mar 1, 2024
1 parent b7b12e6 commit 06f8483
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lib/graphql/client/schema/object_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def self.new(type, fields = {})

const_set(:READERS, {})
const_set(:PREDICATES, {})
const_set(:METHODS_TO_UNDEF, [])
end
end

Expand All @@ -27,8 +28,7 @@ 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?]*$/ }
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
Expand Down Expand Up @@ -62,8 +62,8 @@ def initialize(klass, defined_fields, definition, spreads)
@klass::READERS[:"#{name}"] ||= attr
@klass::PREDICATES[:"#{name}?"] ||= attr

@klass.undef_method(name) if BASE_RUBY_METHODS.include?(name) && @klass.method_defined?(name)
@klass.undef_method("#{name}?") if BASE_RUBY_METHODS.include?("#{name}?") && @klass.method_defined?("#{name}?")
@klass::METHODS_TO_UNDEF << name if BASE_RUBY_METHODS.include?(name)
@klass::METHODS_TO_UNDEF << "#{name}?" if BASE_RUBY_METHODS.include?("#{name}?")
end
end

Expand Down Expand Up @@ -190,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
Expand Down
9 changes: 8 additions & 1 deletion test/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,11 @@ def method
def equal
"equal"
end

field :test, String, null: false
def test
"test"
end
end

schema = Class.new(GraphQL::Schema) do
Expand All @@ -864,12 +869,14 @@ def equal

@client = ::GraphQL::Client.new(schema: schema, execute: schema)

Temp.const_set :Query, @client.parse("{ method things equal }")
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

0 comments on commit 06f8483

Please sign in to comment.