Skip to content

Commit 9442377

Browse files
committed
Support dataloader calls after batched lazy resolve
1 parent 4a72a82 commit 9442377

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/graphql/execution/interpreter/resolve.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def self.resolve_each_depth(lazies_at_depth, dataloader)
2323
if smallest_depth
2424
lazies = lazies_at_depth.delete(smallest_depth)
2525
if !lazies.empty?
26-
dataloader.append_job {
27-
lazies.each(&:value) # resolve these Lazy instances
28-
}
26+
lazies.each do |l|
27+
dataloader.append_job { l.value }
28+
end
2929
# Run lazies _and_ dataloader, see if more are enqueued
3030
dataloader.run
3131
resolve_each_depth(lazies_at_depth, dataloader)

spec/graphql/dataloader_spec.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ def slow_ingredients
182182
end
183183
end
184184

185+
class Cookbook < GraphQL::Schema::Object
186+
field :featured_recipe, Recipe
187+
188+
def featured_recipe
189+
-> { Database.mget([object[:featured_recipe]]).first }
190+
end
191+
end
192+
185193
class Query < GraphQL::Schema::Object
186194
field :recipes, [Recipe], null: false
187195

@@ -357,11 +365,19 @@ class LookaheadInput < GraphQL::Schema::InputObject
357365
argument :input, LookaheadInput
358366
end
359367

360-
361368
def lookahead_ingredient(input:, lookahead:)
362369
lookahead.arguments # forces a dataloader.run_isolated call
363370
dataloader.with(CustomBatchKeySource, input[:batch_key]).load(input[:id])
364371
end
372+
373+
field :cookbooks, [Cookbook]
374+
375+
def cookbooks
376+
[
377+
{ featured_recipe: "5" },
378+
{ featured_recipe: "6" },
379+
]
380+
end
365381
end
366382

367383
query(Query)
@@ -434,6 +450,7 @@ def self.resolve_type(type, obj, ctx)
434450

435451
orphan_types(Grain, Dairy, Recipe, LeaveningAgent)
436452
use GraphQL::Dataloader
453+
lazy_resolve Proc, :call
437454

438455
class FieldTestError < StandardError; end
439456

@@ -919,6 +936,14 @@ def self.included(child_class)
919936
assert_equal 1, context[:batched_calls_counter].count
920937
end
921938

939+
it "batches nested object calls in .authorized? after using lazy_resolve" do
940+
query_str = "{ cookbooks { featuredRecipe { name } } }"
941+
context = { batched_calls_counter: BatchedCallsCounter.new }
942+
result = schema.execute(query_str, context: context)
943+
refute result.key?("errors")
944+
assert_equal 1, context[:batched_calls_counter].count
945+
end
946+
922947
it "works when passing nil into source" do
923948
query_str = <<-GRAPHQL
924949
query($id: ID) {

0 commit comments

Comments
 (0)