Sha256: fb26aa1d2c60ddf697e7599a3beb50477b3ba370401302cc8928a47a50de58ac

Contents?: true

Size: 774 Bytes

Versions: 7

Compression:

Stored size: 774 Bytes

Contents

# frozen_string_literal: true

class BatchLoader
  class GraphQL
    class Wrapper
      def initialize(batch_loader)
        @batch_loader = batch_loader
      end

      def sync
        @batch_loader.__sync
      end
    end

    def self.use(schema_definition)
      schema_definition.lazy_resolve(BatchLoader::GraphQL::Wrapper, :sync)
      schema_definition.instrument(:field, self)
    end

    def self.instrument(type, field)
      old_resolve_proc = field.resolve_proc
      new_resolve_proc = ->(object, arguments, context) do
        result = old_resolve_proc.call(object, arguments, context)
        result.respond_to?(:__sync) ? BatchLoader::GraphQL::Wrapper.new(result) : result
      end

      field.redefine { resolve(new_resolve_proc) }
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
batch-loader-1.2.2 lib/batch_loader/graphql.rb
batch-loader-1.2.1 lib/batch_loader/graphql.rb
batch-loader-1.2.0 lib/batch_loader/graphql.rb
batch-loader-1.1.1 lib/batch_loader/graphql.rb
batch-loader-1.1.0 lib/batch_loader/graphql.rb
batch-loader-1.0.4 lib/batch_loader/graphql.rb
batch-loader-1.0.3 lib/batch_loader/graphql.rb