Skip to content

Commit d406d39

Browse files
committed
Use a fresh NullDataloader instance for run_isolated to support frozen instances
1 parent 889bcf6 commit d406d39

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/graphql/dataloader.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def run_isolated
163163
prev_pending_keys = {}
164164
prev_lazies_at_depth = @lazies_at_depth
165165
@lazies_at_depth = @lazies_at_depth.dup.clear
166+
# Clear pending loads but keep already-cached records
167+
# in case they are useful to the given block.
166168
@source_cache.each do |source_class, batched_sources|
167169
batched_sources.each do |batch_args, batched_source_instance|
168170
if batched_source_instance.pending?

lib/graphql/dataloader/null_dataloader.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ def run(trace_query_lazy: nil)
3737
end
3838

3939
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
40+
new_dl = self.class.new
41+
res = nil
42+
new_dl.append_job {
43+
res = yield
44+
}
45+
new_dl.run
46+
res
4547
end
4648

4749
def clear_cache; end
50+
4851
def yield(_source)
4952
raise GraphQL::Error, "GraphQL::Dataloader is not running -- add `use GraphQL::Dataloader` to your schema to use Dataloader sources."
5053
end

0 commit comments

Comments
 (0)