Class: GraphQL::Batch::Executor

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExecutor

Returns a new instance of Executor.



35
36
37
38
39
# File 'lib/graphql/batch/executor.rb', line 35

def initialize
  @loaders = {}
  @loading = false
  @nesting_level = 0
end

Instance Attribute Details

#loadingObject (readonly)

Set to true when performing a batch query, otherwise, it is false.

Can be used to detect unbatched queries in an ActiveSupport::Notifications.subscribe block.



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

def loading
  @loading
end

Class Method Details

.currentObject



7
8
9
# File 'lib/graphql/batch/executor.rb', line 7

def current
  Thread.current[THREAD_KEY]
end

.current=(executor) ⇒ Object



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

def current=(executor)
  Thread.current[THREAD_KEY] = executor
end

.end_batchObject



20
21
22
23
24
25
26
27
# File 'lib/graphql/batch/executor.rb', line 20

def end_batch
  executor = current
  unless executor
    raise NoExecutorError, 'Cannot end a batch without an Executor.'
  end
  return unless executor.decrement_level < 1
  self.current = nil
end

.start_batch(executor_class) ⇒ Object



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

def start_batch(executor_class)
  executor = Thread.current[THREAD_KEY] ||= executor_class.new
  executor.increment_level
end

Instance Method Details

#around_promise_callbacksObject



76
77
78
79
80
81
82
83
84
# File 'lib/graphql/batch/executor.rb', line 76

def around_promise_callbacks
  # We need to set #loading to false so that any queries that happen in the promise
  # callback aren't interpreted as being performed in GraphQL::Batch::Loader#perform
  was_loading = @loading
  @loading = false
  yield
ensure
  @loading = was_loading
end

#clearObject



64
65
66
# File 'lib/graphql/batch/executor.rb', line 64

def clear
  @loaders.clear
end

#decrement_levelObject



72
73
74
# File 'lib/graphql/batch/executor.rb', line 72

def decrement_level
  @nesting_level -= 1
end

#increment_levelObject



68
69
70
# File 'lib/graphql/batch/executor.rb', line 68

def increment_level
  @nesting_level += 1
end

#loader(key) ⇒ Object



41
42
43
44
45
46
# File 'lib/graphql/batch/executor.rb', line 41

def loader(key)
  @loaders[key] ||= yield.tap do |loader|
    loader.executor = self
    loader.loader_key = key
  end
end

#resolve(loader) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/graphql/batch/executor.rb', line 48

def resolve(loader)
  was_loading = @loading
  @loading = true
  loader.resolve
ensure
  @loading = was_loading
end

#tickObject



56
57
58
# File 'lib/graphql/batch/executor.rb', line 56

def tick
  resolve(@loaders.shift.last)
end

#wait_allObject



60
61
62
# File 'lib/graphql/batch/executor.rb', line 60

def wait_all
  tick until @loaders.empty?
end