Sha256: 1e0cacf1e04925fe43315f0d3cf01377f387b37fcd9321679a9783a0d027fa9c

Contents?: true

Size: 1.28 KB

Versions: 6

Compression:

Stored size: 1.28 KB

Contents

module GraphQL::Batch
  class Executor
    THREAD_KEY = :"#{name}.batched_queries"
    private_constant :THREAD_KEY

    def self.current
      Thread.current[THREAD_KEY]
    end

    def self.current=(executor)
      Thread.current[THREAD_KEY] = executor
    end

    attr_reader :loaders

    # Set to true when performing a batch query, otherwise, it is false.
    #
    # Can be used to detect unbatched queries in an ActiveSupport::Notifications.subscribe block.
    attr_reader :loading

    def initialize
      @loaders = {}
      @loading = false
    end

    def resolve(loader)
      with_loading(true) { loader.resolve }
    end

    def shift
      @loaders.shift.last
    end

    def tick
      resolve(shift)
    end

    def wait_all
      tick until loaders.empty?
    end

    def clear
      loaders.clear
    end

    def defer
      # Since we aren't actually deferring callbacks, we need to set #loading to false so that any queries
      # that happen in the callback aren't interpreted as being performed in GraphQL::Batch::Loader#perform
      with_loading(false) { yield }
    end

    private

    def with_loading(loading)
      was_loading = @loading
      begin
        @loading = loading
        yield
      ensure
        @loading = was_loading
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graphql-batch-0.3.5 lib/graphql/batch/executor.rb
graphql-batch-0.3.4 lib/graphql/batch/executor.rb
graphql-batch-0.3.3 lib/graphql/batch/executor.rb
graphql-batch-0.3.2 lib/graphql/batch/executor.rb
graphql-batch-0.3.1 lib/graphql/batch/executor.rb
graphql-batch-0.3.0 lib/graphql/batch/executor.rb