Class: GraphQL::Connections::KeyAsc

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

Overview

Implements pagination by one field with asc order

Instance Attribute Summary

Attributes inherited from Base

#opaque_cursor

Instance Method Summary collapse

Methods inherited from Base

#nodes, #primary_key

Constructor Details

#initialize(*args, primary_key: nil, **kwargs) ⇒ KeyAsc

Returns a new instance of KeyAsc.



7
8
9
10
11
# File 'lib/graphql/connections/key_asc.rb', line 7

def initialize(*args, primary_key: nil, **kwargs)
  @primary_key = primary_key

  super(*args, **kwargs)
end

Instance Method Details

#cursor_for(item) ⇒ Object



33
34
35
36
37
# File 'lib/graphql/connections/key_asc.rb', line 33

def cursor_for(item)
  cursor = serialize(item[primary_key])
  cursor = encode(cursor) if opaque_cursor
  cursor
end

#has_next_pageObject



23
24
25
26
27
28
29
30
31
# File 'lib/graphql/connections/key_asc.rb', line 23

def has_next_page
  if first
    nodes.any? && items.where(arel_table[primary_key].gt(nodes.last[primary_key])).exists?
  elsif before
    items.where(arel_table[primary_key].gteq(before_cursor)).exists?
  else
    false
  end
end

#has_previous_pageObject



13
14
15
16
17
18
19
20
21
# File 'lib/graphql/connections/key_asc.rb', line 13

def has_previous_page
  if last
    nodes.any? && items.where(arel_table[primary_key].lt(nodes.first[primary_key])).exists?
  elsif after
    items.where(arel_table[primary_key].lteq(after_cursor)).exists?
  else
    false
  end
end