Sha256: 9e753c8b4067f25dcbef6ac3121d66260b354dcf7060cd00816fddf90d751b3f

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 Bytes

Contents

# frozen_string_literal: true
module GraphQL
  class Schema
    # Given {steps} and {arguments}, call steps in order, passing `(*arguments, next_step)`.
    #
    # Steps should call `next_step.call` to continue the chain, or _not_ call it to stop the chain.
    class MiddlewareChain
      # @return [Array<#call(*args)>] Steps in this chain, will be called with arguments and `next_middleware`
      attr_reader :steps

      # @return [Array] Arguments passed to steps (followed by `next_middleware`)
      attr_reader :arguments

      def initialize(steps:, arguments:)
        # We're gonna destroy this array, so copy it:
        @steps = steps.dup
        @arguments = arguments
      end

      # Run the next step in the chain, passing in arguments and handle to the next step
      def call(next_arguments = @arguments)
        @arguments = next_arguments
        next_step = steps.shift
        next_middleware = self
        next_step.call(*arguments, next_middleware)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-1.3.0 lib/graphql/schema/middleware_chain.rb