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

Bugfix Extensions Added Twice When Custom Directives Are Present #3776 #3783

Conversation

doney-dkp
Copy link
Contributor

@doney-dkp doney-dkp commented Feb 13, 2025

When custom directives are added, extensions are appended twice in the get_extensions function in the schema.py file.

Description

This fixes an issue where extensions were being duplicated when custom directives were added to the schema. Previously, when user directives were present, extensions were being appended twice to the extension list, causing them to be executed multiple times during query processing.

The fix ensures that extensions are added only once and maintains their original order. Test cases have been added to validate this behavior and ensure extensions are executed exactly once.

Types of Changes

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

Issues Fixed or Closed by This PR

Previous Behavior:

When creating a schema with both extensions and directives:

@strawberry.directive(locations=[DirectiveLocation.FIELD])
def uppercase(value: str) -> str:
    return value.upper()

class LoggingExtension(SchemaExtension):
    async def on_execute(self):
        print("Extension executed")
        yield

schema = strawberry.Schema(
    query=Query,
    extensions=[LoggingExtension],
    directives=[uppercase]
)

The LoggingExtension would be executed twice, resulting in:

Extension executed
Extension executed

After this fix, the same code will execute the extension only once:
Extension executed

The fix ensures that extensions are added only once to the extension list while maintaining their original order. Test cases have been added to validate this behavior and ensure extensions are executed exactly once.
Impact:

Improves performance by preventing duplicate extension execution
Maintains expected extension behavior
Preserves extension order in the schema

To validate this fix, new test cases have been added that verify extension uniqueness and execution count.

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

Bug Fixes:

  • Fixed an issue where extensions were added twice to the schema when custom directives were used, leading to duplicate execution and potential performance issues.

Copy link
Contributor

sourcery-ai bot commented Feb 13, 2025

Reviewer's Guide by Sourcery

This pull request fixes an issue in the get_extensions function where extensions were being appended twice when custom directives are present. The fix modifies the extension concatenation logic to ensure that extensions are added only once while maintaining their order, and introduces new tests to validate the correct behavior.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Corrected the logic in get_extensions to prevent duplicate extension addition when directives are present.
  • Removed the previous approach of merging extensions and directives which led to duplication.
  • Updated the get_extensions method to conditionally extend the extensions list with the appropriate directive extension based on the sync flag.
strawberry/schema/schema.py
Added new tests to validate the bugfix and ensure that extension uniqueness and order preservation are maintained.
  • Introduced a test to verify that an extension appears only once even when custom directives are present.
  • Added a test to confirm that the order of user-provided extensions is preserved after removing duplicates.
tests/schema/test_get_extensions.py
Updated the release documentation to reflect the patch and describe the bugfix.
  • Added release notes in RELEASE.md that explain the bugfix and its impact on extension execution behavior.
RELEASE.md

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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 @doney-dkp - I've reviewed your changes - here's some feedback:

Overall Comments:

  • It's great that you've added tests to cover this bugfix.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 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.

@botberry
Copy link
Member

botberry commented Feb 13, 2025

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release fixes an issue where extensions were being duplicated when custom directives were added to the schema. Previously, when user directives were present, extensions were being appended twice to the extension list, causing them to be executed multiple times during query processing.

The fix ensures that extensions are added only once and maintains their original order. Test cases have been added to validate this behavior and ensure extensions are executed exactly once.

Here's the tweet text:

🆕 Release (next) is out! Thanks to DONEY K PAUL for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

@botberry
Copy link
Member

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


This release fixes an issue where extensions were being duplicated when custom directives were added to the schema. Previously, when user directives were present, extensions were being appended twice to the extension list, causing them to be executed multiple times during query processing.

The fix ensures that extensions are added only once and maintains their original order. Test cases have been added to validate this behavior and ensure extensions are executed exactly once.

Here's the tweet text:

🆕 Release (next) is out! Thanks to DONEY K PAUL for the PR 👏

Get it here 👉 https://strawberry.rocks/release/(next)

Copy link
Member

@patrick91 patrick91 left a comment

Choose a reason for hiding this comment

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

🔥

Thank you so much! Love the tests 😊

Copy link

codecov bot commented Feb 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.24%. Comparing base (4c4e07b) to head (751b1e3).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3783   +/-   ##
=======================================
  Coverage   97.24%   97.24%           
=======================================
  Files         502      503    +1     
  Lines       33481    33509   +28     
  Branches     1714     1714           
=======================================
+ Hits        32558    32586   +28     
  Misses        706      706           
  Partials      217      217           

Copy link

codspeed-hq bot commented Feb 13, 2025

CodSpeed Performance Report

Merging #3783 will not alter performance

Comparing doney-dkp:bugfix-extenstions-added-twice-with-user-directive (751b1e3) with main (03d8aca)

Summary

✅ 21 untouched benchmarks

@patrick91 patrick91 merged commit 2470164 into strawberry-graphql:main Feb 13, 2025
93 of 94 checks passed
@botberry
Copy link
Member

Thanks for contributing to Strawberry! 🎉 You've been invited to join
the Strawberry GraphQL organisation 😊

You can also request a free sticker by filling this form: https://forms.gle/dmnfQUPoY5gZbVT67

And don't forget to join our discord server: https://strawberry.rocks/discord 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants