Skip to content

Commit 889bcf6

Browse files
committed
Add Lazy resolution to NullDataloader and remove FlatDataloader
1 parent 2b9f736 commit 889bcf6

File tree

6 files changed

+42
-66
lines changed

6 files changed

+42
-66
lines changed

lib/graphql/dataloader.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "graphql/dataloader/flat_dataloader"
43
require "graphql/dataloader/null_dataloader"
54
require "graphql/dataloader/request"
65
require "graphql/dataloader/request_all"

lib/graphql/dataloader/flat_dataloader.rb

Lines changed: 0 additions & 54 deletions
This file was deleted.

lib/graphql/dataloader/null_dataloader.rb

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,48 @@
22

33
module GraphQL
44
class Dataloader
5-
# The default implementation of dataloading -- all no-ops.
5+
# GraphQL-Ruby uses this when Dataloader isn't enabled.
66
#
7-
# The Dataloader interface isn't public, but it enables
8-
# simple internal code while adding the option to add Dataloader.
7+
# It runs execution code inline and gathers lazy objects (eg. Promises)
8+
# and resolves them during {#run}.
99
class NullDataloader < Dataloader
10-
# These are all no-ops because code was
11-
# executed synchronously.
10+
def initialize(*)
11+
@lazies_at_depth = Hash.new { |h,k| h[k] = [] }
12+
end
13+
14+
def freeze
15+
@lazies_at_depth.default_proc = nil
16+
@lazies_at_depth.freeze
17+
super
18+
end
19+
20+
def run(trace_query_lazy: nil)
21+
with_trace_query_lazy(trace_query_lazy) do
22+
while !@lazies_at_depth.empty?
23+
smallest_depth = nil
24+
@lazies_at_depth.each_key do |depth_key|
25+
smallest_depth ||= depth_key
26+
if depth_key < smallest_depth
27+
smallest_depth = depth_key
28+
end
29+
end
30+
31+
if smallest_depth
32+
lazies = @lazies_at_depth.delete(smallest_depth)
33+
lazies.each(&:value) # resolve these Lazy instances
34+
end
35+
end
36+
end
37+
end
38+
39+
def run_isolated
40+
prev_lazies_at_depth = @lazies_at_depth
41+
@lazies_at_depth = @lazies_at_depth.dup.clear
42+
yield
43+
ensure
44+
@lazies_at_depth = prev_lazies_at_depth
45+
end
1246

13-
def initialize(*); end
14-
def run(trace_query_lazy: nil); end
15-
def run_isolated; yield; end
1647
def clear_cache; end
1748
def yield(_source)
1849
raise GraphQL::Error, "GraphQL::Dataloader is not running -- add `use GraphQL::Dataloader` to your schema to use Dataloader sources."

lib/graphql/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def union_memberships(type = nil)
671671
# @api private
672672
# @see GraphQL::Dataloader
673673
def dataloader_class
674-
@dataloader_class || GraphQL::Dataloader::FlatDataloader
674+
@dataloader_class || GraphQL::Dataloader::NullDataloader
675675
end
676676

677677
attr_writer :dataloader_class

spec/graphql/dataloader_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ class CanaryDataloader < GraphQL::Dataloader::NullDataloader
14511451
end
14521452
query(query_type)
14531453
end.execute("{ __typename }")
1454-
assert_instance_of GraphQL::Dataloader::FlatDataloader, res.context.dataloader
1454+
assert_instance_of GraphQL::Dataloader::NullDataloader, res.context.dataloader
14551455
res = FiberSchema.execute("{ __typename }")
14561456
assert_instance_of GraphQL::Dataloader, res.context.dataloader
14571457
refute res.context.dataloader.nonblocking?

spec/graphql/execution/interpreter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def execute_multiplex(multiplex:)
413413
# All of these get `3` as lazy value. They're resolved together,
414414
# since they aren't _root_ mutation fields.
415415
"lazyValue" => 3,
416-
"i2" => { "value" => 3, "incrementedValue" => 2, "lazyValue" => 3 },
416+
"i2" => { "value" => 2, "incrementedValue" => 2, "lazyValue" => 3 },
417417
"i3" => { "value" => 3, "incrementedValue" => 3, "lazyValue" => 3 },
418418
},
419419
"i4" => { "value" => 4, "incrementedValue" => 4, "lazyValue" => 4},

0 commit comments

Comments
 (0)