Class: GraphQL::Relay::ArrayConnection

Inherits:
BaseConnection show all
Defined in:
lib/graphql/relay/array_connection.rb

Constant Summary

Constants inherited from BaseConnection

BaseConnection::CONNECTION_IMPLEMENTATIONS, BaseConnection::CURSOR_SEPARATOR

Instance Attribute Summary

Attributes inherited from BaseConnection

#arguments, #context, #field, #max_page_size, #nodes, #parent

Instance Method Summary collapse

Methods inherited from BaseConnection

#after, #before, connection_for_nodes, #decode, #edge_nodes, #encode, #end_cursor, #initialize, #inspect, #page_info, register_connection_implementation, #start_cursor

Constructor Details

This class inherits a constructor from GraphQL::Relay::BaseConnection

Instance Method Details

#cursor_from_node(item) ⇒ Object



5
6
7
8
# File 'lib/graphql/relay/array_connection.rb', line 5

def cursor_from_node(item)
  idx = (after ? index_from_cursor(after) : 0) + sliced_nodes.find_index(item) + 1
  encode(idx.to_s)
end

#has_next_pageObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/graphql/relay/array_connection.rb', line 10

def has_next_page
  if first
    # There are more items after these items
    sliced_nodes.count > first
  elsif GraphQL::Relay::ConnectionType.bidirectional_pagination && before
    # The original array is longer than the `before` index
    index_from_cursor(before) < nodes.length
  else
    false
  end
end

#has_previous_pageObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphql/relay/array_connection.rb', line 22

def has_previous_page
  if last
    # There are items preceding the ones in this result
    sliced_nodes.count > last
  elsif GraphQL::Relay::ConnectionType.bidirectional_pagination && after
    # We've paginated into the Array a bit, there are some behind us
    index_from_cursor(after) > 0
  else
    false
  end
end