Class: GraphQL::Dataloader::AsyncDataloader

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

Defined Under Namespace

Classes: Run

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

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



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

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



140
141
142
143
# File 'lib/graphql/dataloader/async_dataloader.rb', line 140

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

#create_pending_runObject



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

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

#lazy_at_depth(depth, lazy) ⇒ Object



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

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

#run(trace_query_lazy: nil) ⇒ Object



187
188
189
190
191
192
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
# File 'lib/graphql/dataloader/async_dataloader.rb', line 187

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_pending_steps(run)
        run_sources(run)

        if !run.lazies_at_depth.empty?
          with_trace_query_lazy(trace_query_lazy) do
            run_next_pending_lazies(run)
            run_pending_steps(run)
          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



153
154
155
156
157
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
# File 'lib/graphql/dataloader/async_dataloader.rb', line 153

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|
      if !source_instance.results.key?(key)
        source_instance.pending[key] = value
      end
    end
  end
end

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



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphql/dataloader/async_dataloader.rb', line 27

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.finished_tasks.push(task)
  condition = task.graphql_async_dataloader_condition
  condition.wait
  run.started_tasks.push(task)
  trace&.dataloader_fiber_resume(source)
  nil
end