Class: GraphQL::Relay::RelationConnection

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

Overview

A connection implementation to expose SQL collection objects. It works for:

  • ActiveRecord::Relation
  • Sequel::Dataset

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, #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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/graphql/relay/relation_connection.rb', line 9

def cursor_from_node(item)
  item_index = paged_nodes_array.index(item)
  if item_index.nil?
    raise("Can't generate cursor, item not found in connection: #{item}")
  else
    offset = item_index + 1 + ((relation_offset(paged_nodes) || 0) - (relation_offset(sliced_nodes) || 0))

    if after
      offset += offset_from_cursor(after)
    elsif before
      offset += offset_from_cursor(before) - 1 - sliced_nodes_count
    end

    encode(offset.to_s)
  end
end

#firstObject



34
35
36
37
38
39
40
# File 'lib/graphql/relay/relation_connection.rb', line 34

def first
  return @first if defined? @first

  @first = get_limited_arg(:first)
  @first = max_page_size if @first && max_page_size && @first > max_page_size
  @first
end

#has_next_pageObject



26
27
28
# File 'lib/graphql/relay/relation_connection.rb', line 26

def has_next_page
  !!(first && sliced_nodes_count > first)
end

#has_previous_pageObject



30
31
32
# File 'lib/graphql/relay/relation_connection.rb', line 30

def has_previous_page
  !!(last && sliced_nodes_count > last)
end

#lastObject



42
43
44
45
46
47
48
# File 'lib/graphql/relay/relation_connection.rb', line 42

def last
  return @last if defined? @last

  @last = get_limited_arg(:last)
  @last = max_page_size if @last && max_page_size && @last > max_page_size
  @last
end