class GraphQL::Relay::ArrayConnection

Public Instance Methods

cursor_from_node(item) click to toggle source
# File lib/graphql/relay/array_connection.rb, line 5
def cursor_from_node(item)
  idx = (after ? index_from_cursor(after) : 0) + sliced_nodes.find_index(item) + 1
  encode(idx.to_s)
end
first() click to toggle source
# File lib/graphql/relay/array_connection.rb, line 34
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_page() click to toggle source
# File lib/graphql/relay/array_connection.rb, line 10
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 + 1
  else
    false
  end
end
has_previous_page() click to toggle source
# File lib/graphql/relay/array_connection.rb, line 22
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
last() click to toggle source
# File lib/graphql/relay/array_connection.rb, line 44
def last
  @last ||= limit_pagination_argument(arguments[:last], max_page_size)
end

Private Instance Methods

index_from_cursor(cursor) click to toggle source
# File lib/graphql/relay/array_connection.rb, line 76
def index_from_cursor(cursor)
  decode(cursor).to_i
end
paged_nodes() click to toggle source

apply first / last limit results

# File lib/graphql/relay/array_connection.rb, line 51
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 64
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