Sha256: e91db5a6888eb04248f91e8db029088a019553fa5dcbf5002fe46f8af700fee9

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

warn "GraphQL::Batch::ExecutionStrategy is deprecated, instead add `use GraphQL::Batch` in GraphQL::Schema.define"

module GraphQL::Batch
  class ExecutionStrategy < GraphQL::Query::SerialExecution
    def execute(_, _, query)
      GraphQL::Batch.batch do
        as_promise_unless_resolved(super)
      end
    rescue GraphQL::InvalidNullError => err
      err.parent_error? || query.context.errors.push(err)
      nil
    end

    # Needed for MutationExecutionStrategy
    def deep_sync(result) #:nodoc:
      ::Promise.sync(as_promise_unless_resolved(result))
    end

    private

    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

4 entries across 4 versions & 1 rubygems

Version Path
graphql-batch-0.3.10 lib/graphql/batch/execution_strategy.rb
graphql-batch-0.3.9 lib/graphql/batch/execution_strategy.rb
graphql-batch-0.3.8 lib/graphql/batch/execution_strategy.rb
graphql-batch-0.3.7 lib/graphql/batch/execution_strategy.rb