Class: GraphqlLazyLoad::Custom
- Inherits:
-
Object
- Object
- GraphqlLazyLoad::Custom
- Defined in:
- lib/graphql_lazy_load/custom.rb
Instance Method Summary collapse
-
#initialize(type, unique_identifier, value, default_value: nil, **params, &block) ⇒ Custom
constructor
A new instance of Custom.
-
#result ⇒ Object
Return the loaded record, hitting the database if needed.
Constructor Details
#initialize(type, unique_identifier, value, default_value: nil, **params, &block) ⇒ Custom
Returns a new instance of Custom.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/graphql_lazy_load/custom.rb', line 5 def initialize(type, unique_identifier, value, default_value: nil, **params, &block) context_key = [type.class, unique_identifier, params] @value = value # Initialize the loading state for this query, # or get the previously-initiated state @lazy = type.context[context_key] ||= { values: Set.new, results: Hash.new(default_value), params: params, block: block, } # Register this to be loaded later unless we've already queued or loaded it return if already_loaded_or_queued? lazy_values.add(value) end |
Instance Method Details
#result ⇒ Object
Return the loaded record, hitting the database if needed
22 23 24 25 26 27 28 |
# File 'lib/graphql_lazy_load/custom.rb', line 22 def result if not_already_loaded? && any_to_load? lazy_results.merge!(block_results) lazy_values.clear end lazy_results[value] end |