Class: GraphqlLazyLoad::Custom

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

Instance Method Summary collapse

Constructor Details

#initialize(type, unique_identifier, value, **params, &block) ⇒ Custom

Returns a new instance of Custom.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/graphql_lazy_load.rb', line 8

def initialize(type, unique_identifier, value, **params, &block)
  @unique_identifier = unique_identifier
  @params = params
  @block = block
  @value = value
  # Initialize the loading state for this query,
  # or get the previously-initiated state
  # scope cant be used as a hash key because when .hash is called on diff
  # for ~same~ scopes its diff every time but scope == scope will return true if ~same~
  @lazy = type.context[context_key] ||= {
    values_to_load: Set.new,
    ids: Set.new,
    results: {}
  }
  # Register this to be loaded later unless we've already queued or loaded it
  return if already_loaded_or_queued?
  lazy_values.add(value)
  lazy_ids.add(value)
end

Instance Method Details

#resultObject

Return the loaded record, hitting the database if needed



29
30
31
32
33
34
35
# File 'lib/graphql_lazy_load.rb', line 29

def result
  if !already_loaded? && any_to_load?
    lazy_results.merge!(block_results)
    lazy_values.clear
  end
  lazy_results[value]
end