mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-04 20:33:12 +03:00
Add schema test
This commit is contained in:
parent
b4b64fd6f4
commit
8c20e183ad
|
@ -81,6 +81,13 @@ def generate_enum_name(django_model_meta, field):
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
|
||||||
|
def convert_choice_field_to_enum(field, name=None):
|
||||||
|
if name is None:
|
||||||
|
name = generate_enum_name(field.model._meta, field)
|
||||||
|
choices = field.choices
|
||||||
|
return convert_choices_to_named_enum_with_descriptions(name, choices)
|
||||||
|
|
||||||
|
|
||||||
def convert_django_field_with_choices(
|
def convert_django_field_with_choices(
|
||||||
field, registry=None, convert_choices_to_enum=True
|
field, registry=None, convert_choices_to_enum=True
|
||||||
):
|
):
|
||||||
|
@ -90,8 +97,7 @@ def convert_django_field_with_choices(
|
||||||
return converted
|
return converted
|
||||||
choices = getattr(field, "choices", None)
|
choices = getattr(field, "choices", None)
|
||||||
if choices and convert_choices_to_enum:
|
if choices and convert_choices_to_enum:
|
||||||
name = generate_enum_name(field.model._meta, field)
|
enum = convert_choice_field_to_enum(field)
|
||||||
enum = convert_choices_to_named_enum_with_descriptions(name, choices)
|
|
||||||
required = not (field.blank or field.null)
|
required = not (field.blank or field.null)
|
||||||
converted = enum(description=field.help_text, required=required)
|
converted = enum(description=field.help_text, required=required)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -11,6 +11,7 @@ from graphene.relay import Node
|
||||||
from .. import registry
|
from .. import registry
|
||||||
from ..settings import graphene_settings
|
from ..settings import graphene_settings
|
||||||
from ..types import DjangoObjectType, DjangoObjectTypeOptions
|
from ..types import DjangoObjectType, DjangoObjectTypeOptions
|
||||||
|
from ..converter import convert_choice_field_to_enum
|
||||||
from .models import Article as ArticleModel
|
from .models import Article as ArticleModel
|
||||||
from .models import Reporter as ReporterModel
|
from .models import Reporter as ReporterModel
|
||||||
|
|
||||||
|
@ -529,3 +530,43 @@ class TestDjangoObjectType:
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
graphene_settings.CHOICES_TO_ENUM_UNIQUE_TYPE_NAME = False
|
graphene_settings.CHOICES_TO_ENUM_UNIQUE_TYPE_NAME = False
|
||||||
|
|
||||||
|
def test_django_objecttype_choices_override_enum(self, PetModel):
|
||||||
|
def convert_choice(model, field_name, **kwargs):
|
||||||
|
return convert_choice_field_to_enum(
|
||||||
|
model._meta.get_field(field_name), **kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
class PetModelKind(DjangoObjectType):
|
||||||
|
kind = Field(convert_choice(PetModel, "kind", name="CustomEnumName"))
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = PetModel
|
||||||
|
fields = ["id", "kind"]
|
||||||
|
|
||||||
|
class Query(ObjectType):
|
||||||
|
pet = Field(PetModelKind)
|
||||||
|
|
||||||
|
schema = Schema(query=Query)
|
||||||
|
|
||||||
|
assert str(schema) == dedent(
|
||||||
|
"""\
|
||||||
|
schema {
|
||||||
|
query: Query
|
||||||
|
}
|
||||||
|
|
||||||
|
enum DjangoModelTestsPetModelKindChoices {
|
||||||
|
CAT
|
||||||
|
DOG
|
||||||
|
}
|
||||||
|
|
||||||
|
type PetModelKind {
|
||||||
|
id: ID!
|
||||||
|
kind: DjangoModelTestsPetModelKindChoices!
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query {
|
||||||
|
pet: PetModelKind
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user