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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gql, *options) ⇒ Pager

Returns a new instance of Pager.



406
407
408
409
# File 'lib/shopify_api/graphql/tiny.rb', line 406

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

Instance Attribute Details

#cursor ⇒ Object (readonly)

Returns the value of attribute cursor.



404
405
406
# File 'lib/shopify_api/graphql/tiny.rb', line 404

def cursor
  @cursor
end

Instance Method Details

#execute(q, variables = nil) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/shopify_api/graphql/tiny.rb', line 411

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

  variables = (variables || {}).dup
  @cursor = variables[@options[:variable]] || variables[@options[:variable].to_sym]
  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