Class: CardDB::Rails::GraphQL::GameLoader

Inherits:
GraphQL::Batch::Loader
  • Object
show all
Defined in:
lib/carddb/rails/graphql/game_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(publisher_slug: nil, client: nil) ⇒ GameLoader

Returns a new instance of GameLoader.



9
10
11
12
13
# File 'lib/carddb/rails/graphql/game_loader.rb', line 9

def initialize(publisher_slug: nil, client: nil)
  super()
  @publisher_slug = publisher_slug
  @client = client
end

Instance Method Details

#perform(game_keys) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/carddb/rails/graphql/game_loader.rb', line 15

def perform(game_keys)
  client = @client || CardDB::Rails.client
  publisher_slug = client.config.resolve_publisher(@publisher_slug)

  raise ArgumentError, 'publisher_slug is required (no default configured)' if publisher_slug.nil? || publisher_slug.empty?

  results = client.batch do |batch|
    game_keys.each do |game_key|
      batch.games.get(publisher_slug: publisher_slug, game_key: game_key)
    end
  end

  game_keys.each_with_index do |game_key, index|
    fulfill(game_key, results[index])
  end
end