Skip to content

Instantly share code, notes, and snippets.

@handshake-engineering-blog
Created April 19, 2021 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save handshake-engineering-blog/9ca3d49d99fbb3b7d328122cd780a978 to your computer and use it in GitHub Desktop.
Save handshake-engineering-blog/9ca3d49d99fbb3b7d328122cd780a978 to your computer and use it in GitHub Desktop.
1977385383_07
class CustomConnection < GraphQL::Pagination::Connection
def nodes
results.slice(0, page_size) # Remove the extra result we fetched to check if there's another page
end
def cursor_for(item)
Base64.encode64(item.id.to_s)
end
def has_next_page
results.size > page_size
end
# Always return false because we're not implementing backwards pagination yet
def has_previous_page
false
end
def page_size
@first_value || max_page_size
end
def results
@_results ||= begin
# If there’s an after cursor, decode it and only query for records with an id that come after that cursor
@items = @items.where('id > ?', Base64.decode64(@after_value)) if @after_value.present?
@items.limit(page_size + 1) # Fetch one extra result to determine if there's another page
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment