graphql-relay-py icon indicating copy to clipboard operation
graphql-relay-py copied to clipboard

n+1 pagination

Open sebastiandev opened this issue 6 years ago • 0 comments

Related to this issue

Adds a n+1 strategy for paginating connections which make paginating a lot more performant avoiding the need to call on list() or length() on the iterator (which ends up consuming the entire iterator). We just get the desired amount of results and try to fetch one extra item. If there's a result it means there are more results, otherwise its the end of the iteration/pagination.

The new method connection_from_list_slice_lazy is optional, so instead of replacing the original one, those who want to use it can override the connection field like this:

class SmartConnectionField(ConnectionField):
    @classmethod
    def connection_resolver(cls, resolver, connection_type, root, info, **args):
        resolved = resolver(root, info, **args)

        if resolved is None:
            resolved = []

        connection = connection_from_list_slice_lazy(
            resolved,
            args,
            connection_type=connection_type,
            pageinfo_type=PageInfo,
            edge_type=connection_type.Edge,
        )

        connection.iterable = resolved
        return connection

sebastiandev avatar Sep 23 '19 15:09 sebastiandev