Class: ActiveGraphql::Model::FindInBatches

Inherits:
Object
  • Object
show all
Defined in:
lib/active_graphql/model/find_in_batches.rb

Overview

fetches graphql paginated records in batches

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, batch_size: 100, fetched_items_count: 0) ⇒ FindInBatches

Returns a new instance of FindInBatches.



11
12
13
14
15
# File 'lib/active_graphql/model/find_in_batches.rb', line 11

def initialize(relation, batch_size: 100, fetched_items_count: 0)
  @relation = relation
  @batch_size = batch_size
  @fetched_items_count = fetched_items_count
end

Class Method Details

.call(*args, &block) ⇒ Object



7
8
9
# File 'lib/active_graphql/model/find_in_batches.rb', line 7

def self.call(*args, &block)
  new(*args).call(&block)
end

Instance Method Details

#call {|items| ... } ⇒ Object

Yields:

  • (items)


17
18
19
20
21
22
23
24
25
# File 'lib/active_graphql/model/find_in_batches.rb', line 17

def call(&block)
  scope = relation.limit(batch_size).offset(offset_size)

  items = scope.first_batch
  return nil if items.empty?

  yield(items)
  fetch_next_batch(items_count: items.count, &block) if scope.next_page?
end