Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source keyword arguments #3457

Merged
merged 1 commit into from
Apr 30, 2021
Merged
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
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