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



52
53
54
55
56
57
58
59
60
# File 'lib/graphql/relay/relation_connection.rb', line 52

def first
  @first ||= begin
    capped = limit_pagination_argument(arguments[:first], max_page_size)
    if capped.nil? && last.nil?
      capped = max_page_size
    end
    capped
  end
end

#has_next_pageObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphql/relay/relation_connection.rb', line 26

def has_next_page
  if first
    if defined?(ActiveRecord::Relation) && nodes.is_a?(ActiveRecord::Relation)
      initial_offset = after ? offset_from_cursor(after) : 0
      return paged_nodes.length >= first && nodes.offset(first + initial_offset).exists?
    end
    return paged_nodes.length >= first && sliced_nodes_count > first
  end
  if GraphQL::Relay::ConnectionType.bidirectional_pagination && last
    return sliced_nodes_count >= last
  end
  false
end

#has_previous_pageObject



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

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



62
63
64
# File 'lib/graphql/relay/relation_connection.rb', line 62

def last
  @last ||= limit_pagination_argument(arguments[:last], max_page_size)
end