Class: GraphitiGql::Schema::Connection

Inherits:
GraphQL::Pagination::Connection
  • Object
show all
Defined in:
lib/graphiti_gql/schema/connection.rb

Direct Known Subclasses

ToManyConnection

Instance Method Summary collapse

Instance Method Details

#cursor_for(item) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graphiti_gql/schema/connection.rb', line 28

def cursor_for(item)
  nodes
  starting_offset = 0
  page_param = proxy.query.pagination
  if (page_number = page_param[:number])
    page_size = page_param[:size] || proxy.resource.default_page_size
    starting_offset = (page_number - 1) * page_size
  end
  
  if (cursor = page_param[:after])
    starting_offset = cursor[:offset]
  end
  
  current_offset = @items.index(item)
  offset = starting_offset + current_offset + 1 # (+ 1 b/c o-base index)
  Base64.encode64({offset: offset}.to_json).chomp
end

#has_next_pageObject



21
22
23
24
25
26
# File 'lib/graphiti_gql/schema/connection.rb', line 21

def has_next_page
  nodes
  return false if @items.length.zero?
  cursor = JSON.parse(Base64.decode64(cursor_for(@items.last)))
  cursor["offset"] < @proxy.pagination.send(:item_count)
end

#has_previous_pageObject



17
18
19
# File 'lib/graphiti_gql/schema/connection.rb', line 17

def has_previous_page
  proxy.pagination.has_previous_page?
end

#nodesObject



4
5
6
7
8
9
10
# File 'lib/graphiti_gql/schema/connection.rb', line 4

def nodes
  return @items if @run_once
  @proxy = @items.proxy
  @items = @items.data
  @run_once = true
  @items
end

#proxyObject



12
13
14
15
# File 'lib/graphiti_gql/schema/connection.rb', line 12

def proxy
  nodes
  @proxy
end