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

#initializeAsyncDataloader

Returns a new instance of AsyncDataloader.



17
18
19
20
# File 'lib/graphql/dataloader/async_dataloader.rb', line 17

def initialize(...)
  super
  create_pending_run
end

Instance Attribute Details

#pending_sourcesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def pending_sources
  @pending_sources
end

Class Method Details

.useObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/graphql/dataloader/async_dataloader.rb', line 6

def self.use(...)
  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
  super
end

Instance Method Details

#active_runObject



154
155
156
# File 'lib/graphql/dataloader/async_dataloader.rb', line 154

def active_run
  @pending_run || Async::Task.current?&.graphql_async_dataloader_run || raise(GraphQL::Error, "No available Run to append to, GraphQL-Ruby bug")
end

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



145
146
147
148
# File 'lib/graphql/dataloader/async_dataloader.rb', line 145

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

#create_pending_runObject



25
26
27
28
# File 'lib/graphql/dataloader/async_dataloader.rb', line 25

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

#lazy_at_depth(depth, lazy) ⇒ Object



150
151
152
# File 'lib/graphql/dataloader/async_dataloader.rb', line 150

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

#run(trace_query_lazy: nil) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
# File 'lib/graphql/dataloader/async_dataloader.rb', line 193

def run(trace_query_lazy: nil)
  trace = Fiber[:__graphql_current_multiplex]&.current_trace
  run = @pending_run || Async::Task.current?&.graphql_async_dataloader_run || raise(GraphQL::Error, "No available Run, GraphQL-Ruby internal bug")
  @pending_run = nil
  run.trace = trace
  first_pass = true
  trace&.begin_dataloader(self)
  fiber_vars = get_fiber_variables
  raised_error = nil
  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
      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_isolatedObject



158
159
160
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
# File 'lib/graphql/dataloader/async_dataloader.rb', line 158

def run_isolated
  previous_run = Async::Task.current?&.graphql_async_dataloader_run
  prev_pending_keys = {}
  # 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
  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.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



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/graphql/dataloader/async_dataloader.rb', line 30

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)
  run.tasks_channel.push([:paused_task, task])
  condition = task.graphql_async_dataloader_condition
  condition.wait
  run.tasks_channel.push([:resumed_task, task])
  trace&.dataloader_fiber_resume(source)
  nil
end