Class: GraphQL::Execution::Lazy Private

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/execution/lazy.rb,
lib/graphql/execution/lazy/resolve.rb,
lib/graphql/execution/lazy/lazy_method_map.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This wraps a value which is available, but not yet calculated, like a promise or future.

Calling #value will trigger calculation & return the "lazy" value.

This is an itty-bitty promise-like object, with key differences:

  • It has only two states, not-resolved and resolved
  • It has no error-catching functionality

Defined Under Namespace

Modules: Resolve Classes: LazyMethodMap

Constant Summary collapse

NullResult =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

This can be used for fields which had no lazy results

Lazy.new(){}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&get_value_func) ⇒ Lazy

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a GraphQL::Execution::Lazy which will get its inner value by calling the block

Parameters:

  • get_value_func (Proc)

    a block to get the inner value (later)



24
25
26
27
# File 'lib/graphql/execution/lazy.rb', line 24

def initialize(&get_value_func)
  @get_value_func = get_value_func
  @resolved = false
end

Class Method Details

.resolve(val) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Traverse val, lazily resolving any values along the way

Parameters:

  • val (Object)

    A data structure containing mixed plain values and Lazy instances

Returns:

  • void



18
19
20
# File 'lib/graphql/execution/lazy.rb', line 18

def self.resolve(val)
  Resolve.resolve(val)
end

Instance Method Details

#thenLazy

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns A GraphQL::Execution::Lazy whose value depends on another GraphQL::Execution::Lazy, plus any transformations in block.

Returns:



43
44
45
46
47
# File 'lib/graphql/execution/lazy.rb', line 43

def then
  self.class.new {
    yield(value)
  }
end

#valueObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The wrapped value, calling the lazy block if necessary.

Returns:

  • (Object)

    The wrapped value, calling the lazy block if necessary



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/graphql/execution/lazy.rb', line 30

def value
  if !@resolved
    @resolved = true
    @value = begin
      @get_value_func.call
    rescue GraphQL::ExecutionError => err
      err
    end
  end
  @value
end