Class: GraphQL::Connections::Keyset::Asc

Inherits:
Base
  • Object
show all
Defined in:
lib/graphql/connections/keyset/asc.rb

Overview

Implements keyset pagination by two fields with asc order

Constant Summary

Constants inherited from Base

Base::SEPARATOR

Instance Attribute Summary

Attributes inherited from Base

#field_key

Attributes inherited from Base

#opaque_cursor

Instance Method Summary collapse

Methods inherited from Base

#cursor_for, #initialize

Methods inherited from Base

#cursor_for, #initialize, #nodes, #primary_key

Constructor Details

This class inherits a constructor from GraphQL::Connections::Keyset::Base

Instance Method Details

#has_next_pageObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graphql/connections/keyset/asc.rb', line 27

def has_next_page
  if first
    nodes.any? &&
      items.where(arel_table[field_key].eq(nodes.last[field_key]))
        .where(arel_table[primary_key].gt(nodes.last[primary_key]))
        .or(items.where(arel_table[field_key].gt(nodes.last[field_key])))
        .exists?
  elsif before
    items
      .where(arel_table[field_key].gt(before_cursor_date))
      .or(
        items.where(arel_table[field_key].eq(before_cursor_date))
          .where(arel_table[primary_key].gt(before_cursor_primary_key))
      ).exists?
  else
    false
  end
end

#has_previous_pageObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/graphql/connections/keyset/asc.rb', line 8

def has_previous_page
  if last
    nodes.any? &&
      items.where(arel_table[field_key].eq(nodes.first[field_key]))
        .where(arel_table[primary_key].lt(nodes.first[primary_key]))
        .or(items.where(arel_table[field_key].lt(nodes.first[field_key])))
        .exists?
  elsif after
    items
      .where(arel_table[field_key].lt(after_cursor_date))
      .or(
        items.where(arel_table[field_key].eq(after_cursor_date))
          .where(arel_table[primary_key].lt(after_cursor_primary_key))
      ).exists?
  else
    false
  end
end