Sha256: 3f7a962f6e4cb140298f1109fa3fa3221b9b057aa98cb64fafbeda60c902d3af

Contents?: true

Size: 1.57 KB

Versions: 16

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true
require "graphql/execution/lazy/lazy_method_map"
require "graphql/execution/lazy/resolve"
module GraphQL
  module Execution
    # 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
    # @api private
    class Lazy
      # Traverse `val`, lazily resolving any values along the way
      # @param val [Object] A data structure containing mixed plain values and `Lazy` instances
      # @return void
      def self.resolve(val)
        Resolve.resolve(val)
      end

      # Create a {Lazy} which will get its inner value by calling the block
      # @param get_value_func [Proc] a block to get the inner value (later)
      def initialize(&get_value_func)
        @get_value_func = get_value_func
        @resolved = false
      end

      # @return [Object] The wrapped value, calling the lazy block if necessary
      def value
        if !@resolved
          @resolved = true
          @value = begin
            @get_value_func.call
          rescue GraphQL::ExecutionError => err
            err
          end
        end
        @value
      end

      # @return [Lazy] A {Lazy} whose value depends on another {Lazy}, plus any transformations in `block`
      def then(&block)
        self.class.new {
          block.call(value)
        }
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
graphql-1.6.2 lib/graphql/execution/lazy.rb
graphql-1.6.1 lib/graphql/execution/lazy.rb
graphql-1.6.0 lib/graphql/execution/lazy.rb
graphql-1.5.14 lib/graphql/execution/lazy.rb
graphql-1.5.13 lib/graphql/execution/lazy.rb
graphql-1.5.7.1 lib/graphql/execution/lazy.rb
graphql-1.5.12 lib/graphql/execution/lazy.rb
graphql-1.5.11 lib/graphql/execution/lazy.rb
graphql-1.5.10 lib/graphql/execution/lazy.rb
graphql-1.5.9 lib/graphql/execution/lazy.rb
graphql-1.5.8 lib/graphql/execution/lazy.rb
graphql-1.5.7 lib/graphql/execution/lazy.rb
graphql-1.5.6 lib/graphql/execution/lazy.rb
graphql-1.5.5 lib/graphql/execution/lazy.rb
graphql-1.5.4 lib/graphql/execution/lazy.rb
graphql-1.5.3 lib/graphql/execution/lazy.rb