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

Direct Known Subclasses

MongoRelationConnection

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



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.index(item)
  if item_index.nil?
    raise("Can't generate cursor, item not found in connection: #{item}")
  else
    offset = item_index + 1 + ((paged_nodes_offset || 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



48
49
50
51
52
53
54
# File 'lib/graphql/relay/relation_connection.rb', line 48

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
29
30
31
32
33
34
# File 'lib/graphql/relay/relation_connection.rb', line 26

def has_next_page
  if first
    paged_nodes.length >= first && sliced_nodes_count > first
  elsif GraphQL::Relay::ConnectionType.bidirectional_pagination && last
    sliced_nodes_count >= last
  else
    false
  end
end

#has_previous_pageObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/graphql/relay/relation_connection.rb', line 36

def has_previous_page
  if last
    paged_nodes.length >= last && sliced_nodes_count > last
  elsif GraphQL::Relay::ConnectionType.bidirectional_pagination && after
    # We've already paginated through the collection a bit,
    # there are nodes behind us
    offset_from_cursor(after) > 0
  else
    false
  end
end

#lastObject



56
57
58
59
60
61
62
# File 'lib/graphql/relay/relation_connection.rb', line 56

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