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, #arguments, #before_value, #context, #edge_class, #field, #first, #first_value, #items, #last, #last_value, #parent

Instance Method Summary collapse

Methods inherited from Connection

#after, #before, #default_page_size, #default_page_size=, #edge_nodes, #edges, #end_cursor, #has_default_page_size_override?, #has_max_page_size_override?, #initialize, #max_page_size, #max_page_size=, #page_info, #range_add_edge, #start_cursor

Constructor Details

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

Instance Method Details

#cursor_for(item) ⇒ Object



47
48
49
50
51
52
# File 'lib/graphql/pagination/relation_connection.rb', line 47

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)
  encode(offset.to_s)
end

#has_next_pageObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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
      if @nodes && @nodes.count < first
        false
      else
        relation_larger_than(sliced_nodes, @sliced_nodes_offset, first)
      end
    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