Class: GraphQL::Batch::Loader
- Inherits:
-
Object
- Object
- GraphQL::Batch::Loader
- Defined in:
- lib/graphql/batch/loader.rb
Constant Summary collapse
- NoExecutorError =
GraphQL::Batch::NoExecutorError
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.
32 33 34 |
# File 'lib/graphql/batch/loader.rb', line 32 def executor @executor end |
#loader_key ⇒ Object
Returns the value of attribute loader_key.
32 33 34 |
# File 'lib/graphql/batch/loader.rb', line 32 def loader_key @loader_key end |
Class Method Details
.for(*group_args) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/graphql/batch/loader.rb', line 6 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
24 25 26 |
# File 'lib/graphql/batch/loader.rb', line 24 def self.load(key) self.for.load(key) end |
.load_many(keys) ⇒ Object
28 29 30 |
# File 'lib/graphql/batch/loader.rb', line 28 def self.load_many(keys) self.for.load_many(keys) end |
.loader_key_for(*group_args) ⇒ Object
20 21 22 |
# File 'lib/graphql/batch/loader.rb', line 20 def self.loader_key_for(*group_args) [self].concat(group_args) end |
Instance Method Details
#load(key) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/graphql/batch/loader.rb', line 34 def load(key) cache[cache_key(key)] ||= begin queue << key ::Promise.new.tap { |promise| promise.source = self } end end |
#load_many(keys) ⇒ Object
41 42 43 |
# File 'lib/graphql/batch/loader.rb', line 41 def load_many(keys) ::Promise.all(keys.map { |key| load(key) }) end |
#resolve ⇒ Object
:nodoc:
45 46 47 48 49 50 51 52 53 |
# File 'lib/graphql/batch/loader.rb', line 45 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
64 65 66 |
# File 'lib/graphql/batch/loader.rb', line 64 def resolved? @queue.nil? || @queue.empty? end |
#wait ⇒ Object
For Promise#sync
56 57 58 59 60 61 62 |
# File 'lib/graphql/batch/loader.rb', line 56 def wait #:nodoc: if executor executor.resolve(self) else resolve end end |