class GraphQL::Relay::BaseConnection


- {#max_page_size} (the specified maximum page size that can be returned from a connection)
- {#first}, {#after}, {#last}, {#before} (arguments passed to the field)
- {#nodes}, the collection which the connection will wrap
In a subclass, you have access to
- {#paged_nodes}, which applies ‘first` & `last` limits
- {#sliced_nodes}, which slices by `before` & `after`
- {#cursor_from_node}, which returns an opaque cursor for the given item
Subclasses must implement:

def after

Returns:
  • (String, nil) -
def after
  arguments[:after]
end

def before

Returns:
  • (String, nil) -
def before
  arguments[:before]
end

def connection_for_nodes(nodes)

Returns:
  • (subclass of BaseConnection) - a connection Class for wrapping `nodes`

Parameters:
  • nodes (Object) -- A collection of nodes (eg, Array, AR::Relation)
def connection_for_nodes(nodes)
  # If it's a new-style connection object, it's already ready to go
  if nodes.is_a?(GraphQL::Pagination::Connection)
    return nodes
  end
  # Check for class _names_ because classes can be redefined in Rails development
  nodes.class.ancestors.each do |ancestor|
    conn_impl = CONNECTION_IMPLEMENTATIONS[ancestor.name]
    if conn_impl
      return conn_impl
    end
  end
  # Should have found a connection during the loop:
  raise("No connection implementation to wrap #{nodes.class} (#{nodes})")
end

def cursor_from_node(object)

An opaque operation which returns a connection-specific cursor.
def cursor_from_node(object)
  raise GraphQL::RequiredImplementationMissingError, "must return a cursor for this object/connection pair"
end

def decode(data)

def decode(data)
  @encoder.decode(data, nonce: true)
rescue ArgumentError
  raise GraphQL::ExecutionError, "Invalid cursor: #{data.inspect}"
end

def edge_nodes

probably wrapped by {GraphQL::Relay::Edge}
These are the nodes to render for this connection,
def edge_nodes
  @edge_nodes ||= paged_nodes
end

def encode(data)

def encode(data)
  @encoder.encode(data, nonce: true)
end

def end_cursor

Used by `pageInfo`
def end_cursor
  if end_node = (respond_to?(:paged_nodes_array, true) ? paged_nodes_array : paged_nodes).last
    return cursor_from_node(end_node)
  else
    return nil
  end
end

def first

Returns:
  • (Integer, nil) -
def first
  @first ||= get_limited_arg(:first)
end

def get_limited_arg(arg_name)

Return a sanitized `arguments[arg_name]` (don't allow negatives)
def get_limited_arg(arg_name)
  arg_value = arguments[arg_name]
  if arg_value.nil?
    arg_value
  elsif arg_value < 0
    0
  else
    arg_value
  end
end

def has_next_page

Used by `pageInfo`
def has_next_page
  !!(first && sliced_nodes.count > first)
end

def has_previous_page

Used by `pageInfo`
def has_previous_page
  !!(last && sliced_nodes.count > last)
end

def initialize(nodes, arguments, field: nil, max_page_size: nil, parent: nil, context: nil)

Parameters:
  • context (GraphQL::Query::Context) -- The context from the field being resolved
  • parent (Object) -- The object which this collection belongs to
  • max_page_size (Int) -- The maximum number of results to return
  • field (GraphQL::Field) -- The underlying field
  • arguments (GraphQL::Query::Arguments) -- Query arguments
  • nodes (Object) -- The collection of nodes
def initialize(nodes, arguments, field: nil, max_page_size: nil, parent: nil, context: nil)
  @context = context
  @nodes = nodes
  @arguments = arguments
  @field = field
  @parent = parent
  @encoder = context ? @context.schema.cursor_encoder : GraphQL::Schema::Base64Encoder
  @max_page_size = max_page_size.nil? && context ? @context.schema.default_max_page_size : max_page_size
end

def inspect

def inspect
  "#<GraphQL::Relay::Connection @parent=#{@parent.inspect} @arguments=#{@arguments.to_h.inspect}>"
end

def last

Returns:
  • (Integer, nil) -
def last
  @last ||= get_limited_arg(:last)
end

def page_info

Support the `pageInfo` field
def page_info
  self
end

def paged_nodes

def paged_nodes
  raise GraphQL::RequiredImplementationMissingError, "must return nodes for this connection after paging"
end

def register_connection_implementation(nodes_class, connection_class)

Parameters:
  • connection_class (Class) -- A class implementing Connection methods
  • nodes_class (Class) -- A class representing a collection (eg, Array, AR::Relation)
def register_connection_implementation(nodes_class, connection_class)
  CONNECTION_IMPLEMENTATIONS[nodes_class.name] = connection_class
end

def sliced_nodes

def sliced_nodes
  raise GraphQL::RequiredImplementationMissingError, "must return  all nodes for this connection after chopping off first and last"
end

def start_cursor

Used by `pageInfo`
def start_cursor
  if start_node = (respond_to?(:paged_nodes_array, true) ? paged_nodes_array : paged_nodes).first
    return cursor_from_node(start_node)
  else
    return nil
  end
end