class ActiveGraphql::Model::FindInBatches

fetches graphql paginated records in batches

Attributes

fetched_items_count[R]
relation[R]

Public Class Methods

call(*args, &block) click to toggle source
# File lib/active_graphql/model/find_in_batches.rb, line 7
def self.call(*args, &block)
  new(*args).call(&block)
end
new(relation, batch_size: 100, fetched_items_count: 0) click to toggle source
# 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

Public Instance Methods

call() { |items| ... } click to toggle source
# 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

Private Instance Methods

batch_size() click to toggle source
# File lib/active_graphql/model/find_in_batches.rb, line 44
def batch_size
  items_to_fetch = collection_limit_number - fetched_items_count if collection_limit_number
  [@batch_size, collection_limit_number, items_to_fetch].compact.min
end
collection_limit_number() click to toggle source
# File lib/active_graphql/model/find_in_batches.rb, line 49
def collection_limit_number
  relation.send(:limit_number)
end
fetch_next_batch(items_count:, &block) click to toggle source
# File lib/active_graphql/model/find_in_batches.rb, line 31
def fetch_next_batch(items_count:, &block)
  self.class.call(
    relation,
    batch_size: batch_size,
    fetched_items_count: fetched_items_count + items_count,
    &block
  )
end
offset_size() click to toggle source
# File lib/active_graphql/model/find_in_batches.rb, line 40
def offset_size
  relation.send(:offset_number).to_i + fetched_items_count
end