Class: GraphQL::Batch::Loader
- Inherits:
-
Object
- Object
- GraphQL::Batch::Loader
- Defined in:
- lib/graphql/batch/loader.rb
Instance Attribute Summary collapse
-
#executor ⇒ Object
Returns the value of attribute executor.
-
#loader_key ⇒ Object
Returns the value of attribute loader_key.
Class Method Summary collapse
- .for(*group_args) ⇒ Object
- .load(key) ⇒ Object
- .load_many(keys) ⇒ Object
- .loader_key_for(*group_args, **group_kwargs) ⇒ Object
Instance Method Summary collapse
- #load(key) ⇒ Object
- #load_many(keys) ⇒ Object
-
#resolve ⇒ Object
:nodoc:.
- #resolved? ⇒ Boolean
-
#wait ⇒ Object
For Promise#sync.
Instance Attribute Details
#executor ⇒ Object
Returns the value of attribute executor.
45 46 47 |
# File 'lib/graphql/batch/loader.rb', line 45 def executor @executor end |
#loader_key ⇒ Object
Returns the value of attribute loader_key.
45 46 47 |
# File 'lib/graphql/batch/loader.rb', line 45 def loader_key @loader_key end |
Class Method Details
.for(*group_args) ⇒ Object
11 12 13 |
# File 'lib/graphql/batch/loader.rb', line 11 def self.for(*group_args) current_executor.loader(loader_key_for(*group_args)) { new(*group_args) } end |
.load(key) ⇒ Object
20 21 22 |
# File 'lib/graphql/batch/loader.rb', line 20 def self.load(key) self.for.load(key) end |
.load_many(keys) ⇒ Object
24 25 26 |
# File 'lib/graphql/batch/loader.rb', line 24 def self.load_many(keys) self.for.load_many(keys) end |
.loader_key_for(*group_args, **group_kwargs) ⇒ Object
16 17 18 |
# File 'lib/graphql/batch/loader.rb', line 16 def self.loader_key_for(*group_args, **group_kwargs) [self, group_kwargs, group_args] end |
Instance Method Details
#load(key) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/graphql/batch/loader.rb', line 47 def load(key) cache[cache_key(key)] ||= begin queue << key ::Promise.new.tap { |promise| promise.source = self } end end |
#load_many(keys) ⇒ Object
54 55 56 |
# File 'lib/graphql/batch/loader.rb', line 54 def load_many(keys) ::Promise.all(keys.map { |key| load(key) }) end |
#resolve ⇒ Object
:nodoc:
58 59 60 61 62 63 64 65 66 |
# File 'lib/graphql/batch/loader.rb', line 58 def resolve #:nodoc: return if resolved? load_keys = queue @queue = nil perform(load_keys) check_for_broken_promises(load_keys) rescue => err reject_pending_promises(load_keys, err) end |
#resolved? ⇒ Boolean
77 78 79 |
# File 'lib/graphql/batch/loader.rb', line 77 def resolved? @queue.nil? || @queue.empty? end |
#wait ⇒ Object
For Promise#sync
69 70 71 72 73 74 75 |
# File 'lib/graphql/batch/loader.rb', line 69 def wait #:nodoc: if executor executor.resolve(self) else resolve end end |