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) ⇒ 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.
29 30 31 |
# File 'lib/graphql/batch/loader.rb', line 29 def executor @executor end |
#loader_key ⇒ Object
Returns the value of attribute loader_key.
29 30 31 |
# File 'lib/graphql/batch/loader.rb', line 29 def loader_key @loader_key end |
Class Method Details
.for(*group_args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 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) ⇒ Object
21 22 23 |
# File 'lib/graphql/batch/loader.rb', line 21 def self.load(key) self.for.load(key) end |
.load_many(keys) ⇒ Object
25 26 27 |
# File 'lib/graphql/batch/loader.rb', line 25 def self.load_many(keys) self.for.load_many(keys) end |
.loader_key_for(*group_args) ⇒ Object
17 18 19 |
# File 'lib/graphql/batch/loader.rb', line 17 def self.loader_key_for(*group_args) [self].concat(group_args) end |
Instance Method Details
#load(key) ⇒ Object
31 32 33 34 35 36 |
# 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) ⇒ Object
38 39 40 |
# File 'lib/graphql/batch/loader.rb', line 38 def load_many(keys) ::Promise.all(keys.map { |key| load(key) }) end |
#resolve ⇒ Object
:nodoc:
42 43 44 45 46 47 48 49 50 |
# File 'lib/graphql/batch/loader.rb', line 42 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
61 62 63 |
# File 'lib/graphql/batch/loader.rb', line 61 def resolved? @queue.nil? || @queue.empty? end |
#wait ⇒ Object
For Promise#sync
53 54 55 56 57 58 59 |
# File 'lib/graphql/batch/loader.rb', line 53 def wait #:nodoc: if executor executor.resolve(self) else resolve end end |