-
-
Notifications
You must be signed in to change notification settings - Fork 565
Add support for passing name
to enum_value
#3841
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
Conversation
Reviewer's Guide by SourceryThis pull request introduces the ability to specify a custom GraphQL name for enum values using the Sequence diagram for enum value processing with custom namesequenceDiagram
participant User
participant strawberry.enum
participant enum_value()
participant EnumValueDefinition
participant _process_enum()
participant EnumValue
User->>strawberry.enum: Defines enum with enum_value(name='customName')
strawberry.enum->>enum_value(): Calls enum_value with value and name
enum_value()->>EnumValueDefinition: Creates EnumValueDefinition(value, graphql_name='customName')
strawberry.enum->>_process_enum(): Processes enum
_process_enum()->>EnumValueDefinition: Retrieves value and graphql_name
alt graphql_name is not None
_process_enum()->>EnumValue: Creates EnumValue with graphql_name
else graphql_name is None
_process_enum()->>EnumValue: Creates EnumValue with item_name
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Apollo Federation Subgraph Compatibility Results
Learn more: |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3841 +/- ##
=======================================
Coverage 95.04% 95.04%
=======================================
Files 502 502
Lines 32682 32719 +37
Branches 1695 1696 +1
=======================================
+ Hits 31061 31098 +37
Misses 1348 1348
Partials 273 273 🚀 New features to boost your workflow:
|
8e713a1
to
fe3866a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
This PR adds support for custom GraphQL names for enum values in Strawberry, allowing developers to specify different names in the GraphQL schema while maintaining Python-style enum names in code.
- Added
name
parameter toenum_value()
instrawberry/types/enum.py
to support custom GraphQL names - Updated
_process_enum
function to handle custom names during schema generation - Added comprehensive documentation in
docs/types/enums.md
with examples showingCHOCOLATE_COOKIE
mapping tochocolateCookie
- Added test cases in
tests/schema/test_enum.py
verifying custom naming works for both input and output scenarios - Maintained federation support in
strawberry/federation/enum.py
by preserving directive handling with new naming feature
💡 (5/5) You can turn off certain types of comments like style here!
5 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile
enum IceCreamFlavour { | ||
VANILLA | ||
chocolateCookie | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Indentation in GraphQL schema example is inconsistent with standard GraphQL formatting (should be 2 spaces)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @patrick91 - I've reviewed your changes - here's some feedback:
Overall Comments:
- It would be good to add a release note to explain the new feature and how to use it.
- Consider adding a test case that uses directives on the enum value.
Here's what I looked at during the review
- 🟡 General issues: 1 issue found
- 🟢 Security: all looks good
- 🟡 Testing: 2 issues found
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
@strawberry.enum | ||
class IceCreamFlavour(Enum): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Missing test cases for edge cases.
It would be beneficial to add tests for edge cases such as providing an invalid name
argument (e.g., containing spaces or special characters) to enum_value
to ensure proper error handling and prevent unexpected behavior.
Suggested implementation:
def test_invalid_enum_value_name_with_space_raises_exception():
import pytest
with pytest.raises(ValueError, match="Invalid custom enum value name"):
@strawberry.enum
class BadEnum(Enum):
A = "a"
B = strawberry.enum_value("b", name="invalid name")
def test_invalid_enum_value_name_with_special_characters_raises_exception():
import pytest
with pytest.raises(ValueError, match="Invalid custom enum value name"):
@strawberry.enum
class AnotherBadEnum(Enum):
X = "x"
Y = strawberry.enum_value("y", name="invalid@name")
Ensure that the implementation of strawberry.enum_value properly validates the custom name and raises a ValueError with a matching message ("Invalid custom enum value name") when the name is invalid. If the current implementation does not do that, you might need to update it accordingly.
VANILLA = "vanilla" | ||
CHOCOLATE_COOKIE = strawberry.enum_value("chocolate", name="chocolateCookie") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Test case for duplicate names is missing.
A test case should be added to verify the behavior when a custom name is provided that duplicates an existing enum value name (e.g., defining another enum value with the name "VANILLA"). This will help ensure that the library handles such scenarios gracefully and provides informative error messages.
Suggested implementation:
import pytest
def test_duplicate_enum_custom_names():
with pytest.raises(ValueError, match="duplicate.*VANILLA"):
@strawberry.enum
class DuplicateIceCreamFlavour(Enum):
VANILLA = "vanilla"
# Define another value with a custom name that duplicates "VANILLA"
ANOTHER_VANILLA = strawberry.enum_value("another", name="VANILLA")
@strawberry.type
class Query:
flavour: DuplicateIceCreamFlavour = strawberry.field()
# Attempting to build the schema should raise an error due to the duplicate names.
strawberry.Schema(Query)
• Ensure that pytest is available in your testing environment.
• Verify that the error message thrown by strawberry for duplicate enum value names matches the regex "duplicate.*VANILLA" (adjust the match string if necessary).
Thanks for adding the Here's a preview of the changelog: This release adds support for custom names in enum values using the This allows you to specify a different name for an enum value in the GraphQL schema while keeping the original Python enum member name. For example: @strawberry.enum
class IceCreamFlavour(Enum):
VANILLA = "vanilla"
CHOCOLATE_COOKIE = strawberry.enum_value("chocolate", name="chocolateCookie") This will produce a GraphQL schema with the custom name: enum IceCreamFlavour {
VANILLA
chocolateCookie
} Here's the tweet text:
|
CodSpeed Performance ReportMerging #3841 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool one!! ❤️
This is a pretty cool feature! Thanks folks! |
Summary by Sourcery
Add support for custom names in enum values using the
name
parameter instrawberry.enum_value
New Features:
Documentation:
Tests: