Class: ActiveShopifyGraphQL::Response::PaginatedResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_shopify_graphql/response/paginated_result.rb

Overview

Represents a page of results from a paginated GraphQL query. Lazily builds model instances from attribute hashes on access. Provides methods to navigate between pages and access pagination metadata.

Examples:

Manual pagination

page = ProductVariant.where(sku: "*").in_pages(of: 10)
page.has_next_page? # => true
next_page = page.next_page

Iteration with block

ProductVariant.where(sku: "*").in_pages(of: 10) do |page|
  page.each { |variant| process(variant) }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes:, model_class:, page_info:, query_scope:) ⇒ PaginatedResult

Returns a new instance of PaginatedResult.



23
24
25
26
27
28
29
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 23

def initialize(attributes:, model_class:, page_info:, query_scope:)
  @attributes = attributes
  @model_class = model_class
  @page_info = page_info
  @query_scope = query_scope
  @records = nil # Lazily built
end

Instance Attribute Details

#page_infoObject (readonly)

Returns the value of attribute page_info.



21
22
23
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 21

def page_info
  @page_info
end

#query_scopeObject (readonly)

Returns the value of attribute query_scope.



21
22
23
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 21

def query_scope
  @query_scope
end

Instance Method Details

#[](index) ⇒ Object

Access records by index



42
43
44
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 42

def [](index)
  records[index]
end

#all_recordsArray

Return all records across all pages Warning: This will make multiple API calls if there are many pages

Returns:

  • (Array)

    All records from all pages



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 101

def all_records
  all = records.dup
  current = self

  while current.has_next_page?
    current = current.next_page
    all.concat(current.records)
  end

  all
end

#each(&block) ⇒ Object

Iterate over records in this page



37
38
39
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 37

def each(&block)
  records.each(&block)
end

#empty?Boolean

Check if this page has records

Returns:

  • (Boolean)


53
54
55
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 53

def empty?
  @attributes.empty?
end

#end_cursorObject

Cursor pointing to the end of this page



73
74
75
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 73

def end_cursor
  @page_info.end_cursor
end

#has_next_page?Boolean

Check if there is a next page

Returns:

  • (Boolean)


58
59
60
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 58

def has_next_page?
  @page_info.has_next_page?
end

#has_previous_page?Boolean

Check if there is a previous page

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 63

def has_previous_page?
  @page_info.has_previous_page?
end

#next_pagePaginatedResult?

Fetch the next page of results

Returns:



79
80
81
82
83
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 79

def next_page
  return nil unless has_next_page?

  @query_scope.fetch_page(after: end_cursor)
end

#previous_pagePaginatedResult?

Fetch the previous page of results

Returns:



87
88
89
90
91
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 87

def previous_page
  return nil unless has_previous_page?

  @query_scope.fetch_page(before: start_cursor)
end

#recordsObject

Get the records for this page (builds instances on first access)



32
33
34
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 32

def records
  @records ||= ModelBuilder.build_many(@model_class, @attributes)
end

#sizeObject Also known as: length

Number of records in this page



47
48
49
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 47

def size
  @attributes.size
end

#start_cursorObject

Cursor pointing to the start of this page



68
69
70
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 68

def start_cursor
  @page_info.start_cursor
end

#to_aObject

Convert to array (useful for compatibility)



94
95
96
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 94

def to_a
  records.dup
end