strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
421 stars 123 forks source link

Fix calculation of `has_next_page` in `resolve_connection_from_cache` #622

Closed SupImDos closed 2 months ago

SupImDos commented 2 months ago

Description

The strawberry_django.relay.ListConnectionWithTotalCount.resolve_connection_from_cache method currently incorrectly attempts to determine has_next_page from the QuerySet._result_cache, as opposed to the cached records within it, resulting in an AttributeError being raised.

This PR ensures that has_next_page is determined via the last record in the QuerySet._result_cache (if applicable), so that we don't get an AttributeError

Types of Changes

Issues Fixed or Closed by This PR

Checklist

Summary by Sourcery

Fix the calculation of has_next_page in resolve_connection_from_cache to prevent an AttributeError by using the last record in the QuerySet._result_cache.

Bug Fixes:

sourcery-ai[bot] commented 2 months ago

Reviewer's Guide by Sourcery

This pull request fixes a bug in the resolve_connection_from_cache method of the ListConnectionWithTotalCount class in the strawberry_django.relay module. The bug was causing an AttributeError when attempting to determine the has_next_page value. The implementation has been updated to correctly calculate has_next_page using the cached records.

File-Level Changes

Change Details Files
Fixed calculation of has_next_page in resolve_connection_from_cache method
  • Removed incorrect calculation of has_next_page using result._strawberry_row_number
  • Added new calculation for has_next_page using the last record in result
  • Updated has_previous_page calculation to use result[0] instead of nodes[0]
  • Added null checks to prevent errors when result is empty
strawberry_django/relay.py

Tips - Trigger a new Sourcery review by commenting `@sourcery-ai review` on the pull request. - Continue your discussion with Sourcery by replying directly to review comments. - You can change your review settings at any time by accessing your [dashboard](https://app.sourcery.ai): - Enable or disable the Sourcery-generated pull request summary or reviewer's guide; - Change the review language; - You can always [contact us](mailto:support@sourcery.ai) if you have any questions or feedback.