Class: GraphQL::Batch::Loader

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(*group_args) ⇒ Object



3
4
5
# File 'lib/graphql/batch/loader.rb', line 3

def self.for(*group_args)
  Executor.current.loaders[group_args] ||= new(*group_args)
end

Instance Method Details

#fulfill(key, value) ⇒ Object



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

def fulfill(key, value)
  promises_by_key[key].fulfill(value)
end

#fulfilled?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def fulfilled?(key)
  promises_by_key[key].fulfilled?
end

#keysObject



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

def keys
  promises_by_key.keys
end

#load(key) ⇒ Object



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

def load(key)
  promises_by_key[key] ||= Promise.new
end

#load_many(keys) ⇒ Object



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

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

#perform(keys) ⇒ Object

batch load keys and fulfill promises

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/graphql/batch/loader.rb', line 32

def perform(keys)
  raise NotImplementedError
end

#promises_by_keyObject



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

def promises_by_key
  @promises_by_key ||= {}
end

#resolveObject



36
37
38
39
40
41
42
43
# File 'lib/graphql/batch/loader.rb', line 36

def resolve
  perform(keys)
  check_for_broken_promises
rescue => err
  promises_by_key.each do |key, promise|
    promise.reject(err)
  end
end