Sha256: 37ec9ebd519a58b1e58a773cbde6ce7613e53d943d1a383e967ced2d621c7627

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module GraphQL::Batch
  class ExecutionStrategy < GraphQL::Query::SerialExecution
    attr_accessor :disable_batching

    def execute(_, _, query)
      as_promise(super).sync
    rescue GraphQL::InvalidNullError => err
      err.parent_error? || query.context.errors.push(err)
      nil
    ensure
      GraphQL::Batch::Executor.current.clear
    end

    private

    def as_promise(result)
      GraphQL::Batch::Promise.resolve(as_promise_unless_resolved(result))
    end

    def as_promise_unless_resolved(result)
      all_promises = []
      each_promise(result) do |obj, key, promise|
        obj[key] = nil
        all_promises << promise.then do |value|
          obj[key] = value
          as_promise_unless_resolved(value)
        end
      end
      return result if all_promises.empty?
      Promise.all(all_promises).then { result }
    end

    def each_promise(obj, &block)
      case obj
      when Array
        obj.each_with_index do |value, idx|
          each_promise_in_entry(obj, idx, value, &block)
        end
      when Hash
        obj.each do |key, value|
          each_promise_in_entry(obj, key, value, &block)
        end
      end
    end

    def each_promise_in_entry(obj, key, value, &block)
      if value.is_a?(::Promise)
        yield obj, key, value
      else
        each_promise(value, &block)
      end
    end

    class FieldResolution < GraphQL::Query::SerialExecution::FieldResolution
      def get_finished_value(raw_value)
        if raw_value.is_a?(::Promise)
          raw_value.then(->(result) { super(result) }, lambda do |error|
            error.is_a?(GraphQL::ExecutionError) ? super(error) : raise(error)
          end)
        else
          super
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-batch-0.2.5 lib/graphql/batch/execution_strategy.rb