summaryrefslogtreecommitdiff
path: root/app/graphql/loaders/base_loader.rb
blob: c32c4daa91af7294354ee09e69fb08da52123780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Helper methods for all loaders
class Loaders::BaseLoader < GraphQL::Batch::Loader
  # Convert a class method into a resolver proc. The method should follow the
  # (obj, args, ctx) calling convention
  class << self
    def [](sym)
      resolver = method(sym)
      raise ArgumentError.new("#{self}.#{sym} is not a resolver") unless resolver.arity == 3

      resolver
    end
  end

  # Fulfill all keys. Pass a block that converts each result into a key.
  # Any keys not in results will be fulfilled with nil.
  def fulfill_all(results, keys, &key_blk)
    results.each do |result|
      key = yield result
      fulfill(key, result)
    end

    keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
  end
end