Skip to content

Commit 289d17b

Browse files
author
Robert Mosolgo
authored
Merge pull request #3457 from coding-chimp/source-keyword-arguments
Source keyword arguments
2 parents f7c0aa4 + 0947198 commit 289d17b

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

lib/graphql/dataloader.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ def self.use(schema)
2929

3030
def initialize
3131
@source_cache = Hash.new { |h, source_class| h[source_class] = Hash.new { |h2, batch_parameters|
32-
source = source_class.new(*batch_parameters)
32+
source = if RUBY_VERSION < "3"
33+
source_class.new(*batch_parameters)
34+
else
35+
batch_args, batch_kwargs = batch_parameters
36+
source_class.new(*batch_args, **batch_kwargs)
37+
end
3338
source.setup(self)
3439
h2[batch_parameters] = source
3540
}
@@ -43,8 +48,15 @@ def initialize
4348
# @param batch_parameters [Array<Object>]
4449
# @return [GraphQL::Dataloader::Source] An instance of {source_class}, initialized with `self, *batch_parameters`,
4550
# and cached for the lifetime of this {Multiplex}.
46-
def with(source_class, *batch_parameters)
47-
@source_cache[source_class][batch_parameters]
51+
if RUBY_VERSION < "3"
52+
def with(source_class, *batch_parameters)
53+
@source_cache[source_class][batch_parameters]
54+
end
55+
else
56+
def with(source_class, *batch_args, **batch_kwargs)
57+
batch_parameters = [batch_args, batch_kwargs]
58+
@source_cache[source_class][batch_parameters]
59+
end
4860
end
4961

5062
# Tell the dataloader that this fiber is waiting for data.

spec/graphql/dataloader_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ def fetch(keys)
6767
end
6868
end
6969

70+
class KeywordArgumentSource < GraphQL::Dataloader::Source
71+
def initialize(column:)
72+
@column = column
73+
end
74+
75+
def fetch(keys)
76+
if @column == :id
77+
Database.mget(keys)
78+
else
79+
Database.find_by(@column, keys)
80+
end
81+
end
82+
end
83+
7084
module Ingredient
7185
include GraphQL::Schema::Interface
7286
field :name, String, null: false
@@ -149,6 +163,14 @@ def recipe(recipe:)
149163
recipe
150164
end
151165

166+
field :key_ingredient, Ingredient, null: true do
167+
argument :id, ID, required: true
168+
end
169+
170+
def key_ingredient(id:)
171+
dataloader.with(KeywordArgumentSource, column: :id).load(id)
172+
end
173+
152174
field :recipe_ingredient, Ingredient, null: true do
153175
argument :recipe_id, ID, required: true
154176
argument :ingredient_number, Int, required: true
@@ -456,6 +478,26 @@ def database_log
456478
assert_equal expected_log, database_log
457479
end
458480

481+
it "works with sources that use keyword arguments in the initializer" do
482+
query_str = <<-GRAPHQL
483+
{
484+
keyIngredient(id: 1) {
485+
__typename
486+
name
487+
}
488+
}
489+
GRAPHQL
490+
491+
res = FiberSchema.execute(query_str)
492+
expected_data = {
493+
"keyIngredient" => {
494+
"__typename" => "Grain",
495+
"name" => "Wheat",
496+
}
497+
}
498+
assert_equal expected_data, res["data"]
499+
end
500+
459501
class UsageAnalyzer < GraphQL::Analysis::AST::Analyzer
460502
def initialize(query)
461503
@query = query

0 commit comments

Comments
 (0)