Repository navigation
Description
Problem
The default pagination approach used for Django querysets executes a .count() on the query. For tables with hundreds of thousands or millions of rows, this is very slow on Postgres. It also assumes that the set is stable, which somewhat defeats the point of using a cursor based pagination system in the first place.
Possible solution
I released today django-cursor-pagination which provides a paginator class which fulfils the relay cursor connection spec. It is available on PyPI.
Below is a pretty simple approach to using this in Graphene, with inspiration taken from graphene-gae - thanks to @ekampf. It would want some checks around full_args, and possibly a means to explicitly set the ordering. It would also need testing and documenting.
Should we use this in graphene? There are some caveats of using this approach. Most importantly, the ordering fields in my current implementation must uniquely determine the items in the queryset. (Discussion of this restriction). Perhaps we should provide both options?
import graphene
from cursor_pagination import CursorPaginator
def connection_from_cursor_paginated(queryset, args, connection_type, edge_type, pageinfo_type, **kwargs):
paginator = CursorPaginator(queryset, queryset.query.order_by)
full_args = dict(args, **kwargs)
page = paginator.page(**full_args)
edges = []
for item in page:
edge = edge_type(node=item, cursor=paginator.cursor(item))
edges.append(edge)
return connection_type(
edges=edges,
page_info=pageinfo_type(
start_cursor=paginator.cursor(page[0]),
end_cursor=paginator.cursor(page[-1]),
has_previous_page=page.has_previous,
has_next_page=page.has_next,
)
)
class CursorPaginatedConnection(graphene.relay.types.Connection):
@classmethod
def from_list(cls, queryset, args, context, info):
connection = connection_from_cursor_paginated(queryset, args, connection_type=cls, edge_type=cls.edge_type, pageinfo_type=graphene.relay.PageInfo)
connection.set_connection_data(queryset)
return connection
class CursorPaginatedConnectionField(graphene.relay.ConnectionField):
def __init__(self, *args, **kwargs):
kwargs['connection_type'] = kwargs.pop('connection_type', CursorPaginatedConnection)
super(CursorPaginatedConnectionField, self).__init__(*args, **kwargs)
Activity
syrusakbary commentedon May 23, 2016
I think is worth to take a closer look into this approach.
Also, having an order that strictly determine all the items in the queryset is somewhat essential for pagination.
My ideas here are:
CursorPaginatedConnectionwhen possible (if a query order its defined).Let's play with it in a branch that could let us see the edge cases
mjtamlyn commentedon May 24, 2016
Looking at this a bit more in the context of #62 I wonder whether we can reduce the plumbing duplication needed for various different data backends. The core logical structure is the same for my Django implementation, for the GAE one, and for the rethinkdb cursor mentioned in #62.
Ideally what we want is an easy way to add a
Cursorsubclass or similar which has some defined API, possibly similar to what I defined indjango-cursor-pagination. It needs to be able to:page(first, last, after, before), which returns an object with resultant set of items, and the data needed forPageInfo.Ideally you could then specify the cursor as a kwarg and/or attribute of
graphene.relay.ConnectionFieldand everything else would follow. A built inDjangoCursorcould swap between usingCursorPaginatoror the current count/slice approach as needed.defrex commentedon Jun 3, 2016
I would love this.
I'll underscore one point. Right now you can't lookup a node's cursor without the full context of the connection. This means returning an edge from a mutation requires a ton of overhead and possibly knowledge you don't have. For that reason the ability to look up a cursor for a given node without any additional information should be a goal. This issue has been discussed a lot in #59.
Also, to solve the issue of a missing order value, you could order by primary key by default.
crucialfelix commentedon Mar 27, 2017
The example at the top doesn't work now because you cannot make an abstract subclass of
graphene.relay.Connection.Just by importing this (not using it, just defining the class):
raises:
ConnectionMetadoes some error checking to ensure that it's subclasses are usable:https://github.com/graphql-python/graphene/blob/v1.2.0/graphene/relay/connection.py#L61
I've been running into issues with these meta classes quite a bit when working with graphene. You cannot write an abstract subclass of
DjangoObjectTypefor the same reason — the error checking scolds you.I guess these reusable things should be written as plain classes and then apply them as mixins. But then to actually use them we need to write factory methods to assemble a one off class and use it immediately.
mjtamlyn commentedon Mar 30, 2017
The example at the top worked on Graphene 0.X, you're right it doesn't work with 1.X.
I redefined
Connectionmore completely on graphene 1.X to make it work:I also had to add a different default resolver to the field.
necnec commentedon Oct 10, 2017
@mjtamlyn you said you also added a resolver?
Could you provide any working example with your custom paginator?
Merge pull request graphql-python#180 from arianon/master
ramusus commentedon Feb 21, 2018
@mjtamlyn could you please update your approach for latest
there is no such class as
graphene.relay.connection.ConnectionMetastale commentedon Jul 29, 2019
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.