@@ -182,6 +182,14 @@ def slow_ingredients
182
182
end
183
183
end
184
184
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
+
185
193
class Query < GraphQL ::Schema ::Object
186
194
field :recipes , [ Recipe ] , null : false
187
195
@@ -357,11 +365,19 @@ class LookaheadInput < GraphQL::Schema::InputObject
357
365
argument :input , LookaheadInput
358
366
end
359
367
360
-
361
368
def lookahead_ingredient ( input :, lookahead :)
362
369
lookahead . arguments # forces a dataloader.run_isolated call
363
370
dataloader . with ( CustomBatchKeySource , input [ :batch_key ] ) . load ( input [ :id ] )
364
371
end
372
+
373
+ field :cookbooks , [ Cookbook ]
374
+
375
+ def cookbooks
376
+ [
377
+ { featured_recipe : "5" } ,
378
+ { featured_recipe : "6" } ,
379
+ ]
380
+ end
365
381
end
366
382
367
383
query ( Query )
@@ -434,6 +450,7 @@ def self.resolve_type(type, obj, ctx)
434
450
435
451
orphan_types ( Grain , Dairy , Recipe , LeaveningAgent )
436
452
use GraphQL ::Dataloader
453
+ lazy_resolve Proc , :call
437
454
438
455
class FieldTestError < StandardError ; end
439
456
@@ -919,6 +936,14 @@ def self.included(child_class)
919
936
assert_equal 1 , context [ :batched_calls_counter ] . count
920
937
end
921
938
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
+
922
947
it "works when passing nil into source" do
923
948
query_str = <<-GRAPHQL
924
949
query($id: ID) {
0 commit comments