Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [2.7, 3.0, 3.1, 3.2, 3.3]
ruby: [2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 4.0]
graphql_version: ['~> 1.13', '~> 2.0']
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
Expand Down
7 changes: 4 additions & 3 deletions lib/graphql/batch/loader.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module GraphQL::Batch
class Loader
# Use new argument forwarding syntax if available as an optimization
if RUBY_ENGINE && Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7")
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def self.for(...)
current_executor.loader(loader_key_for(...)) { new(...) }
def self.for(*group_args, **group_kwargs, &block)
current_executor.loader(loader_key_for(*group_args, **group_kwargs)) do
new(*group_args, **group_kwargs, &block)
end
end
RUBY
else
Expand Down
36 changes: 36 additions & 0 deletions test/loader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ def perform(_keys)
end
end

class KwargsGroupCountLoader < GraphQL::Batch::Loader
def initialize(group, key: :id, preload: true, not_found_value: nil, **conditions)
end

def perform(keys)
keys.each { |key| fulfill(key, keys.size) }
end

# Mimics RecordLoader.load: destructures kwargs with defaults and **rest,
# then re-passes them to .for()
def self.load_via_intermediary(group, load_key, key: :id, preload: true, not_found_value: nil, **conditions)
self.for(group, key: key, preload: preload, not_found_value: not_found_value, **conditions).load(load_key)
end
end

def setup
GraphQL::Batch::Executor.current = GraphQL::Batch::Executor.new
end
Expand Down Expand Up @@ -92,6 +107,27 @@ def test_query_group
assert_equal [2, 1, 2], group.sync
end

# Reproduces a Ruby 4.0 bug where kwargs forwarded through ... get mutated
# when they were destructured and re-assembled by an intermediary method.
# Direct .for() calls don't trigger this; the kwargs must pass through a
# method that destructures them into named parameters (with defaults and
# **rest) then re-passes them to .for().
def test_query_group_with_kwargs_via_intermediary
group = Promise.all([
KwargsGroupCountLoader.load_via_intermediary('two', :a),
KwargsGroupCountLoader.load_via_intermediary('one', :a),
KwargsGroupCountLoader.load_via_intermediary('two', :b),
])
assert_equal [2, 1, 2], group.sync
end

def test_loader_key_not_mutated_by_for_via_intermediary
KwargsGroupCountLoader.load_via_intermediary('test', :a).sync
loader = GraphQL::Batch::Executor.current.instance_variable_get(:@loaders).values.first
expected_kwargs = { key: :id, preload: true, not_found_value: nil }
assert_equal expected_kwargs, loader.loader_key[1]
end

def test_query_many
assert_equal [:a, :b, :c], EchoLoader.load_many([:a, :b, :c]).sync
end
Expand Down
Loading