class GraphQL::Relay::RelationConnection

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

Public Instance Methods

cursor_from_node(item) click to toggle source
# File lib/graphql/relay/relation_connection.rb, line 8
def cursor_from_node(item)
  offset = starting_offset + paged_nodes_array.index(item) + 1
  Base64.strict_encode64(offset.to_s)
end
has_next_page() click to toggle source
# File lib/graphql/relay/relation_connection.rb, line 13
def has_next_page
  exceeds_limit?(first)
end
has_previous_page() click to toggle source

Used by `pageInfo`

# File lib/graphql/relay/relation_connection.rb, line 18
def has_previous_page
  exceeds_limit?(last)
end

Private Instance Methods

exceeds_limit?(limit_value) click to toggle source

Returns true if the limit is specified and there are more items that follow

# File lib/graphql/relay/relation_connection.rb, line 91
def exceeds_limit?(limit_value)
  !!(limit_value && sliced_nodes.limit(limit_value + 1).count > limit_value)
end
limit() click to toggle source

Limit to apply to this query:

  • find a value from the query

  • don't exceed max_page_size

  • otherwise, don't limit

# File lib/graphql/relay/relation_connection.rb, line 70
def limit
  @limit ||= begin
    limit_from_arguments = if first
      first
    else
      if previous_offset < 0
        previous_offset + (last ? last : 0)
      else
        last
      end
    end
    [limit_from_arguments, max_page_size].compact.min
  end
end
offset_from_cursor(cursor) click to toggle source
# File lib/graphql/relay/relation_connection.rb, line 40
def offset_from_cursor(cursor)
  Base64.decode64(cursor).to_i
end
paged_nodes() click to toggle source

apply first / last limit results

# File lib/graphql/relay/relation_connection.rb, line 25
def paged_nodes
  @paged_nodes ||= begin
    items = sliced_nodes
    items.limit(limit)
  end
end
paged_nodes_array() click to toggle source
# File lib/graphql/relay/relation_connection.rb, line 85
def paged_nodes_array
  @paged_nodes_array ||= paged_nodes.to_a
end
previous_offset() click to toggle source

Offset from the previous selection, if there was one Otherwise, zero

# File lib/graphql/relay/relation_connection.rb, line 56
def previous_offset
  @previous_offset ||= if after
    offset_from_cursor(after)
  elsif before
    offset_from_cursor(before) - (last ? last : 0) - 1
  else
    0
  end
end
sliced_nodes() click to toggle source

Apply cursors to edges

# File lib/graphql/relay/relation_connection.rb, line 33
def sliced_nodes
  @sliced_nodes ||= begin
    items = object
    items.offset(starting_offset)
  end
end
starting_offset() click to toggle source
# File lib/graphql/relay/relation_connection.rb, line 44
def starting_offset
  @initial_offset ||= begin
    if before
      [previous_offset, 0].max
    else
      previous_offset
    end
  end
end