Class: BatchLoader::GraphQL

Inherits:
Object
  • Object
show all
Defined in:
lib/batch_loader/graphql.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item = nil) ⇒ GraphQL

Returns a new instance of GraphQL.



53
54
55
# File 'lib/batch_loader/graphql.rb', line 53

def initialize(item = nil)
  @batch_loader = BatchLoader.for(item)
end

Instance Attribute Details

#batch_loader=(value) ⇒ Object (writeonly)

Sets the attribute batch_loader

Parameters:

  • value

    the value to set the attribute batch_loader to.



51
52
53
# File 'lib/batch_loader/graphql.rb', line 51

def batch_loader=(value)
  @batch_loader = value
end

Class Method Details

.for(item) ⇒ Object



47
48
49
# File 'lib/batch_loader/graphql.rb', line 47

def self.for(item)
  new(item)
end

.instrument(type, field) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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

.trace(event, _data) ⇒ Object



16
17
18
19
20
21
22
23
# 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) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# 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) ⇒ Object



41
42
43
44
45
# 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

Instance Method Details

#batch(**kwargs, &block) ⇒ Object



57
58
59
60
# File 'lib/batch_loader/graphql.rb', line 57

def batch(**kwargs, &block)
  @batch_loader.batch(**kwargs, &block)
  self
end

#syncObject



62
63
64
# File 'lib/batch_loader/graphql.rb', line 62

def sync
  @batch_loader.__sync
end