Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n+1 pagination #27

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sebastiandev
Copy link

@sebastiandev sebastiandev commented Sep 23, 2019

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

… a lot more performant avoiding the need to call on list() or length() on the 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, otherwhise its the end of the iteration/pagination.
@sebastiandev sebastiandev requested a review from Cito as a code owner July 6, 2020 13:18
before_offset = get_offset_with_default(before)
else:
try:
before_offset = int(list_slice.count())
Copy link

@lucas-bremond lucas-bremond May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this defeating the purpose of this PR, since providing last would trigger a count?

There is likely no workaround, but I believe this should at least be made explicit that first and last do not exhibit symmetrical behaviors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants