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.



33
34
35
# File 'lib/batch_loader/graphql.rb', line 33

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.



31
32
33
# File 'lib/batch_loader/graphql.rb', line 31

def batch_loader=(value)
  @batch_loader = value
end

Class Method Details

.for(item) ⇒ Object



27
28
29
# File 'lib/batch_loader/graphql.rb', line 27

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

.instrument(type, field) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/batch_loader/graphql.rb', line 11

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.wrap(result) : result
  end

  field.redefine { resolve(new_resolve_proc) }
end

.use(schema_definition) ⇒ Object



5
6
7
8
9
# File 'lib/batch_loader/graphql.rb', line 5

def self.use(schema_definition)
  schema_definition.lazy_resolve(BatchLoader::GraphQL, :sync)
  # for graphql gem versions <= 1.8.6 which work with BatchLoader instead of BatchLoader::GraphQL
  schema_definition.instrument(:field, self)
end

.wrap(batch_loader) ⇒ Object



21
22
23
24
25
# File 'lib/batch_loader/graphql.rb', line 21

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

Instance Method Details

#batch(*args, &block) ⇒ Object



37
38
39
40
# File 'lib/batch_loader/graphql.rb', line 37

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

#syncObject



42
43
44
# File 'lib/batch_loader/graphql.rb', line 42

def sync
  @batch_loader.__sync
end