-
Notifications
You must be signed in to change notification settings - Fork 766
fix typed choices, make working with different Django 5x choices options #1539
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
Merged
firaskafri
merged 3 commits into
graphql-python:main
from
ivelum:fix-typed-choice-fields
Mar 13, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
) | ||
from ..registry import Registry | ||
from ..types import DjangoObjectType | ||
from .models import Article, Film, FilmDetails, Reporter | ||
from .models import Article, Film, FilmDetails, Reporter, TypedIntChoice, TypedStrChoice | ||
|
||
# from graphene.core.types.custom_scalars import DateTime, Time, JSONString | ||
|
||
|
@@ -443,35 +443,102 @@ def test_choice_enum_blank_value(): | |
class ReporterType(DjangoObjectType): | ||
class Meta: | ||
model = Reporter | ||
fields = ( | ||
"first_name", | ||
"a_choice", | ||
) | ||
fields = ("callable_choice",) | ||
|
||
class Query(graphene.ObjectType): | ||
reporter = graphene.Field(ReporterType) | ||
|
||
def resolve_reporter(root, info): | ||
return Reporter.objects.first() | ||
# return a model instance with blank choice field value | ||
return Reporter(callable_choice="") | ||
|
||
schema = graphene.Schema(query=Query) | ||
|
||
# Create model with empty choice option | ||
Reporter.objects.create( | ||
first_name="Bridget", last_name="Jones", email="[email protected]" | ||
) | ||
|
||
result = schema.execute( | ||
""" | ||
query { | ||
reporter { | ||
firstName | ||
aChoice | ||
callableChoice | ||
} | ||
} | ||
""" | ||
) | ||
assert not result.errors | ||
assert result.data == { | ||
"reporter": {"firstName": "Bridget", "aChoice": None}, | ||
"reporter": {"callableChoice": None}, | ||
} | ||
|
||
|
||
def test_typed_choice_value(): | ||
"""Test that typed choices fields are resolved correctly to the enum values""" | ||
|
||
class ReporterType(DjangoObjectType): | ||
class Meta: | ||
model = Reporter | ||
fields = ("typed_choice", "class_choice", "callable_choice") | ||
|
||
class Query(graphene.ObjectType): | ||
reporter = graphene.Field(ReporterType) | ||
|
||
def resolve_reporter(root, info): | ||
# assign choice values to the fields instead of their str or int values | ||
return Reporter( | ||
typed_choice=TypedIntChoice.CHOICE_THIS, | ||
class_choice=TypedIntChoice.CHOICE_THAT, | ||
callable_choice=TypedStrChoice.CHOICE_THIS, | ||
) | ||
|
||
class CreateReporter(graphene.Mutation): | ||
reporter = graphene.Field(ReporterType) | ||
|
||
def mutate(root, info, **kwargs): | ||
return CreateReporter( | ||
reporter=Reporter( | ||
typed_choice=TypedIntChoice.CHOICE_THIS, | ||
class_choice=TypedIntChoice.CHOICE_THAT, | ||
callable_choice=TypedStrChoice.CHOICE_THIS, | ||
), | ||
) | ||
|
||
class Mutation(graphene.ObjectType): | ||
create_reporter = CreateReporter.Field() | ||
|
||
schema = graphene.Schema(query=Query, mutation=Mutation) | ||
|
||
reporter_fragment = """ | ||
fragment reporter on ReporterType { | ||
typedChoice | ||
classChoice | ||
callableChoice | ||
} | ||
""" | ||
|
||
expected_reporter = { | ||
"typedChoice": "A_1", | ||
"classChoice": "A_2", | ||
"callableChoice": "THIS", | ||
} | ||
|
||
result = schema.execute( | ||
reporter_fragment | ||
+ """ | ||
query { | ||
reporter { ...reporter } | ||
} | ||
""" | ||
) | ||
assert not result.errors | ||
assert result.data["reporter"] == expected_reporter | ||
|
||
result = schema.execute( | ||
reporter_fragment | ||
+ """ | ||
mutation { | ||
createReporter { | ||
reporter { ...reporter } | ||
} | ||
} | ||
""" | ||
) | ||
assert not result.errors | ||
assert result.data["createReporter"]["reporter"] == expected_reporter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.