Skip to content

Commit 78bd98f

Browse files
committed
Clean up lazies_at_depth, DRY FlatDataloader
1 parent 3493129 commit 78bd98f

File tree

5 files changed

+10
-39
lines changed

5 files changed

+10
-39
lines changed

lib/graphql/dataloader.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,9 @@ def initialize(nonblocking: self.class.default_nonblocking, fiber_limit: self.cl
6565
@nonblocking = nonblocking
6666
end
6767
@fiber_limit = fiber_limit
68-
@lazies_at_depth = nil
68+
@lazies_at_depth = Hash.new { |h, k| h[k] = [] }
6969
end
7070

71-
attr_accessor :lazies_at_depth
72-
7371
# @return [Integer, nil]
7472
attr_reader :fiber_limit
7573

@@ -164,6 +162,8 @@ def clear_cache
164162
def run_isolated
165163
prev_queue = @pending_jobs
166164
prev_pending_keys = {}
165+
prev_lazies_at_depth = @lazies_at_depth
166+
@lazies_at_depth = @lazies_at_depth.dup.clear
167167
@source_cache.each do |source_class, batched_sources|
168168
batched_sources.each do |batch_args, batched_source_instance|
169169
if batched_source_instance.pending?
@@ -183,6 +183,7 @@ def run_isolated
183183
res
184184
ensure
185185
@pending_jobs = prev_queue
186+
@lazies_at_depth = prev_lazies_at_depth
186187
prev_pending_keys.each do |source_instance, pending|
187188
pending.each do |key, value|
188189
if !source_instance.results.key?(key)
@@ -194,8 +195,6 @@ def run_isolated
194195

195196
# @param trace_query_lazy [nil, Execution::Multiplex]
196197
def run(trace_query_lazy: nil)
197-
# TODO unify the initialization lazies_at_depth
198-
@lazies_at_depth ||= Hash.new { |h, k| h[k] = [] }
199198
trace = Fiber[:__graphql_current_multiplex]&.current_trace
200199
jobs_fiber_limit, total_fiber_limit = calculate_fiber_limit
201200
job_fibers = []

lib/graphql/dataloader/async_dataloader.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ def yield(source = Fiber[:__graphql_current_dataloader_source])
1515
end
1616

1717
def run(trace_query_lazy: nil)
18-
# TODO unify the initialization lazies_at_depth
19-
@lazies_at_depth ||= Hash.new { |h, k| h[k] = [] }
2018
trace = Fiber[:__graphql_current_multiplex]&.current_trace
2119
jobs_fiber_limit, total_fiber_limit = calculate_fiber_limit
2220
job_fibers = []

lib/graphql/dataloader/flat_dataloader.rb

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,24 @@
22
module GraphQL
33
class Dataloader
44
class FlatDataloader < Dataloader
5-
def initialize(*)
6-
# TODO unify the initialization lazies_at_depth
7-
@lazies_at_depth ||= Hash.new { |h, k| h[k] = [] }
8-
@queue = []
9-
end
10-
115
def run(trace_query_lazy: nil)
12-
while !@queue.empty?
6+
while !@pending_jobs.empty?
137
run_pending_steps
148
with_trace_query_lazy(trace_query_lazy) do
15-
while @lazies_at_depth&.any?
9+
while !@lazies_at_depth.empty?
1610
run_next_pending_lazies
1711
run_pending_steps
1812
end
1913
end
2014
end
2115
end
2216

23-
def run_isolated
24-
prev_queue = @queue
25-
prev_lad = @lazies_at_depth
26-
@queue = []
27-
@lazies_at_depth = @lazies_at_depth.dup&.clear
28-
res = nil
29-
append_job {
30-
res = yield
31-
}
32-
run
33-
res
34-
ensure
35-
@queue = prev_queue
36-
@lazies_at_depth = prev_lad
37-
end
38-
39-
def clear_cache; end
40-
4117
def yield(_source)
4218
raise GraphQL::Error, "GraphQL::Dataloader is not running -- add `use GraphQL::Dataloader` to your schema to use Dataloader sources."
4319
end
4420

4521
def append_job(callable = nil, &block)
46-
@queue << (callable || block)
22+
@pending_jobs << (callable || block)
4723
nil
4824
end
4925

@@ -69,7 +45,7 @@ def run_next_pending_lazies
6945
end
7046

7147
def run_pending_steps
72-
while (step = @queue.shift)
48+
while (step = @pending_jobs.shift)
7349
step.call
7450
end
7551
end

lib/graphql/execution/interpreter.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ def run_all(schema, query_options, context: {}, max_complexity: schema.max_compl
4141
trace.execute_multiplex(multiplex: multiplex) do
4242
schema = multiplex.schema
4343
queries = multiplex.queries
44-
lazies_at_depth = Hash.new { |h, k| h[k] = [] }
45-
multiplex.dataloader.lazies_at_depth = lazies_at_depth
4644
multiplex_analyzers = schema.multiplex_analyzers
4745
if multiplex.max_complexity
4846
multiplex_analyzers += [GraphQL::Analysis::MaxQueryComplexity]
@@ -73,7 +71,7 @@ def run_all(schema, query_options, context: {}, max_complexity: schema.max_compl
7371
# Although queries in a multiplex _share_ an Interpreter instance,
7472
# they also have another item of state, which is private to that query
7573
# in particular, assign it here:
76-
runtime = Runtime.new(query: query, lazies_at_depth: lazies_at_depth)
74+
runtime = Runtime.new(query: query)
7775
query.context.namespace(:interpreter_runtime)[:runtime] = runtime
7876

7977
query.current_trace.execute_query(query: query) do

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def current_object
3535
# @return [GraphQL::Query::Context]
3636
attr_reader :context
3737

38-
def initialize(query:, lazies_at_depth:)
38+
def initialize(query:)
3939
@query = query
4040
@current_trace = query.current_trace
4141
@dataloader = query.multiplex.dataloader

0 commit comments

Comments
 (0)