Class: GraphQL::Batch::Loader

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoader

Returns a new instance of Loader.



47
48
49
50
51
52
# File 'lib/graphql/batch/loader.rb', line 47

def initialize
  @loader_key = nil
  @executor = nil
  @queue = nil
  @cache = nil
end

Instance Attribute Details

#executorObject

Returns the value of attribute executor.



45
46
47
# File 'lib/graphql/batch/loader.rb', line 45

def executor
  @executor
end

#loader_keyObject

Returns the value of attribute loader_key.



45
46
47
# File 'lib/graphql/batch/loader.rb', line 45

def loader_key
  @loader_key
end

Class Method Details

.for(*group_args) ⇒ Object



11
12
13
# File 'lib/graphql/batch/loader.rb', line 11

def self.for(*group_args)
  current_executor.loader(loader_key_for(*group_args)) { new(*group_args) }
end

.load(key) ⇒ Object



20
21
22
# File 'lib/graphql/batch/loader.rb', line 20

def self.load(key)
  self.for.load(key)
end

.load_many(keys) ⇒ Object



24
25
26
# File 'lib/graphql/batch/loader.rb', line 24

def self.load_many(keys)
  self.for.load_many(keys)
end

.loader_key_for(*group_args, **group_kwargs) ⇒ Object



16
17
18
# File 'lib/graphql/batch/loader.rb', line 16

def self.loader_key_for(*group_args, **group_kwargs)
  [self, group_kwargs, group_args]
end

Instance Method Details

#around_performObject

Interface to add custom code for purposes such as instrumenting the performance of the loader.



80
81
82
# File 'lib/graphql/batch/loader.rb', line 80

def around_perform
  yield
end

#load(key) ⇒ Object



54
55
56
57
58
59
# File 'lib/graphql/batch/loader.rb', line 54

def load(key)
  cache[cache_key(key)] ||= begin
    queue << key
    ::Promise.new.tap { |promise| promise.source = self }
  end
end

#load_many(keys) ⇒ Object



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

def load_many(keys)
  ::Promise.all(keys.map { |key| load(key) })
end

#resolveObject

:nodoc:



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/graphql/batch/loader.rb', line 65

def resolve #:nodoc:
  return if resolved?
  load_keys = queue
  @queue = nil

  around_perform do
    perform(load_keys)
  end

  check_for_broken_promises(load_keys)
rescue => err
  reject_pending_promises(load_keys, err)
end

#resolved?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/graphql/batch/loader.rb', line 93

def resolved?
  @queue.nil? || @queue.empty?
end

#waitObject

For Promise#sync



85
86
87
88
89
90
91
# File 'lib/graphql/batch/loader.rb', line 85

def wait #:nodoc:
  if executor
    executor.resolve(self)
  else
    resolve
  end
end