-
Notifications
You must be signed in to change notification settings - Fork 768
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix typed choices, make working with different Django 5x choices options
- Loading branch information
Showing
7 changed files
with
173 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,69 @@ 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 { | ||
callableChoice | ||
} | ||
} | ||
""" | ||
) | ||
assert not result.errors | ||
assert result.data == { | ||
"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 ste or int values | ||
return Reporter( | ||
typed_choice=TypedIntChoice.CHOICE_THIS, | ||
class_choice=TypedIntChoice.CHOICE_THAT, | ||
callable_choice=TypedStrChoice.CHOICE_THIS, | ||
) | ||
|
||
schema = graphene.Schema(query=Query) | ||
|
||
result = schema.execute( | ||
""" | ||
query { | ||
reporter { | ||
firstName | ||
aChoice | ||
typedChoice | ||
classChoice | ||
callableChoice | ||
} | ||
} | ||
""" | ||
) | ||
assert not result.errors | ||
assert result.data == { | ||
"reporter": {"firstName": "Bridget", "aChoice": None}, | ||
"reporter": { | ||
"typedChoice": "A_1", | ||
"classChoice": "A_2", | ||
"callableChoice": "THIS", | ||
}, | ||
} |
This file contains 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 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