Skip to content

Commit

Permalink
Merge pull request #3457 from coding-chimp/source-keyword-arguments
Browse files Browse the repository at this point in the history
Source keyword arguments
  • Loading branch information
Robert Mosolgo authored Apr 30, 2021
2 parents f7c0aa4 + 0947198 commit 289d17b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/graphql/dataloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ def self.use(schema)

def initialize
@source_cache = Hash.new { |h, source_class| h[source_class] = Hash.new { |h2, batch_parameters|
source = source_class.new(*batch_parameters)
source = if RUBY_VERSION < "3"
source_class.new(*batch_parameters)
else
batch_args, batch_kwargs = batch_parameters
source_class.new(*batch_args, **batch_kwargs)
end
source.setup(self)
h2[batch_parameters] = source
}
Expand All @@ -43,8 +48,15 @@ def initialize
# @param batch_parameters [Array<Object>]
# @return [GraphQL::Dataloader::Source] An instance of {source_class}, initialized with `self, *batch_parameters`,
# and cached for the lifetime of this {Multiplex}.
def with(source_class, *batch_parameters)
@source_cache[source_class][batch_parameters]
if RUBY_VERSION < "3"
def with(source_class, *batch_parameters)
@source_cache[source_class][batch_parameters]
end
else
def with(source_class, *batch_args, **batch_kwargs)
batch_parameters = [batch_args, batch_kwargs]
@source_cache[source_class][batch_parameters]
end
end

# Tell the dataloader that this fiber is waiting for data.
Expand Down
42 changes: 42 additions & 0 deletions spec/graphql/dataloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ def fetch(keys)
end
end

class KeywordArgumentSource < GraphQL::Dataloader::Source
def initialize(column:)
@column = column
end

def fetch(keys)
if @column == :id
Database.mget(keys)
else
Database.find_by(@column, keys)
end
end
end

module Ingredient
include GraphQL::Schema::Interface
field :name, String, null: false
Expand Down Expand Up @@ -149,6 +163,14 @@ def recipe(recipe:)
recipe
end

field :key_ingredient, Ingredient, null: true do
argument :id, ID, required: true
end

def key_ingredient(id:)
dataloader.with(KeywordArgumentSource, column: :id).load(id)
end

field :recipe_ingredient, Ingredient, null: true do
argument :recipe_id, ID, required: true
argument :ingredient_number, Int, required: true
Expand Down Expand Up @@ -456,6 +478,26 @@ def database_log
assert_equal expected_log, database_log
end

it "works with sources that use keyword arguments in the initializer" do
query_str = <<-GRAPHQL
{
keyIngredient(id: 1) {
__typename
name
}
}
GRAPHQL

res = FiberSchema.execute(query_str)
expected_data = {
"keyIngredient" => {
"__typename" => "Grain",
"name" => "Wheat",
}
}
assert_equal expected_data, res["data"]
end

class UsageAnalyzer < GraphQL::Analysis::AST::Analyzer
def initialize(query)
@query = query
Expand Down

0 comments on commit 289d17b

Please sign in to comment.