Class: GraphQL::Dataloader::AsyncDataloader

Inherits:
Dataloader
  • Object
show all
Defined in:
lib/graphql/dataloader/async_dataloader.rb

Defined Under Namespace

Classes: Run

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ AsyncDataloader

Returns a new instance of AsyncDataloader.



23
24
25
26
# File 'lib/graphql/dataloader/async_dataloader.rb', line 23

def initialize(...)
  super
  create_pending_run
end

Instance Attribute Details

#pending_sources ⇒ Object (readonly)

:nodoc:



28
29
30
# File 'lib/graphql/dataloader/async_dataloader.rb', line 28

def pending_sources
  @pending_sources
end

Class Method Details

.install_graphql_methods ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/graphql/dataloader/async_dataloader.rb', line 13

def self.install_graphql_methods
  if !Async::Task.method_defined?(:cancel)
    Async::Task.alias_method(:cancel, :stop)
  end
  if !Async::Task.method_defined?(:graphql_async_dataloader_run)
    Async::Task.attr_accessor(:graphql_async_dataloader_run)
    Async::Task.attr_accessor(:graphql_async_dataloader_condition)
  end
end

.use ⇒ Object



8
9
10
11
# File 'lib/graphql/dataloader/async_dataloader.rb', line 8

def self.use(...)
  install_graphql_methods
  super
end

Instance Method Details

#active_run ⇒ Object



196
197
198
# File 'lib/graphql/dataloader/async_dataloader.rb', line 196

def active_run
  @pending_run || current_task_run || raise(GraphQL::Error, "No available Run to append to, GraphQL-Ruby bug")
end

#append_job(callable = nil, &block) ⇒ Object



187
188
189
190
# File 'lib/graphql/dataloader/async_dataloader.rb', line 187

def append_job(callable = nil, &block)
  active_run.jobs.push(callable || block)
  nil
end

#create_pending_run ⇒ Object



30
31
32
33
# File 'lib/graphql/dataloader/async_dataloader.rb', line 30

def create_pending_run
  jobs_fiber_limit, total_fiber_limit = calculate_fiber_limit
  @pending_run = Run.new(self, total_fiber_limit, jobs_fiber_limit)
end

#current_task_run ⇒ Object

The current task's run, but only if it belongs to this dataloader. A different dataloader may be running inside one of our tasks (or vice versa), e.g. a query executed from a resolver or a subscription trigger; its run must not be reused.



203
204
205
206
# File 'lib/graphql/dataloader/async_dataloader.rb', line 203

def current_task_run
  run = Async::Task.current?&.graphql_async_dataloader_run
  run if run&.dataloader.equal?(self)
end

#lazy_at_depth(depth, lazy) ⇒ Object



192
193
194
# File 'lib/graphql/dataloader/async_dataloader.rb', line 192

def lazy_at_depth(depth, lazy)
  active_run.lazies_at_depth[depth] << lazy
end

#run(trace_query_lazy: nil) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/graphql/dataloader/async_dataloader.rb', line 244

def run(trace_query_lazy: nil)
  trace = Fiber[:__graphql_current_multiplex]&.current_trace
  run = @pending_run || current_task_run || raise(GraphQL::Error, "No available Run, GraphQL-Ruby internal bug")
  @pending_run = nil
  run.trace = trace
  first_pass = true #: bool
  trace&.begin_dataloader(self)
  fiber_vars = get_fiber_variables
  raised_error = nil #: StandardError?
  jobs = run.jobs
  Sync do |_maybe_new_task|
    # Make sure there's a new task instance to hold `.graphql_...` state:
    task = Async::Task.new do |root_task|
      run.root_task = root_task
      root_task.graphql_async_dataloader_run = run
      # Tasks inherit the spawning fiber's storage; give this run its own execution state.
      Fiber[:__graphql_runtime_info] = nil
      set_fiber_variables(fiber_vars)

      while first_pass || run.running? || !jobs.empty?
        first_pass = false
        run_queue(run, run.snoozed_jobs_condition, :jobs)
        run_queue(run, run.snoozed_sources_condition, :sources)

        if !run.lazies_at_depth.empty?
          with_trace_query_lazy(trace_query_lazy) do
            if enqueue_next_pending_lazies(run.lazies_at_depth)
              run_queue(run, run.snoozed_jobs_condition, :jobs)
            end
          end
        end
      end
    rescue StandardError => err
      raised_error = err
      root_task.cancel
    end

    task.run
    task.wait
  end
  create_pending_run
  if raised_error
    raise raised_error
  end
  trace&.end_dataloader(self)
rescue UncaughtThrowError => e
  throw e.tag, e.value
end

#run_isolated ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/graphql/dataloader/async_dataloader.rb', line 208

def run_isolated
  previous_run = current_task_run
  prev_pending_keys = {} #: Hash[GraphQL::Dataloader::Source, Hash[untyped, untyped]]
  # 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

  res = nil #: untyped
  create_pending_run
  @pending_run.jobs << -> { res = yield }
  run
  res
ensure
  if previous_run
    Async::Task.current.graphql_async_dataloader_run = previous_run
    # clear the one created in #run:
    @pending_run = nil
  end
  prev_pending_keys = prev_pending_keys #: as !nil
  prev_pending_keys.each do |source_instance, pending|
    pending.each do |key, value|
      next if source_instance.results.key?(key)

      queue_pending_source(source_instance) if source_instance.pending.empty?
      source_instance.pending[key] = value
    end
  end
end

#yield(source = Fiber[:__graphql_current_dataloader_source]) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/graphql/dataloader/async_dataloader.rb', line 35

def yield(source = Fiber[:__graphql_current_dataloader_source])
  task = Async::Task.current
  run = task.graphql_async_dataloader_run
  trace = run.trace
  trace&.dataloader_fiber_yield(source)
  if !run.push_task_message(:paused_task, task)
    task.stop
  end
  condition = task.graphql_async_dataloader_condition
  condition.wait
  if !run.push_task_message(:resumed_task, task)
    task.stop
  end
  trace&.dataloader_fiber_resume(source)
  nil
end