Class: Desiru::GraphQL::DataLoader::BatchLoader
- Inherits:
-
Object
- Object
- Desiru::GraphQL::DataLoader::BatchLoader
- Defined in:
- lib/desiru/graphql/data_loader.rb
Overview
Individual batch loader for a specific module
Instance Attribute Summary collapse
-
#batch_size ⇒ Object
readonly
Returns the value of attribute batch_size.
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#module_class_or_instance ⇒ Object
readonly
Returns the value of attribute module_class_or_instance.
-
#parent_loader ⇒ Object
readonly
Returns the value of attribute parent_loader.
Instance Method Summary collapse
-
#cache_result(inputs, result) ⇒ Object
Cache a single result.
- #clear_cache! ⇒ Object
-
#initialize(module_class_or_instance, parent_loader, batch_size: 100, cache: true) ⇒ BatchLoader
constructor
A new instance of BatchLoader.
-
#load(inputs) ⇒ Object
Load a single input (returns a promise for lazy evaluation).
-
#load_batch(inputs_array) ⇒ Object
Load a batch of inputs - used for immediate batch processing.
-
#process_batch(inputs_array) ⇒ Object
Process a batch of inputs.
Constructor Details
#initialize(module_class_or_instance, parent_loader, batch_size: 100, cache: true) ⇒ BatchLoader
126 127 128 129 130 131 132 |
# File 'lib/desiru/graphql/data_loader.rb', line 126 def initialize(module_class_or_instance, parent_loader, batch_size: 100, cache: true) @module_class_or_instance = module_class_or_instance @parent_loader = parent_loader @batch_size = batch_size @cache = cache @cache_store = {} if cache end |
Instance Attribute Details
#batch_size ⇒ Object (readonly)
Returns the value of attribute batch_size.
124 125 126 |
# File 'lib/desiru/graphql/data_loader.rb', line 124 def batch_size @batch_size end |
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
124 125 126 |
# File 'lib/desiru/graphql/data_loader.rb', line 124 def cache @cache end |
#module_class_or_instance ⇒ Object (readonly)
Returns the value of attribute module_class_or_instance.
124 125 126 |
# File 'lib/desiru/graphql/data_loader.rb', line 124 def module_class_or_instance @module_class_or_instance end |
#parent_loader ⇒ Object (readonly)
Returns the value of attribute parent_loader.
124 125 126 |
# File 'lib/desiru/graphql/data_loader.rb', line 124 def parent_loader @parent_loader end |
Instance Method Details
#cache_result(inputs, result) ⇒ Object
Cache a single result
189 190 191 |
# File 'lib/desiru/graphql/data_loader.rb', line 189 def cache_result(inputs, result) @cache_store[cache_key(inputs)] = result if cache end |
#clear_cache! ⇒ Object
166 167 168 |
# File 'lib/desiru/graphql/data_loader.rb', line 166 def clear_cache! @cache_store.clear if cache end |
#load(inputs) ⇒ Object
Load a single input (returns a promise for lazy evaluation)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/desiru/graphql/data_loader.rb', line 147 def load(inputs) # Check cache first if enabled if cache && @cache_store.key?(cache_key(inputs)) # Return immediately fulfilled promise for cached value promise = Promise.new promise.fulfill(@cache_store[cache_key(inputs)]) promise else # Check for existing pending promise to enable deduplication existing_promise = check_pending_promise(inputs) return existing_promise if existing_promise # Create promise and queue for batch loading Promise.new do |promise| queue_for_loading(inputs, promise) end end end |
#load_batch(inputs_array) ⇒ Object
Load a batch of inputs - used for immediate batch processing
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/desiru/graphql/data_loader.rb', line 135 def load_batch(inputs_array) return load_from_cache(inputs_array) if cache && all_cached?(inputs_array) results = process_batch(inputs_array) # Cache results if enabled cache_results(inputs_array, results) if cache results end |
#process_batch(inputs_array) ⇒ Object
Process a batch of inputs
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/desiru/graphql/data_loader.rb', line 171 def process_batch(inputs_array) # Use the provided module instance or create one module_instance = if @module_class_or_instance.is_a?(Class) create_module_instance(inputs_array.first) else @module_class_or_instance end if module_instance.respond_to?(:batch_forward) # If module supports batch processing module_instance.batch_forward(inputs_array) else # Fall back to individual processing inputs_array.map { |inputs| module_instance.call(inputs) } end end |