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 create multiple directives #3674

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Speedy1991
Copy link
Contributor

@Speedy1991 Speedy1991 commented Oct 17, 2024

Description

Directives are added multiple times leading to VSC errors like The directive "@featuresRequired" can only be used once at this location.

image

Another option to fix this would be to make field.directives a set but this is used on multiple places and I don't think it's worth the rework. If the fix is ok for you I'll add a test with e.g. this sandbox setup

Types of Changes

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

Issues Fixed or Closed by This PR

  • Check if the directive is already set on the field as directive

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 the issue of multiple directives being added to a field by deduplicating permission directives before extending the field's directives list.

Bug Fixes:

  • Fix the issue of directives being added multiple times to a field, which caused errors in VSC.

Enhancements:

  • Optimize the process of applying permission directives by deduplicating them before adding to the field.

Copy link
Contributor

sourcery-ai bot commented Oct 17, 2024

Reviewer's Guide by Sourcery

This pull request addresses a bug where directives were being added multiple times, causing VSCode errors. The fix involves deduplicating directives when applying permissions to a field, ensuring that each directive is only added once.

Class diagram for StrawberryField directive handling

classDiagram
    class StrawberryField {
        +List~Directive~ directives
    }
    class Permission {
        +Directive schema_directive
    }
    class PermissionHandler {
        +apply(field: StrawberryField) void
    }
    PermissionHandler --> StrawberryField : apply
    PermissionHandler --> Permission : uses
    note for StrawberryField "Field directives are now deduplicated when applying permissions."
Loading

File-Level Changes

Change Details Files
Implement deduplication of permission directives
  • Create a set of permission directives from the permissions
  • Create a set of existing field directives
  • Calculate the difference between permission directives and existing directives
  • Extend field directives with only the new, unique directives
strawberry/permission.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 @Speedy1991 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The fix looks good, but please include the test you mentioned (using the sandbox setup) in this PR to ensure the bug doesn't resurface in the future.
  • Consider if any user-facing documentation needs to be updated to reflect this fix, especially if users might have encountered this issue before.
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

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to Arthur for the PR 👏

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

1 similar comment
@botberry
Copy link
Member

Hi, thanks for contributing to Strawberry 🍓!

We noticed that this PR is missing a RELEASE.md file. We use that to automatically do releases here on GitHub and, most importantly, to PyPI!

So as soon as this PR is merged, a release will be made 🚀.

Here's an example of RELEASE.md:

Release type: patch

Description of the changes, ideally with some examples, if adding a new feature.

Release type can be one of patch, minor or major. We use semver, so make sure to pick the appropriate type. If in doubt feel free to ask :)

Here's the tweet text:

🆕 Release (next) is out! Thanks to Arthur for the PR 👏

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

Copy link

codecov bot commented Oct 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.02%. Comparing base (56172dc) to head (a736a61).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3674   +/-   ##
=======================================
  Coverage   97.01%   97.02%           
=======================================
  Files         503      503           
  Lines       33458    33461    +3     
  Branches     5618     5618           
=======================================
+ Hits        32460    32465    +5     
+ Misses        792      790    -2     
  Partials      206      206           

Copy link

codspeed-hq bot commented Oct 17, 2024

CodSpeed Performance Report

Merging #3674 will not alter performance

Comparing Speedy1991:fix-3596 (a736a61) with main (56172dc)

Summary

✅ 15 untouched benchmarks

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.

Hey @Speedy1991 ,

Thank you very much for this!

Could we have a test for this to avoid any future regressions?

@@ -158,9 +158,14 @@ def __init__(
def apply(self, field: StrawberryField) -> None:
"""Applies all of the permission directives to the schema and sets up silent permissions."""
if self.use_directives:
field.directives.extend(
# Dedupe multiple directives
# https://github.com/strawberry-graphql/strawberry/issues/3596
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need the issue linked here as it is easy to check the commit that changed this on git history

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, ok

@Speedy1991
Copy link
Contributor Author

Speedy1991 commented Oct 19, 2024

Hey @Speedy1991 ,

Thank you very much for this!

Could we have a test for this to avoid any future regressions?

yea, i wasn't sure if refactoring the directives from a list to a set would be a more stable fix, see this comment:

Another option to fix this would be to make field.directives a set but this is used on multiple places and I don't think it's worth the rework. If the fix is ok for you I'll add a test

@bellini666
Copy link
Member

yea, i wasn't sure if refactoring the directives from a list to a set would be a more stable fix, see this comment:

Refactoring it into a set could possibly have unwanted consequences. Because they are not "ordered" you can't enforce permissions to run in a given order. So I think your fix is the way to go.

@Speedy1991
Copy link
Contributor Author

Hm I think I need to refactor this a littlebit if the order matters, because the current fix cast the lists to sets and calculates the difference. As far i know casting to/from a set does not guarantee to keep the order.

@bellini666
Copy link
Member

Hm I think I need to refactor this a littlebit if the order matters, because the current fix cast the lists to sets and calculates the difference. As far i know casting to/from a set does not guarantee to keep the order.

Good point, I actually missed that (even though I commented about this same very issue lol)

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.

3 participants