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

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

Merged
merged 1 commit into from
Jul 14, 2024

Conversation

bellini666
Copy link
Member

@bellini666 bellini666 commented Jul 13, 2024

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.

  • Bug Fixes:
    • Fixed an issue where select_related was not respecting custom get_queryset methods by converting them into Prefetch.
  • Enhancements:
    • Introduced a helper function _get_django_type to streamline the retrieval of Django types from Strawberry fields.
    • Updated the optimizer to avoid optimizing fields with a base resolver unless they are auto-generated by the system.
  • Tests:
    • Added a test to ensure select_related falls back to Prefetch when the type defines a custom get_queryset method.

@bellini666 bellini666 self-assigned this Jul 13, 2024
Copy link
Contributor

sourcery-ai bot commented Jul 13, 2024

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:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @bellini666 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

tests/test_optimizer.py Show resolved Hide resolved
strawberry_django/optimizer.py Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Jul 13, 2024

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 ⚠️
Additional details and impacted files
@@            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              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bellini666 bellini666 merged commit c7c50e9 into main Jul 14, 2024
22 checks passed
@bellini666 bellini666 deleted the use_prefetch branch July 14, 2024 13:01
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.

Optimizer ignores custom type querysets for non-list ForeignKey relations
3 participants