Class: GraphQL::Pagination::RelationConnection

Inherits:
Connection
  • Object
show all
Defined in:
lib/graphql/pagination/relation_connection.rb

Overview

A generic class for working with database query objects.

Instance Attribute Summary

Attributes inherited from Connection

#after_value, #before_value, #context, #first, #first_value, #items, #last, #last_value, #max_page_size

Instance Method Summary collapse

Methods inherited from Connection

#after, #before, edge_class, #edge_nodes, #edges, #end_cursor, #initialize, #page_info, #start_cursor

Constructor Details

This class inherits a constructor from GraphQL::Pagination::Connection

Instance Method Details

#cursor_for(item) ⇒ Object



43
44
45
46
47
48
# File 'lib/graphql/pagination/relation_connection.rb', line 43

def cursor_for(item)
  load_nodes
  # index in nodes + existing offset + 1 (because it's offset, not index)
  offset = nodes.index(item) + 1 + (@paged_nodes_offset || 0) + (relation_offset(items) || 0)
  context.schema.cursor_encoder.encode(offset.to_s)
end

#has_next_pageObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/graphql/pagination/relation_connection.rb', line 30

def has_next_page
  if @has_next_page.nil?
    @has_next_page = if @before_offset && @before_offset > 0
      true
    elsif first
      relation_count(set_limit(sliced_nodes, first + 1)) == first + 1
    else
      false
    end
  end
  @has_next_page
end

#has_previous_pageObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/graphql/pagination/relation_connection.rb', line 13

def has_previous_page
  if @has_previous_page.nil?
    @has_previous_page = if @after_offset && @after_offset > 0
      true
    elsif last
      # See whether there are any nodes _before_ the current offset.
      # If there _is no_ current offset, then there can't be any nodes before it.
      # Assume that if the offset is positive, there are nodes before the offset.
      limited_nodes
      !(@paged_nodes_offset.nil? || @paged_nodes_offset == 0)
    else
      false
    end
  end
  @has_previous_page
end

#nodesObject



8
9
10
11
# File 'lib/graphql/pagination/relation_connection.rb', line 8

def nodes
  load_nodes
  @nodes
end