Class: BatchLoader::GraphQL

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

Defined Under Namespace

Modules: Trace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item = nil) ⇒ GraphQL

Returns a new instance of GraphQL.



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

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.



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

def batch_loader=(value)
  @batch_loader = value
end

Class Method Details

.for(item) ⇒ Object



55
56
57
# File 'lib/batch_loader/graphql.rb', line 55

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

.instrument(type, field) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/batch_loader/graphql.rb', line 34

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



25
26
27
28
29
30
31
32
# File 'lib/batch_loader/graphql.rb', line 25

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



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/batch_loader/graphql.rb', line 12

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?(:trace_with)
    schema_definition.trace_with(Trace)
  elsif schema_definition.respond_to?(:interpreter?) && schema_definition.interpreter?
    schema_definition.tracer(self)
  else
    schema_definition.instrument(:field, self)
  end
end

.wrap(batch_loader) ⇒ Object



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

def self.wrap(batch_loader)
  BatchLoader::GraphQL.new.tap do |graphql|
    graphql.batch_loader = batch_loader
  end
end

.wrap_with_warning(batch_loader) ⇒ Object



44
45
46
47
# File 'lib/batch_loader/graphql.rb', line 44

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

Instance Method Details

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



65
66
67
68
# File 'lib/batch_loader/graphql.rb', line 65

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

#syncObject



70
71
72
# File 'lib/batch_loader/graphql.rb', line 70

def sync
  @batch_loader.__sync
end