strawberry-graphql / strawberry-django

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

fix(optimizer): Convert select_related into Prefetch when the type defines a custom get_queryset #583

Closed bellini666 closed 4 months ago

bellini666 commented 4 months ago

Fix #572 Initial resolution for #549

Summary by Sourcery

This pull request addresses a bug where select_related was not respecting custom get_queryset methods by converting them into Prefetch. It also introduces a helper function to streamline type retrieval and updates the optimizer to avoid optimizing fields with a base resolver unless auto-generated. Additionally, a new test was added to verify the fallback behavior.

sourcery-ai[bot] commented 4 months ago

Reviewer's Guide by Sourcery

This pull request addresses issue #572 by refactoring the optimizer to convert select_related into Prefetch when the type defines a custom get_queryset. Key changes include the introduction of a helper function to resolve Django types, updates to the optimizer logic, and the addition of new tests to verify the behavior.

File-Level Changes

Files Changes
strawberry_django/optimizer.py
strawberry_django/fields/field.py
Refactored optimizer to use Prefetch when a custom get_queryset is defined and added optimization flag to default_resolver.
tests/test_optimizer.py
tests/projects/schema.py
Added tests to verify the new Prefetch behavior and updated schema definitions accordingly.

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.
codecov-commenter commented 4 months ago

Codecov Report

Attention: Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.90%. Comparing base (3aa993a) to head (538dbe5).

Files Patch % Lines
strawberry_django/optimizer.py 88.23% 2 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #583 +/- ## ========================================== + Coverage 88.87% 88.90% +0.03% ========================================== Files 41 41 Lines 3585 3595 +10 ========================================== + Hits 3186 3196 +10 Misses 399 399 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.