• Alex Kalderimis's avatar
    Prepare tests for framework authorization changes · 7d1278ad
    Alex Kalderimis authored
    This mostly adds connection methods in preparation for the
    graphql helper changes.
    
    None of the changes in this commit should cause any observable
    effects on application behaviour.
    
    This includes
    
    - removal of pagination tests from resolver specs (resolvers do not -
      currently - know enough of their arguments to make this work). At the
      moment we need to run all our pagination specs at the request
      integration level.
    
      This is the same for N+1 specs must always be run in request specs,
      since otherwise they are unreliable.
    7d1278ad
lazy.rb 1.03 KB
# frozen_string_literal: true

module Gitlab
  module Graphql
    class Lazy
      include Gitlab::Utils::StrongMemoize

      def initialize(&block)
        @proc = block
      end

      def force
        strong_memoize(:force) { self.class.force(@proc.call) }
      end

      def then(&block)
        self.class.new { yield force }
      end

      # Force evaluation of a (possibly) lazy value
      def self.force(value)
        case value
        when ::Gitlab::Graphql::Lazy
          value.force
        when ::BatchLoader::GraphQL
          value.sync
        when ::Gitlab::Graphql::Deferred
          value.execute
        when ::GraphQL::Execution::Lazy
          value.value # part of the private api, but we can force this as well
        when ::Concurrent::Promise
          value.execute if value.state == :unscheduled

          value.value # value.value(10.seconds)
        else
          value
        end
      end

      def self.with_value(unforced, &block)
        self.new { unforced }.then(&block)
      end
    end
  end
end