Class: GraphQL::Batch::Loader
- Inherits:
-
Object
- Object
- GraphQL::Batch::Loader
- Defined in:
- lib/graphql/batch/loader.rb
Class Method Summary collapse
Instance Method Summary collapse
- #fulfill(key, value) ⇒ Object
- #fulfilled?(key) ⇒ Boolean
- #keys ⇒ Object
- #load(key) ⇒ Object
- #load_many(keys) ⇒ Object
-
#perform(keys) ⇒ Object
batch load keys and fulfill promises.
- #promises_by_key ⇒ Object
- #resolve ⇒ Object
Class Method Details
.for(*group_args) ⇒ Object
3 4 5 |
# File 'lib/graphql/batch/loader.rb', line 3 def self.for(*group_args) Executor.current.loaders[group_args] ||= new(*group_args) end |
Instance Method Details
#fulfill(key, value) ⇒ Object
23 24 25 |
# File 'lib/graphql/batch/loader.rb', line 23 def fulfill(key, value) promises_by_key[key].fulfill(value) end |
#fulfilled?(key) ⇒ Boolean
27 28 29 |
# File 'lib/graphql/batch/loader.rb', line 27 def fulfilled?(key) promises_by_key[key].fulfilled? end |
#keys ⇒ Object
11 12 13 |
# File 'lib/graphql/batch/loader.rb', line 11 def keys promises_by_key.keys end |
#load(key) ⇒ Object
15 16 17 |
# File 'lib/graphql/batch/loader.rb', line 15 def load(key) promises_by_key[key] ||= Promise.new end |
#load_many(keys) ⇒ Object
19 20 21 |
# File 'lib/graphql/batch/loader.rb', line 19 def load_many(keys) Promise.all(keys.map { |key| load(key) }) end |
#perform(keys) ⇒ Object
batch load keys and fulfill promises
32 33 34 |
# File 'lib/graphql/batch/loader.rb', line 32 def perform(keys) raise NotImplementedError end |
#promises_by_key ⇒ Object
7 8 9 |
# File 'lib/graphql/batch/loader.rb', line 7 def promises_by_key @promises_by_key ||= {} end |
#resolve ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/graphql/batch/loader.rb', line 36 def resolve perform(keys) check_for_broken_promises rescue => err promises_by_key.each do |key, promise| promise.reject(err) end end |