Class: GraphqlLazyLoad::Custom

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_lazy_load/custom.rb

Instance Method Summary collapse

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

#resultObject

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