module GraphQL::Execution::Lazy::Resolve

Helpers for dealing with data structures containing {Lazy} instances @api private

Public Class Methods

deep_sync(val) click to toggle source

Traverse `val`, triggering resolution for each {Lazy}. These {Lazy}s are expected to mutate their owner data structures during resolution! (They're created with the `.then` calls in `resolve_in_place`). @return [void]

# File lib/graphql/execution/lazy/resolve.rb, line 78
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(acc, value) click to toggle source

If `value` is a collection, add any {Lazy} instances in the collection to `acc` @return [void]

# File lib/graphql/execution/lazy/resolve.rb, line 51
def self.each_lazy(acc, value)
  case value
  when Hash
    value.each do |key, field_result|
      acc = each_lazy(acc, field_result)
    end
  when Array
    value.each do |field_result|
      acc = each_lazy(acc, field_result)
    end
  when Query::Context::SharedMethods
    field_value = value.value
    case field_value
    when Lazy
      acc = acc << value
    when Enumerable # shortcut for Hash & Array
      acc = each_lazy(acc, field_value)
    end
  end

  acc
end
resolve(value) click to toggle source
# File lib/graphql/execution/lazy/resolve.rb, line 27
def self.resolve(value)
  lazies = resolve_in_place(value)
  deep_sync(lazies)
end
resolve_in_place(value) click to toggle source
# File lib/graphql/execution/lazy/resolve.rb, line 32
def self.resolve_in_place(value)
  acc = each_lazy(NullAccumulator, value)

  if acc.empty?
    Lazy::NullResult
  else
    Lazy.new {
      acc.each_with_index { |ctx, idx|
        acc[idx] = ctx.value.value
      }
      resolve_in_place(acc)
    }
  end
end