Class: GraphQL::Execution::Lazy

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

Overview

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

Classes: LazyMethodMap

Constant Summary collapse

NullResult =

This can be used for fields which had no lazy results :nodoc:

Lazy.new(){}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field: nil, &get_value_func) ⇒ Lazy

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

Parameters

  • field (GraphQL::Schema::Field)
  • get_value_func (Proc) — a block to get the inner value (later)

:call-seq:

initialize(GraphQL::Schema::Field field:, Proc &get_value_func)


27
28
29
30
31
# File 'lib/graphql/execution/lazy.rb', line 27

def initialize(field: nil, &get_value_func)
  @get_value_func = get_value_func
  @resolved = false
  @field = field
end

Instance Attribute Details

#field ⇒ Object (readonly)

:nodoc:



16
17
18
# File 'lib/graphql/execution/lazy.rb', line 16

def field
  @field
end

Class Method Details

.all(lazies) ⇒ Object

Parameters

  • lazies (Array<Object>) — Maybe-lazy objects

Returns

  • Lazy — A lazy which will sync all of lazies

:call-seq:

all(Array[Object] lazies) -> Lazy


82
83
84
85
86
# File 'lib/graphql/execution/lazy.rb', line 82

def self.all(lazies)
  self.new {
    lazies.map { |l| l.is_a?(Lazy) ? l.value : l }
  }
end

Instance Method Details

#then ⇒ Object

Returns

  • Lazy — A Lazy whose value depends on another Lazy, plus any transformations in block

:call-seq:

then() -> Lazy


66
67
68
69
70
# File 'lib/graphql/execution/lazy.rb', line 66

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

#value ⇒ Object

Returns

  • Object — The wrapped value, calling the lazy block if necessary

:call-seq:

value() -> Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/graphql/execution/lazy.rb', line 39

def value
  if !@resolved
    @resolved = true
    v = @get_value_func.call
    if v.is_a?(Lazy)
      v = v.value
    end
    @value = v
  end

  # `SKIP` was made into a subclass of `GraphQL::Error` to improve runtime performance
  # (fewer clauses in a hot `case` block), but now it requires special handling here.
  # I think it's still worth it for the performance win, but if the number of special
  # cases grows, then maybe it's worth rethinking somehow.
  if @value.is_a?(StandardError) && !@value.is_a?(GraphQL::Execution::Skip)
    raise @value
  else
    @value
  end
end