Method: GraphQL::Dataloader#run_isolated

Defined in:
lib/graphql/dataloader.rb

#run_isolatedObject

Use a self-contained queue for the work in the block.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/graphql/dataloader.rb', line 161

def run_isolated
  prev_queue = @pending_jobs
  prev_pending_keys = {}
  prev_lazies_at_depth = @lazies_at_depth
  @lazies_at_depth = @lazies_at_depth.dup.clear
  # Clear pending loads but keep already-cached records
  # in case they are useful to the given block.
  @source_cache.each do |source_class, batched_sources|
    batched_sources.each do |batch_args, batched_source_instance|
      if batched_source_instance.pending?
        prev_pending_keys[batched_source_instance] = batched_source_instance.pending.dup
        batched_source_instance.pending.clear
      end
    end
  end

  @pending_jobs = []
  res = nil
  # Make sure the block is inside a Fiber, so it can `Fiber.yield`
  append_job {
    res = yield
  }
  run
  res
ensure
  @pending_jobs = prev_queue
  @lazies_at_depth = prev_lazies_at_depth
  prev_pending_keys.each do |source_instance, pending|
    pending.each do |key, value|
      if !source_instance.results.key?(key)
        source_instance.pending[key] = value
      end
    end
  end
end