Class: GraphQL::Dataloader::Source
- Inherits:
-
Object
- Object
- GraphQL::Dataloader::Source
- Defined in:
- lib/graphql/dataloader/source.rb
Direct Known Subclasses
Constant Summary collapse
- MAX_ITERATIONS =
1000
Instance Attribute Summary collapse
-
#dataloader ⇒ Object
readonly
Returns the value of attribute dataloader.
-
#pending ⇒ Object
readonly
Returns the value of attribute pending.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Class Method Summary collapse
-
.batch_key_for(*batch_args, **batch_kwargs) ⇒ Object
These arguments are given to
dataloader.with(source_class, ...).
Instance Method Summary collapse
-
#clear_cache ⇒ Object
Clear any already-loaded objects for this source.
-
#fetch(keys) ⇒ Object
Subclasses must implement this method to return a value for each of
keys. -
#load(value) ⇒ Object
Parameters.
-
#load_all(values) ⇒ Object
Parameters.
-
#merge(new_results) ⇒ Object
Add these key-value pairs to this source's cache (future loads will use these merged values).
- #normalize_fetch_key(value) ⇒ Object
-
#pending? ⇒ Boolean
Returns.
-
#request(value) ⇒ Object
Returns.
-
#request_all(values) ⇒ Object
Returns.
-
#result_key_for(value) ⇒ Object
Implement this method to return a stable identifier if different key objects should load the same data value.
-
#run_pending_keys ⇒ Object
Called by GraphQL::Dataloader to resolve and pending requests to this source.
-
#setup(dataloader) ⇒ Object
Called by Dataloader to prepare the Source's internal state.
-
#sync(pending_result_keys) ⇒ Object
Wait for a batch, if there's anything to batch.
Instance Attribute Details
#dataloader ⇒ Object (readonly)
Returns the value of attribute dataloader.
19 20 21 |
# File 'lib/graphql/dataloader/source.rb', line 19 def dataloader @dataloader end |
#pending ⇒ Object (readonly)
Returns the value of attribute pending.
276 277 278 |
# File 'lib/graphql/dataloader/source.rb', line 276 def pending @pending end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
276 277 278 |
# File 'lib/graphql/dataloader/source.rb', line 276 def results @results end |
Class Method Details
.batch_key_for(*batch_args, **batch_kwargs) ⇒ Object
These arguments are given to dataloader.with(source_class, ...). The object
returned from this method is used to de-duplicate batch loads under the hood
by using it as a Hash key.
By default, the arguments are all put in an Array. To customize how this source's batches are merged, override this method to return something else.
For example, if you pass ActiveRecord::Relations to .with(...), you could override
this method to call .to_sql on them, thus merging .load(...) calls when they apply
to equivalent relations.
Parameters
batch_args(Array<Object>)batch_kwargs(Hash)
Returns
Object
:call-seq:
batch_key_for(Array[Object] *batch_args, Hash **batch_kwargs) -> Object
255 256 257 258 259 260 261 |
# File 'lib/graphql/dataloader/source.rb', line 255 def self.batch_key_for(*batch_args, **batch_kwargs) if batch_kwargs.any? # rubocop:disable Development/NoneWithoutBlockCop [*batch_args, **batch_kwargs] else batch_args end end |
Instance Method Details
#clear_cache ⇒ Object
Clear any already-loaded objects for this source
Returns
void
:call-seq:
clear_cache() -> void
271 272 273 274 |
# File 'lib/graphql/dataloader/source.rb', line 271 def clear_cache @results.clear nil end |
#fetch(keys) ⇒ Object
Subclasses must implement this method to return a value for each of keys
Parameters
keys(Array<Object>) — keys passed to load, load all, request, or request all
Returns
Array<Object>— A loaded value for each ofkeys. The array must match one-for-one to the list ofkeys.
:call-seq:
fetch(Array[Object] keys) -> Array[Object]
146 147 148 149 |
# File 'lib/graphql/dataloader/source.rb', line 146 def fetch(keys) # somehow retrieve these from the backend raise "Implement `#{self.class}#fetch(#{keys.inspect}) to return a record for each of the keys" end |
#load(value) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/graphql/dataloader/source.rb', line 94 def load(value) result_key = result_key_for(value) if @results.key?(result_key) result_for(result_key) else add_pending_key(result_key, value) sync([result_key]) result_for(result_key) end end |
#load_all(values) ⇒ Object
Parameters
values(Array<Object>) — Loading keys which will be passed to#fetch(or read from the internal cache).
Returns
Object— The result from fetch forkeys. Ifkeyshaven't been loaded yet, the Fiber will yield until they're loaded.
:call-seq:
load_all(Array[Object] values) -> Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/graphql/dataloader/source.rb', line 115 def load_all(values) result_keys = [] pending_keys = [] values.each { |v| k = result_key_for(v) result_keys << k if add_pending_key(k, v) pending_keys << k end } if !pending_keys.empty? sync(pending_keys) end result_keys.map! { |k| result_for(k) } result_keys end |
#merge(new_results) ⇒ Object
Add these key-value pairs to this source's cache (future loads will use these merged values).
Parameters
new_results(Hash<Object => Object>) — key-value pairs to cache in this source
Returns
void
:call-seq:
merge(Hash[Object, Object] new_results) -> void
198 199 200 201 202 203 204 |
# File 'lib/graphql/dataloader/source.rb', line 198 def merge(new_results) new_results.each do |new_k, new_v| key = result_key_for(new_k) @results[key] = new_v end nil end |
#normalize_fetch_key(value) ⇒ Object
Implement this method if varying values given to load (etc) should be consolidated or normalized before being handed off to your fetch implementation.
This is different than result_key_for because that method handles unification inside Dataloader's cache, but this method changes the value passed into fetch.
Parameters
value(Object) — The value passed to load, load_all, request, or request_all
Returns
Object— The value given to fetch
:call-seq:
normalize_fetch_key(Object value) -> Object
66 67 68 |
# File 'lib/graphql/dataloader/source.rb', line 66 def normalize_fetch_key(value) value end |
#pending? ⇒ Boolean
Returns
Boolean— True if this source has any pending requests for data.
:call-seq:
pending?() -> bool
181 182 183 |
# File 'lib/graphql/dataloader/source.rb', line 181 def pending? !@pending.empty? end |
#request(value) ⇒ Object
Returns
Dataloader::Request— a pending request for a value fromkey. Call.loadon that object to wait for the result.
:call-seq:
request(value) -> Dataloader::Request
27 28 29 30 31 |
# File 'lib/graphql/dataloader/source.rb', line 27 def request(value) res_key = result_key_for(value) add_pending_key(res_key, value) Dataloader::Request.new(self, value) end |
#request_all(values) ⇒ Object
Returns
Dataloader::Request— a pending request for a values fromkeys. Call.loadon that object to wait for the results.
:call-seq:
request_all(values) -> Dataloader::Request
76 77 78 79 80 81 82 |
# File 'lib/graphql/dataloader/source.rb', line 76 def request_all(values) values.each do |v| res_key = result_key_for(v) add_pending_key(res_key, v) end Dataloader::RequestAll.new(self, values) end |
#result_key_for(value) ⇒ Object
Implement this method to return a stable identifier if different key objects should load the same data value.
Parameters
value(Object) — A value passed to.requestor.load, for which a value will be loaded
Returns
Object— The key for tracking this pending data
:call-seq:
result_key_for(Object value) -> Object
46 47 48 |
# File 'lib/graphql/dataloader/source.rb', line 46 def result_key_for(value) value end |
#run_pending_keys ⇒ Object
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/source.rb', line 211 def run_pending_keys # :nodoc: @fetching.each_key { |k| @pending.delete(k) } return if @pending.empty? fetch_h = @pending @fetching.merge!(fetch_h) @pending = {} results = fetch(fetch_h.values) idx = 0 fetch_h.each_key do |key| @results[key] = results[idx] @fetching.delete(key) idx += 1 end nil rescue StandardError => error fetch_h.each_key { |key| @results[key] = error @fetching.delete(key) } end |
#setup(dataloader) ⇒ Object
Called by Dataloader to prepare the Source's internal state
9 10 11 12 13 14 15 16 17 |
# File 'lib/graphql/dataloader/source.rb', line 9 def setup(dataloader) # :nodoc: # These keys have been requested but haven't been fetched yet @pending = {} # These keys have been passed to `fetch` but haven't been finished yet @fetching = {} # { key => result } @results = {} @dataloader = dataloader end |
#sync(pending_result_keys) ⇒ Object
Wait for a batch, if there's anything to batch. Then run the batch and update the cache.
Returns
void
:call-seq:
sync(pending_result_keys) -> void
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/graphql/dataloader/source.rb', line 161 def sync(pending_result_keys) @dataloader.queue_pending_source(self) if pending? @dataloader.yield(self) iterations = 0 while pending_result_keys.any? { |key| !@results.key?(key) } iterations += 1 if iterations > MAX_ITERATIONS raise "#{self.class}#sync tried #{MAX_ITERATIONS} times to load pending keys (#{pending_result_keys}), but they still weren't loaded. There is likely a circular dependency#{@dataloader.fiber_limit ? " or `fiber_limit: #{@dataloader.fiber_limit}` is set too low" : ""}." end @dataloader.yield(self) end nil end |