Module: GraphQL::Execution::Lazy::Resolve

Defined in:
lib/graphql/execution/lazy/resolve.rb

Overview

Helpers for dealing with data structures containing GraphQL::Execution::Lazy instances

Class Method Summary collapse

Class Method Details

.deep_sync(val) ⇒ void

This method returns an undefined value.

Traverse val, triggering resolution for each GraphQL::Execution::Lazy. These GraphQL::Execution::Lazys are expected to mutate their owner data structures during resolution! (They're created with the .then calls in resolve_in_place).



55
56
57
58
59
60
61
62
63
64
# File 'lib/graphql/execution/lazy/resolve.rb', line 55

def self.deep_sync(val)
  case val
  when Lazy
    deep_sync(val.value)
  when Array
    val.each { |v| deep_sync(v.value) }
  when Hash
    val.each { |k, v| deep_sync(v.value) }
  end
end

.each_lazy(value, &block) ⇒ void

This method returns an undefined value.

If value is a collection, call block with any GraphQL::Execution::Lazy instances in the collection



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphql/execution/lazy/resolve.rb', line 31

def self.each_lazy(value, &block)
  case value
  when SelectionResult
    value.each do |key, field_result|
      each_lazy(field_result, &block)
    end
  when Array
    value.each do |field_result|
      each_lazy(field_result, &block)
    end
  when FieldResult
    field_value = value.value
    if field_value.is_a?(Lazy)
      yield(value)
    else
      each_lazy(field_value, &block)
    end
  end
end

.resolve(value) ⇒ void

This method returns an undefined value.

Mutate value, replacing GraphQL::Execution::Lazy instances in place with their resolved values



9
10
11
12
# File 'lib/graphql/execution/lazy/resolve.rb', line 9

def self.resolve(value)
  lazies = resolve_in_place(value)
  deep_sync(lazies)
end

.resolve_in_place(value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/graphql/execution/lazy/resolve.rb', line 14

def self.resolve_in_place(value)
  lazies = []

  each_lazy(value) do |field_result|
    inner_lazy = field_result.value.then do |inner_v|
      field_result.value = inner_v
      resolve_in_place(inner_v)
    end
    lazies.push(inner_lazy)
  end

  Lazy.new { lazies.map(&:value) }
end