class BatchLoader::GraphQL

Attributes

batch_loader[W]

Public Class Methods

for(item) click to toggle source
# File lib/batch_loader/graphql.rb, line 47
def self.for(item)
  new(item)
end
instrument(type, field) click to toggle source
# File lib/batch_loader/graphql.rb, line 25
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) ? wrap_with_warning(result) : result
  end

  field.redefine { resolve(new_resolve_proc) }
end
new(item = nil) click to toggle source
# File lib/batch_loader/graphql.rb, line 53
def initialize(item = nil)
  @batch_loader = BatchLoader.for(item)
end
trace(event, _data) { || ... } click to toggle source
# File lib/batch_loader/graphql.rb, line 16
def self.trace(event, _data)
  if event == 'execute_field'
    result = yield
    result.respond_to?(:__sync) ? wrap_with_warning(result) : result
  else
    yield
  end
end
use(schema_definition) click to toggle source
# File lib/batch_loader/graphql.rb, line 5
def self.use(schema_definition)
  schema_definition.lazy_resolve(BatchLoader::GraphQL, :sync)

  # in cases when BatchLoader is being used instead of BatchLoader::GraphQL
  if schema_definition.respond_to?(:interpreter?) && schema_definition.interpreter?
    schema_definition.tracer(self)
  else
    schema_definition.instrument(:field, self)
  end
end
wrap(batch_loader) click to toggle source
# File lib/batch_loader/graphql.rb, line 41
def self.wrap(batch_loader)
  BatchLoader::GraphQL.new.tap do |graphql|
    graphql.batch_loader = batch_loader
  end
end

Private Class Methods

wrap_with_warning(batch_loader) click to toggle source
# File lib/batch_loader/graphql.rb, line 35
def self.wrap_with_warning(batch_loader)
  warn "DEPRECATION WARNING: using BatchLoader.for in GraphQL is deprecated. Use BatchLoader::GraphQL.for instead or return BatchLoader::GraphQL.wrap from your resolver."
  wrap(batch_loader)
end

Public Instance Methods

batch(**kwargs, &block) click to toggle source
# File lib/batch_loader/graphql.rb, line 57
def batch(**kwargs, &block)
  @batch_loader.batch(**kwargs, &block)
  self
end
sync() click to toggle source
# File lib/batch_loader/graphql.rb, line 62
def sync
  @batch_loader.__sync
end