Class: ShopifyAPI::GraphQL::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_api/graphql/tiny.rb

Overview

:nodoc:

Constant Summary collapse

NEXT_PAGE_KEYS =
{
  :before => %w[hasPreviousPage startCursor].freeze,
  :after  => %w[hasNextPage endCursor].freeze
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(gql, *options) ⇒ Pager

Returns a new instance of Pager.



338
339
340
341
# File 'lib/shopify_api/graphql/tiny.rb', line 338

def initialize(gql, *options)
  @gql = gql
  @options = normalize_options(options)
end

Instance Method Details

#execute(q, variables = nil) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/shopify_api/graphql/tiny.rb', line 343

def execute(q, variables = nil)
  unless pagination_variable_exists?(q)
    raise ArgumentError, "query does not contain the pagination variable '#{@options[:variable]}'"
  end

  variables ||= {}
  pagination_finder = @options[@options[:direction]]

  enumerator = Enumerator.new do |y|
    loop do
      page = @gql.execute(q, variables)
      y << page

      cursor = pagination_finder[page]
      break unless cursor

      variables[@options[:variable]] = cursor
    end
  end

  if block_given?
    enumerator.each { |page| yield page }
  else
    enumerator.lazy
  end
end