-
-
Notifications
You must be signed in to change notification settings - Fork 549
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
Add support for using raw Python enum types in schema #3639
Conversation
Reviewer's Guide by SourceryThis pull request adds support for using raw Python enum types in Strawberry schemas without requiring the File-Level Changes
Tips
|
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 @nrbnlulu - 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: 2 issues found
- 🟢 Complexity: all looks good
- 🟡 Documentation: 3 issues found
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.
tests/enums/test_enum.py
Outdated
def test_default_int_enum_implementation() -> None: | ||
class Foo(IntEnum): | ||
BAR = 1 | ||
BAZ = 2 | ||
|
||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def foo(self, foo: Foo) -> int: | ||
return foo.value | ||
|
||
schema = strawberry.Schema(Query) | ||
res = schema.execute_sync("{ foo(foo: BAR) }") | ||
assert not res.errors | ||
assert res.data | ||
assert res.data["foo"] == 1 |
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): New test for default IntEnum implementation
Good test. Consider adding a case to verify that the GraphQL schema represents the enum values as strings, even though they're integers in Python.
def test_default_int_enum_implementation() -> None:
class Foo(IntEnum):
BAR = 1
BAZ = 2
@strawberry.type
class Query:
@strawberry.field
def foo(self, foo: Foo) -> int:
return foo.value
schema = strawberry.Schema(Query)
res = schema.execute_sync("{ foo(foo: BAR) }")
assert not res.errors
assert res.data and res.data["foo"] == 1
schema_str = str(schema)
assert '"BAR"' in schema_str and '"BAZ"' in schema_str
```py | ||
# somewhere.py | ||
from enum import Enum | ||
|
||
|
||
class AnimalKind(Enum): | ||
AXOLOTL, CAPYBARA = range(2) | ||
|
||
|
||
# gql/animals | ||
from somewhere import AnimalKind | ||
|
||
|
||
@strawberry.type | ||
class AnimalType: | ||
kind: AnimalKind | ||
``` |
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 (documentation): Improve the code example for clarity and completeness
Consider adding the missing import strawberry
statement, and complete the AnimalKind
enum definition. Also, you might want to add a comment to indicate that this is two separate files.
# somewhere.py
from enum import Enum
class AnimalKind(Enum):
AXOLOTL = 0
CAPYBARA = 1
# gql/animals.py
import strawberry
from somewhere import AnimalKind
@strawberry.type
class AnimalType:
kind: AnimalKind
This release adds support for using rwa Python enum types in your schema | ||
(enums that are not decorated with `@strawberry.enum`) | ||
|
||
This is useful if you have enum types from other places in your code |
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 (documentation): Consider expanding on the benefits of this feature
It would be helpful to briefly explain why using raw Python enums in Strawberry schemas is beneficial. This could help users better understand the value of this update.
This is useful if you have enum types from other places in your code
that you want to use in Strawberry without modification. This feature
enhances code reusability, reduces duplication, and simplifies
integration with existing Python libraries or codebases that use enums.
Thanks for adding the Here's a preview of the changelog: This release adds support for using raw Python enum types in your schema This is useful if you have enum types from other places in your code # somewhere.py
from enum import Enum
class AnimalKind(Enum):
AXOLOTL, CAPYBARA = range(2)
# gql/animals
from somewhere import AnimalKind
@strawberry.type
class AnimalType:
kind: AnimalKind Here's the tweet text:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3639 +/- ##
==========================================
+ Coverage 96.66% 96.94% +0.27%
==========================================
Files 504 503 -1
Lines 33448 33457 +9
Branches 5600 5602 +2
==========================================
+ Hits 32333 32434 +101
+ Misses 881 791 -90
+ Partials 234 232 -2 |
CodSpeed Performance ReportMerging #3639 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.
Loving this! Thanks for the PR
…usage in StrawberryAnnotation
for more information, see https://pre-commit.ci
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.
LGTM, we should add some docs on this though 😊
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.
looks good! I left a couple of comments around the old exception 😊
docs/errors/object-is-not-an-enum.md
Outdated
@@ -13,7 +13,7 @@ example the following code will throw this error: | |||
import strawberry | |||
|
|||
|
|||
# note the lack of @strawberry.enum here: | |||
@strawberry.enum here: |
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.
@nrbnlulu can you fix this? :D
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.
I guess we can delete this file now?
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.
probably
@@ -120,25 +120,6 @@ class IceCreamFlavour(Enum): | |||
assert definition.values[2].description is None | |||
|
|||
|
|||
@pytest.mark.raises_strawberry_exception( | |||
NotAStrawberryEnumError, match='Enum "IceCreamFlavour" is not a Strawberry 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.
we should delete the exception too :)
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.
I've removed the NotAStrawberryEnumError exception :D
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.
marking as request changes so we dont accidentally merge the test due to previous approval
@erikwrede you mean the failing test, right? |
@patrick91 i just meant the wrong file @nrbnlulu mentioned but looks good now 😊 |
Description
Types of Changes
Issues Fixed or Closed by This PR
fix #3543
Checklist
Summary by Sourcery
Add support for using raw Python enum types in schemas, enabling the use of enums without
@strawberry.enum
decoration. Fix issue #3543 and update tests to cover the new functionality. Document the changes in RELEASE.md.New Features:
@strawberry.enum
to be used.Bug Fixes:
Documentation:
Tests: