Class: GraphQL::Dataloader
- Inherits:
-
Object
- Object
- GraphQL::Dataloader
- Defined in:
- lib/graphql/dataloader.rb,
lib/graphql/dataloader/source.rb,
lib/graphql/dataloader/request.rb,
lib/graphql/dataloader/request_all.rb,
lib/graphql/dataloader/null_dataloader.rb
Overview
This plugin supports Fiber-based concurrency, along with Source.
Direct Known Subclasses
Defined Under Namespace
Classes: NullDataloader, Request, RequestAll, Source
Instance Attribute Summary collapse
-
#context ⇒ Hash
readonly
The Multiplex context.
-
#current_runtime ⇒ Object
private
-
#yielded_fibers ⇒ Object
readonly
private
Class Method Summary collapse
Instance Method Summary collapse
-
#enqueue(&block) ⇒ void
Add some work to this dataloader to be scheduled later.
-
#initialize(multiplex_context) ⇒ Dataloader
constructor
A new instance of Dataloader.
-
#run ⇒ void
Run all Fibers until they’re all done.
-
#with(source_class, *batch_parameters) ⇒ GraphQL::Dataloader::Source
Get a Source instance from this dataloader, for calling
.load(...)
or.request(...)
on. -
#yield ⇒ void
Tell the dataloader that this fiber is waiting for data.
-
#yielded?(path) ⇒ Boolean
True if the current Fiber has yielded once via Dataloader at path.
Constructor Details
#initialize(multiplex_context) ⇒ Dataloader
Returns a new instance of Dataloader.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/graphql/dataloader.rb', line 30 def initialize(multiplex_context) @context = multiplex_context @source_cache = Hash.new { |h, source_class| h[source_class] = Hash.new { |h2, batch_parameters| source = source_class.new(*batch_parameters) source.setup(self) h2[batch_parameters] = source } } @waiting_fibers = [] @yielded_fibers = {} end |
Instance Attribute Details
#context ⇒ Hash (readonly)
Returns the Multiplex context.
43 44 45 |
# File 'lib/graphql/dataloader.rb', line 43 def context @context end |
#current_runtime ⇒ Object
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.
144 145 146 |
# File 'lib/graphql/dataloader.rb', line 144 def current_runtime @current_runtime end |
#yielded_fibers ⇒ Object (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.
46 47 48 |
# File 'lib/graphql/dataloader.rb', line 46 def yielded_fibers @yielded_fibers end |
Class Method Details
.use(schema) ⇒ Object
26 27 28 |
# File 'lib/graphql/dataloader.rb', line 26 def self.use(schema) schema.dataloader_class = self end |
Instance Method Details
#enqueue(&block) ⇒ void
This method returns an undefined value.
Add some work to this dataloader to be scheduled later.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/graphql/dataloader.rb', line 51 def enqueue(&block) @waiting_fibers << Fiber.new { begin yield rescue StandardError => exception exception end } nil end |
#run ⇒ void
This method returns an undefined value.
Run all Fibers until they’re all done
Each cycle works like this:
- Run each pending execution fiber (
@waiting_fibers
), - Then run each pending Source, preparing more data for those fibers.
- Run each pending Source again (if one Source requested more data from another Source)
- Continue until there are no pending sources
- Repeat: run execution fibers again …
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/graphql/dataloader.rb', line 89 def run # Start executing Fibers. This will run until all the Fibers are done. already_run_fibers = [] while (current_fiber = @waiting_fibers.pop) # Run each execution fiber, enqueuing it in `already_run_fibers` # if it's still `.alive?`. # Any spin-off continuations will be enqueued in `@waiting_fibers` (via {#enqueue}) resume_fiber_and_enqueue_continuation(current_fiber, already_run_fibers) if @waiting_fibers.empty? # Now, run all Sources which have become pending _before_ resuming GraphQL execution. # Sources might queue up other Sources, which is fine -- those will also run before resuming execution. # # This is where an evented approach would be even better -- can we tell which # fibers are ready to continue, and continue execution there? # source_fiber_stack = if (first_source_fiber = create_source_fiber) [first_source_fiber] else nil end if source_fiber_stack while (outer_source_fiber = source_fiber_stack.pop) resume_fiber_and_enqueue_continuation(outer_source_fiber, source_fiber_stack) # If this source caused more sources to become pending, run those before running this one again: next_source_fiber = create_source_fiber if next_source_fiber source_fiber_stack << next_source_fiber end end end # We ran all the first round of execution fibers, # and we ran all the pending sources. # So pick up any paused execution fibers and repeat. @waiting_fibers.concat(already_run_fibers) already_run_fibers.clear end end nil end |
#with(source_class, *batch_parameters) ⇒ GraphQL::Dataloader::Source
Get a Source instance from this dataloader, for calling .load(...)
or .request(...)
on.
139 140 141 |
# File 'lib/graphql/dataloader.rb', line 139 def with(source_class, *batch_parameters) @source_cache[source_class][batch_parameters] end |
#yield ⇒ void
This method returns an undefined value.
Tell the dataloader that this fiber is waiting for data.
Dataloader will resume the fiber after the requested data has been loaded (by another Fiber).
67 68 69 70 |
# File 'lib/graphql/dataloader.rb', line 67 def yield Fiber.yield nil end |
#yielded?(path) ⇒ Boolean
Returns True if the current Fiber has yielded once via Dataloader at path.
74 75 76 |
# File 'lib/graphql/dataloader.rb', line 74 def yielded?(path) @yielded_fibers[Fiber.current] == path end |