Skip to content
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
6 changes: 3 additions & 3 deletions lib/graphql/execution/interpreter/resolve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def self.resolve_each_depth(lazies_at_depth, dataloader)
if smallest_depth
lazies = lazies_at_depth.delete(smallest_depth)
if !lazies.empty?
dataloader.append_job {
lazies.each(&:value) # resolve these Lazy instances
}
lazies.each do |l|
dataloader.append_job { l.value }
end
# Run lazies _and_ dataloader, see if more are enqueued
dataloader.run
resolve_each_depth(lazies_at_depth, dataloader)
Expand Down
27 changes: 26 additions & 1 deletion spec/graphql/dataloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ def slow_ingredients
end
end

class Cookbook < GraphQL::Schema::Object
field :featured_recipe, Recipe

def featured_recipe
-> { Database.mget([object[:featured_recipe]]).first }
end
end

class Query < GraphQL::Schema::Object
field :recipes, [Recipe], null: false

Expand Down Expand Up @@ -357,11 +365,19 @@ class LookaheadInput < GraphQL::Schema::InputObject
argument :input, LookaheadInput
end


def lookahead_ingredient(input:, lookahead:)
lookahead.arguments # forces a dataloader.run_isolated call
dataloader.with(CustomBatchKeySource, input[:batch_key]).load(input[:id])
end

field :cookbooks, [Cookbook]

def cookbooks
[
{ featured_recipe: "5" },
{ featured_recipe: "6" },
]
end
end

query(Query)
Expand Down Expand Up @@ -434,6 +450,7 @@ def self.resolve_type(type, obj, ctx)

orphan_types(Grain, Dairy, Recipe, LeaveningAgent)
use GraphQL::Dataloader
lazy_resolve Proc, :call

class FieldTestError < StandardError; end

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

it "batches nested object calls in .authorized? after using lazy_resolve" do
query_str = "{ cookbooks { featuredRecipe { name } } }"
context = { batched_calls_counter: BatchedCallsCounter.new }
result = schema.execute(query_str, context: context)
refute result.key?("errors")
assert_equal 1, context[:batched_calls_counter].count
end

it "works when passing nil into source" do
query_str = <<-GRAPHQL
query($id: ID) {
Expand Down