class GraphQL::Batch::Loader
Attributes
executor[RW]
loader_key[RW]
Public Class Methods
for(*group_args)
click to toggle source
# File lib/graphql/batch/loader.rb, line 3 def self.for(*group_args) loader_key = loader_key_for(*group_args) executor = Executor.current unless executor raise GraphQL::Batch::NoExecutorError, 'Cannot create loader without'\ ' an Executor. Wrap the call to `for` with `GraphQL::Batch.batch`'\ ' or use `GraphQL::Batch::Setup` as a query instrumenter if'\ ' using with `graphql-ruby`' end executor.loader(loader_key) { new(*group_args) } end
load(key)
click to toggle source
# File lib/graphql/batch/loader.rb, line 21 def self.load(key) self.for.load(key) end
load_many(keys)
click to toggle source
# File lib/graphql/batch/loader.rb, line 25 def self.load_many(keys) self.for.load_many(keys) end
loader_key_for(*group_args)
click to toggle source
# File lib/graphql/batch/loader.rb, line 17 def self.loader_key_for(*group_args) [self].concat(group_args) end
Public Instance Methods
load(key)
click to toggle source
# File lib/graphql/batch/loader.rb, line 31 def load(key) cache[cache_key(key)] ||= begin queue << key ::Promise.new.tap { |promise| promise.source = self } end end
load_many(keys)
click to toggle source
# File lib/graphql/batch/loader.rb, line 38 def load_many(keys) ::Promise.all(keys.map { |key| load(key) }) end
resolved?()
click to toggle source
# File lib/graphql/batch/loader.rb, line 61 def resolved? @queue.nil? || @queue.empty? end
Protected Instance Methods
cache_key(load_key)
click to toggle source
Override to use a different key for the cache than the load key
# File lib/graphql/batch/loader.rb, line 91 def cache_key(load_key) load_key end
fulfill(key, value)
click to toggle source
Fulfill the key with provided value, for use in perform
# File lib/graphql/batch/loader.rb, line 68 def fulfill(key, value) finish_resolve(key) do |promise| promise.fulfill(value) end end
fulfilled?(key)
click to toggle source
Returns true when the key has already been fulfilled, otherwise returns false
# File lib/graphql/batch/loader.rb, line 81 def fulfilled?(key) promise_for(key).fulfilled? end
perform(keys)
click to toggle source
Must override to load the keys and call fulfill
for each key
# File lib/graphql/batch/loader.rb, line 86 def perform(keys) raise NotImplementedError end
reject(key, reason)
click to toggle source
# File lib/graphql/batch/loader.rb, line 74 def reject(key, reason) finish_resolve(key) do |promise| promise.reject(reason) end end
Private Instance Methods
cache()
click to toggle source
# File lib/graphql/batch/loader.rb, line 105 def cache @cache ||= {} end
check_for_broken_promises(load_keys)
click to toggle source
# File lib/graphql/batch/loader.rb, line 125 def check_for_broken_promises(load_keys) load_keys.each do |key| next unless promise_for(key).pending? reject(key, ::Promise::BrokenError.new("#{self.class} didn't fulfill promise for key #{key.inspect}")) end end
finish_resolve(key) { |promise| ... }
click to toggle source
# File lib/graphql/batch/loader.rb, line 97 def finish_resolve(key) promise = promise_for(key) return yield(promise) unless executor executor.around_promise_callbacks do yield promise end end
promise_for(load_key)
click to toggle source
# File lib/graphql/batch/loader.rb, line 113 def promise_for(load_key) cache.fetch(cache_key(load_key)) end
queue()
click to toggle source
# File lib/graphql/batch/loader.rb, line 109 def queue @queue ||= [] end
reject_pending_promises(load_keys, err)
click to toggle source
# File lib/graphql/batch/loader.rb, line 117 def reject_pending_promises(load_keys, err) load_keys.each do |key| next unless promise_for(key).pending? reject(key, err) end end