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
-
#around_perform ⇒ Object
Interface to add custom code for purposes such as instrumenting the performance of the loader.
-
#initialize ⇒ Loader
constructor
A new instance of Loader.
- #load(key) ⇒ Object
- #load_many(keys) ⇒ Object
- #prime(key, value) ⇒ Object
-
#resolve ⇒ Object
:nodoc:.
- #resolved? ⇒ Boolean
-
#wait ⇒ Object
For Promise#sync.
Constructor Details
#initialize ⇒ Loader
Returns a new instance of Loader.
47 48 49 50 51 52 |
# File 'lib/graphql/batch/loader.rb', line 47 def initialize @loader_key = nil @executor = nil @queue = nil @cache = nil end |
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
#around_perform ⇒ Object
Interface to add custom code for purposes such as instrumenting the performance of the loader.
84 85 86 |
# File 'lib/graphql/batch/loader.rb', line 84 def around_perform yield end |
#load(key) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/graphql/batch/loader.rb', line 54 def load(key) cache[cache_key(key)] ||= begin queue << key ::Promise.new.tap { |promise| promise.source = self } end end |
#load_many(keys) ⇒ Object
61 62 63 |
# File 'lib/graphql/batch/loader.rb', line 61 def load_many(keys) ::Promise.all(keys.map { |key| load(key) }) end |
#prime(key, value) ⇒ Object
65 66 67 |
# File 'lib/graphql/batch/loader.rb', line 65 def prime(key, value) cache[cache_key(key)] ||= ::Promise.resolve(value).tap { |p| p.source = self } end |
#resolve ⇒ Object
:nodoc:
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/graphql/batch/loader.rb', line 69 def resolve #:nodoc: return if resolved? load_keys = queue @queue = nil around_perform do perform(load_keys) end check_for_broken_promises(load_keys) rescue => err reject_pending_promises(load_keys, err) end |
#resolved? ⇒ Boolean
97 98 99 |
# File 'lib/graphql/batch/loader.rb', line 97 def resolved? @queue.nil? || @queue.empty? end |
#wait ⇒ Object
For Promise#sync
89 90 91 92 93 94 95 |
# File 'lib/graphql/batch/loader.rb', line 89 def wait #:nodoc: if executor executor.resolve(self) else resolve end end |