Skip to content

Conversation

@star2000
Copy link
Member

@star2000 star2000 commented Jun 1, 2025

Description

This is to prevent front-end developers from accidentally modifying or deleting the entire table.

Types of Changes

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

Issues Fixed or Closed by This PR

close #751

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

Enforce requiring filters in update and delete mutations by default to prevent unintended bulk operations, and provide a new configuration option to override this behavior.

Enhancements:

  • Require filters argument for all mutations by default to avoid accidental full-table modifications.
  • Introduce ALLOW_MUTATIONS_WITHOUT_FILTERS setting to permit omitting filters when explicitly enabled.
  • Add ALLOW_MUTATIONS_WITHOUT_FILTERS default configuration (false) to project settings.

Tests:

  • Update mutation tests to include empty filters in update and delete operations.
  • Add tests for the ALLOW_MUTATIONS_WITHOUT_FILTERS setting in the settings suite.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 1, 2025

Reviewer's Guide

Introduce a new ALLOW_MUTATIONS_WITHOUT_FILTERS setting (default false) to enforce required filters on Django mutations, update the filter argument logic to respect this flag, and adjust the test suite to include explicit filters in mutation calls and validate the new setting.

Sequence Diagram: Filter Argument Optionality in Mutations

sequenceDiagram
    title "Sequence Diagram: Filter Argument Optionality in Mutations"
    participant ClientCode as Client Code (e.g., Strawberry framework)
    participant FieldInstance as FieldInstance (self, in filters.py)
    participant DjangoMutationBaseMod as DjangoMutationBase (type check)
    participant Settings as strawberry_django_settings

    ClientCode->>FieldInstance: call arguments()
    FieldInstance->>FieldInstance: if (filters is not None and self.is_list)
    activate FieldInstance
        FieldInstance->>DjangoMutationBaseMod: isinstance(self, DjangoMutationBase)?
        alt self is instance of DjangoMutationBase
            FieldInstance->>Settings: strawberry_django_settings()
            Settings-->>FieldInstance: settings_data (contains ALLOW_MUTATIONS_WITHOUT_FILTERS)
            FieldInstance->>FieldInstance: is_optional_for_filters_arg = settings_data['ALLOW_MUTATIONS_WITHOUT_FILTERS']
        else self is not instance of DjangoMutationBase
            FieldInstance->>FieldInstance: is_optional_for_filters_arg = True
        end
        FieldInstance->>FieldInstance: arguments.append(FILTERS_ARG, filters, is_optional=is_optional_for_filters_arg)
    deactivate FieldInstance
    FieldInstance-->>ClientCode: list of arguments (with FILTERS_ARG optionality based on logic)
Loading

ER Diagram: Update to StrawberryDjangoSettings

erDiagram
    title "ER Diagram: Update to StrawberryDjangoSettings"
    StrawberryDjangoSettings {
        bool ALLOW_MUTATIONS_WITHOUT_FILTERS "New setting to control filter requirement in mutations (default: false)"
        string other_existing_settings "Represents other pre-existing settings"
    }
Loading

File-Level Changes

Change Details Files
Add ALLOW_MUTATIONS_WITHOUT_FILTERS setting to control mutation filter requirements
  • Define setting in StrawberryDjangoSettings TypedDict
  • Set default value to False
  • Extend test_settings to cover the new setting
strawberry_django/settings.py
tests/test_settings.py
Update filter argument logic to honor the new setting for Django mutations
  • Import DjangoMutationBase and determine is_optional based on setting
  • Wrap filters argument addition to respect ALLOW_MUTATIONS_WITHOUT_FILTERS
strawberry_django/filters.py
Ensure all mutation tests supply explicit filters
  • Add filters: {} to update and delete mutation queries
  • Update tests in both mutation and relationship suites
tests/mutations/test_mutations.py
tests/mutations/test_relationship.py

Assessment against linked issues

Issue Objective Addressed Explanation
#751 Make filters required by default for updates and deletes in mutations.
#751 Provide a setting to allow mutations without filters for cases where this is desired.

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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

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 @star2000 - 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
  • 🟢 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.

@codecov-commenter
Copy link

codecov-commenter commented Jun 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.02%. Comparing base (a3da538) to head (9d1f921).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #755      +/-   ##
==========================================
+ Coverage   89.00%   89.02%   +0.01%     
==========================================
  Files          45       45              
  Lines        4303     4309       +6     
==========================================
+ Hits         3830     3836       +6     
  Misses        473      473              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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! :)

@bellini666 bellini666 merged commit 497a540 into strawberry-graphql:main Jun 8, 2025
38 checks passed
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.

For safety reasons, filters used for updates and deletes in mutations should be set to required by default.

3 participants