-
-
Notifications
You must be signed in to change notification settings - Fork 609
Support using enum values in the GraphQL schema #4071
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
base: main
Are you sure you want to change the base?
Conversation
Reviewer's GuideAdds support for using Python enum values instead of enum member names as GraphQL enum value names via a new File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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 there - I've reviewed your changes - here's some feedback:
- When
use_enum_values=True,graphql_nameis taken directly fromitem_value, which may not be a valid GraphQLName(or even a string); consider validating and/or coercing tostrand raising a clear error if the value is not a valid GraphQL enum name. - The new
use_enum_valuesflag subtly changes how input enum literals are written; it might be worth clarifying in the implementation (e.g., via a short comment near_process_enum) thatuse_enum_valuesaffects the GraphQL enum names, not the underlyingEnum.value, to make the semantics clear for future maintainers.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- When `use_enum_values=True`, `graphql_name` is taken directly from `item_value`, which may not be a valid GraphQL `Name` (or even a string); consider validating and/or coercing to `str` and raising a clear error if the value is not a valid GraphQL enum name.
- The new `use_enum_values` flag subtly changes how input enum literals are written; it might be worth clarifying in the implementation (e.g., via a short comment near `_process_enum`) that `use_enum_values` affects the *GraphQL enum names*, not the underlying `Enum.value`, to make the semantics clear for future maintainers.
## Individual Comments
### Comment 1
<location> `tests/enums/test_enum.py:208-217` </location>
<code_context>
+ assert res.data["foo"] == ["FOO", "BAR", "Baz"]
+
+
+def test_use_enum_values() -> None:
+ @strawberry.enum(use_enum_values=True)
+ class Foo(Enum):
+ FOO = "foo"
+ BAR = strawberry.enum_value("bar")
+ BAZ = strawberry.enum_value("baz", name="Baz")
@strawberry.type
class Query:
@strawberry.field
- def foo(self, foo: Foo) -> str:
- return foo.value
+ def foo(self, enums: List[Foo]) -> List[Foo]:
+ assert enums == [Foo.FOO, Foo.BAR, Foo.BAZ]
+ return enums
schema = strawberry.Schema(Query)
- res = schema.execute_sync("{ foo(foo: BAZ) }")
+ res = schema.execute_sync("{ foo(enums: [foo, bar, Baz]) }")
assert not res.errors
assert res.data
- assert res.data["foo"] == "baz"
+ assert res.data["foo"] == ["foo", "bar", "Baz"]
</code_context>
<issue_to_address>
**suggestion (testing):** Add a test for the functional `strawberry.enum(MyEnum, use_enum_values=True)` usage
Could you also add a companion test for the functional style, e.g.
```python
class Foo(Enum):
FOO = "foo"
BAR = strawberry.enum_value("bar")
BAZ = strawberry.enum_value("baz", name="Baz")
FooType = strawberry.enum(Foo, use_enum_values=True)
@strawberry.type
class Query:
@strawberry.field
def foo(self, enums: List[FooType]) -> List[FooType]:
return enums
```
and assert that both input literals and serialized output use the enum values (with `name=` overrides still taking precedence)? That would confirm decorator and functional forms handle `use_enum_values` consistently.
</issue_to_address>
### Comment 2
<location> `docs/types/enums.md:137-139` </location>
<code_context>
GraphQL types are not a map of name: value, like in python enums. Strawberry
-uses the name of the members of the enum to create the GraphQL type.
+default to using the name of the members of the enum to create the GraphQL type.
+You can use their values instead with `@strawberry.enum(use_enum_values=True)`
</code_context>
<issue_to_address>
**issue (typo):** Use correct verb form: "defaults" instead of "default" after "Strawberry".
The sentence currently reads as “Strawberry default to using…”. For correct subject–verb agreement, update it to: “Strawberry defaults to using the name of the members of the enum to create the GraphQL type.”
```suggestion
GraphQL types are not a map of name: value, like in python enums. Strawberry
defaults to using the name of the members of the enum to create the GraphQL type.
You can use their values instead with `@strawberry.enum(use_enum_values=True)`
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Hi, thanks for contributing to Strawberry 🍓! We noticed that this PR is missing a So as soon as this PR is merged, a release will be made 🚀. Here's an example of 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: |
Greptile OverviewGreptile SummaryThis PR adds Key Changes:
Implementation Quality:
Confidence Score: 5/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant User
participant Decorator as @strawberry.enum
participant ProcessEnum as _process_enum()
participant EnumValue as EnumValue
participant Schema as Schema Converter
participant GraphQL as GraphQL Schema
User->>Decorator: Define enum with use_enum_values=True
Decorator->>ProcessEnum: Pass use_enum_values flag
loop For each enum member
ProcessEnum->>ProcessEnum: Check if value is EnumValueDefinition
alt Has EnumValueDefinition with custom name
ProcessEnum->>EnumValue: Use custom graphql_name from enum_value()
else use_enum_values=True
ProcessEnum->>EnumValue: Use enum.value as graphql_name
else Default behavior
ProcessEnum->>EnumValue: Use enum.name as graphql_name
end
ProcessEnum->>ProcessEnum: Create EnumValue with graphql_name
end
ProcessEnum->>Schema: Return StrawberryEnumDefinition
Schema->>GraphQL: Convert to GraphQLEnumType
GraphQL->>User: Enum values exposed in schema
|
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.
3 files reviewed, no comments
67babf4 to
44a6fe8
Compare
for more information, see https://pre-commit.ci
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4071 +/- ##
=======================================
Coverage 94.22% 94.22%
=======================================
Files 540 540
Lines 35426 35461 +35
Branches 1870 1872 +2
=======================================
+ Hits 33379 33412 +33
- Misses 1737 1738 +1
- Partials 310 311 +1 🚀 New features to boost your workflow:
|
CodSpeed Performance ReportMerging #4071 will not alter performanceComparing Summary
|
for more information, see https://pre-commit.ci
4a624ff to
1d25d52
Compare
for more information, see https://pre-commit.ci

Currently, Strawberry exposes the enum name in GraphQL.
This PR adds the option to use the enum values instead.
Why
I'm wrapping an existing OpenAPI service in GraphQL. There are tons of enums generated by the OpenAPI client that I want to reuse instead of writing again (also, they change often). They generated enums look like this:
Since the lowercase values are used in a ton of different places, including documentation and case-sensitive queries (e.g.,
findEntities('myenum eq "foo"'), I want them to stay the same everywhere.This PR adds
@strawberry.enum(use_enum_values=True), so that the GraphQL mapping uses the enum values instead of namesTypes of Changes
Checklist
Summary by Sourcery
Add support for mapping GraphQL enum values from either the Python enum names or their underlying values, controlled via a new use_enum_values option.
New Features:
Enhancements:
Tests: