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(mutations): Refetch instances to optimize the return value #674

Merged
merged 1 commit into from
Dec 15, 2024

Conversation

bellini666
Copy link
Member

@bellini666 bellini666 commented Dec 15, 2024

Refetch instances after the mutation to make sure we optimize the result.

Without this, annotated fields would not exist in the return value and would fail to be returned.

I'm also modifying a test to include an annotated field. Without this change applied it would fail.

Fix #635

Summary by Sourcery

Refetch instances after mutations to ensure annotated fields are included in the return value, addressing issue #635. Introduce a refetch method to optimize the retrieval of instances post-mutation. Add a test case to verify the inclusion of the 'isDelayed' field in the mutation response.

Bug Fixes:

Enhancements:

  • Introduce a refetch method to optimize the retrieval of instances post-mutation.

Tests:

  • Add a test case to verify the inclusion of the 'isDelayed' field in the mutation response.

@bellini666 bellini666 self-assigned this Dec 15, 2024
Copy link
Contributor

sourcery-ai bot commented Dec 15, 2024

Reviewer's Guide by Sourcery

This PR optimizes mutation return values by refetching instances after mutations to ensure all fields (including annotated ones) are properly included in the response. The implementation adds a new refetch method to handle optimization of both single and list model instances, and modifies create/update/delete mutations to use this optimization approach.

Sequence diagram for refetching instances after mutation

sequenceDiagram
    participant User
    participant Mutation as DjangoMutation
    participant DB as Database

    User->>Mutation: Request mutation (create/update/delete)
    Mutation->>DB: Perform mutation
    DB-->>Mutation: Return mutated instance(s)
    Mutation->>Mutation: Call refetch(resolved, info)
    Mutation->>DB: Refetch optimized instance(s)
    DB-->>Mutation: Return optimized instance(s)
    Mutation-->>User: Return optimized result
Loading

Class diagram for updated mutation classes

classDiagram
    class DjangoMutationBase {
        +refetch(resolved: _T, info: Info | None) _T
    }

    class DjangoCreateMutation {
        +resolver(...)
        +create(data: dict[str, Any], info: Info)
    }

    class DjangoUpdateMutation {
        +resolver(...)
        +instance_level_update(...)
        +update(instance: models.Model | Iterable[models.Model], data: dict[str, Any])
    }

    class DjangoDeleteMutation {
        +resolver(...)
        +delete(instance: models.Model | Iterable[models.Model], data: dict[str, Any] | None)
    }

    DjangoCreateMutation --|> DjangoMutationBase
    DjangoUpdateMutation --|> DjangoMutationBase
    DjangoDeleteMutation --|> DjangoMutationBase

    note for DjangoMutationBase "New refetch method added for optimization"
Loading

File-Level Changes

Change Details Files
Added a new refetch method to optimize mutation return values
  • Implemented a generic refetch method that handles both single models and lists of models
  • Added type variable _T bound to Model or list[Model]
  • Preserved original ordering when refetching lists of models
  • Added optimization using DjangoOptimizerExtension and model queries
strawberry_django/mutations/fields.py
Restructured mutation operations to use the new refetch optimization
  • Modified create mutation to refetch instances after creation
  • Updated update mutation to refetch instances after updates
  • Moved DjangoOptimizerExtension.disabled() context to resolver level
  • Simplified delete mutation by removing unnecessary optimization disable context
strawberry_django/mutations/fields.py
Updated tests to verify optimization behavior
  • Added query count assertion to verify optimization performance
  • Added isDelayed field to test query to verify annotated field handling
tests/test_input_mutations.py

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 @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: all looks good
  • 🟢 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.

tests/test_input_mutations.py Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Dec 15, 2024

Codecov Report

Attention: Patch coverage is 96.77419% with 1 line in your changes missing coverage. Please review.

Project coverage is 88.99%. Comparing base (0d59b84) to head (75d41c7).

Files with missing lines Patch % Lines
strawberry_django/mutations/fields.py 96.77% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #674      +/-   ##
==========================================
+ Coverage   88.94%   88.99%   +0.04%     
==========================================
  Files          41       41              
  Lines        3737     3752      +15     
==========================================
+ Hits         3324     3339      +15     
  Misses        413      413              

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

@bellini666 bellini666 merged commit ea97376 into main Dec 15, 2024
24 checks passed
@bellini666 bellini666 deleted the refetch_mutations branch December 15, 2024 11:24
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.

annotate and other query optimizations fail in mutations
2 participants