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): Prevent issuing duplicated queries for certain uses of first() and get() #675

Conversation

diesieben07
Copy link
Contributor

@diesieben07 diesieben07 commented Dec 15, 2024

For "many-type" fields (i.e. ManyToManyField or a reverse FK) when the corresponding GraphQL field has a singular result, StrawberryDjangoField will use QuerySet#get and QuerySet#first to produce the result. This prevents the QuerySet from using any prefetched data, because those methods do an implicit slice.
This PR adds a check to not call get/first in case the QuerySet is prefetched and instead mimic their behavior to prevent duplicated queries.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Fixes #662

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Summary by Sourcery

Fix duplicated queries in StrawberryDjangoField by avoiding get() and first() on prefetched QuerySets and add tests to verify the fix.

Bug Fixes:

  • Prevent duplicated queries by avoiding the use of get() and first() on prefetched QuerySets in StrawberryDjangoField.

Tests:

  • Add tests to verify the behavior of prefetched QuerySets with singular GraphQL fields, ensuring no duplicated queries occur.

Copy link
Contributor

sourcery-ai bot commented Dec 15, 2024

Reviewer's Guide by Sourcery

This PR optimizes the handling of prefetched QuerySets in StrawberryDjangoField by preventing duplicate queries when using first() and get() methods. The implementation checks if a QuerySet is prefetched and, if so, mimics the behavior of these methods using the prefetched data instead of executing additional database queries.

Sequence diagram for optimized QuerySet handling

sequenceDiagram
    participant User
    participant StrawberryDjangoField
    participant QuerySet
    participant Database

    User->>StrawberryDjangoField: Request data
    StrawberryDjangoField->>QuerySet: Check if prefetched
    alt Prefetched
        QuerySet->>StrawberryDjangoField: Return prefetched data
        StrawberryDjangoField->>User: Return data
    else Not prefetched
        QuerySet->>Database: Execute query
        Database->>QuerySet: Return data
        QuerySet->>StrawberryDjangoField: Return data
        StrawberryDjangoField->>User: Return data
    end
Loading

File-Level Changes

Change Details Files
Added optimization for prefetched QuerySets to prevent duplicate queries
  • Added check for prefetched QuerySets using is_optimized_by_prefetching()
  • Implemented custom handling of first() using next(iter(qs), None)
  • Implemented custom get() behavior with proper error handling for zero or multiple results
strawberry_django/fields/field.py
Added comprehensive test coverage for the new optimization
  • Added test for optional single field with multiple milestones
  • Added test for required single field with existing issue
  • Added test for required single field with missing issue
  • Added test for required single field with multiple issues
tests/test_optimizer.py
Extended milestone schema with new field definitions
  • Added first_issue optional field
  • Added first_issue_required field
tests/projects/schema.py

Assessment against linked issues

Issue Objective Addressed Explanation
#662 Fix the issue where prefetched objects are bypassed when using QuerySet#first/get in one-to-many relations that return a single type

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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 @diesieben07 - 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: all looks good
  • 🟡 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 and I'll use the feedback to improve your reviews.

strawberry_django/fields/field.py Show resolved Hide resolved
@diesieben07
Copy link
Contributor Author

@bellini666 I'm not sure why the type check is failing. What is "Error: Fields without default values cannot appear after fields with default values (reportGeneralTypeIssues)" referring to? Any guidance would be appreciated if you can.

@codecov-commenter
Copy link

codecov-commenter commented Dec 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.08%. Comparing base (1b49393) to head (913c5f6).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #675      +/-   ##
==========================================
+ Coverage   88.99%   89.08%   +0.09%     
==========================================
  Files          41       41              
  Lines        3752     3767      +15     
==========================================
+ Hits         3339     3356      +17     
+ Misses        413      411       -2     

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

@diesieben07 diesieben07 force-pushed the GH-662/fix-prefetching-for-single-fields branch from 2dd6fa6 to 3f81116 Compare December 17, 2024 15:31
Copy link
Member

@bellini666 bellini666 left a comment

Choose a reason for hiding this comment

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

ty! :)

I've made a small adjustment to use qs._result_cache instead of len(qs). Although it should be ok, I think it's better to be safe than sorry 😅

@bellini666 bellini666 force-pushed the GH-662/fix-prefetching-for-single-fields branch from a98263b to 913c5f6 Compare December 18, 2024 18:08
@bellini666 bellini666 merged commit 5f27c02 into strawberry-graphql:main Dec 18, 2024
22 checks passed
@diesieben07
Copy link
Contributor Author

Thank you! :)

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 results are not used in case of one-to-many relations where just one object is used
3 participants