class GraphQL::Relay::ArrayConnection

Public Instance Methods

cursor_from_node(item) click to toggle source
# File lib/graphql/relay/array_connection.rb, line 4
def cursor_from_node(item)
  idx = (after ? index_from_cursor(after) : 0) + sliced_nodes.find_index(item) + 1
  encode(idx.to_s)
end
has_next_page() click to toggle source
# File lib/graphql/relay/array_connection.rb, line 9
def has_next_page
  if first
    # There are more items after these items
    sliced_nodes.count > first
  elsif GraphQL::Relay::ConnectionType.bidirectional_pagination && before
    # The original array is longer than the `before` index
    index_from_cursor(before) < nodes.length
  else
    false
  end
end
has_previous_page() click to toggle source
# File lib/graphql/relay/array_connection.rb, line 21
def has_previous_page
  if last
    # There are items preceding the ones in this result
    sliced_nodes.count > last
  elsif GraphQL::Relay::ConnectionType.bidirectional_pagination && after
    # We've paginated into the Array a bit, there are some behind us
    index_from_cursor(after) > 0
  else
    false
  end
end

Private Instance Methods

first() click to toggle source
# File lib/graphql/relay/array_connection.rb, line 35
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
index_from_cursor(cursor) click to toggle source
# File lib/graphql/relay/array_connection.rb, line 77
def index_from_cursor(cursor)
  decode(cursor).to_i
end
last() click to toggle source
# File lib/graphql/relay/array_connection.rb, line 43
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
paged_nodes() click to toggle source

apply first / last limit results

# File lib/graphql/relay/array_connection.rb, line 52
def paged_nodes
  @paged_nodes ||= begin
    items = sliced_nodes

    items = items.first(first) if first
    items = items.last(last) if last
    items = items.first(max_page_size) if max_page_size && !first && !last

    items
  end
end
sliced_nodes() click to toggle source

Apply cursors to edges

# File lib/graphql/relay/array_connection.rb, line 65
def sliced_nodes
  @sliced_nodes ||= if before && after
    nodes[index_from_cursor(after)..index_from_cursor(before)-1] || []
  elsif before
    nodes[0..index_from_cursor(before)-2] || []
  elsif after
    nodes[index_from_cursor(after)..-1] || []
  else
    nodes
  end
end