mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-07-13 17:52:19 +03:00
Type errors for serializer mutation
This commit is contained in:
parent
f76f38ef30
commit
98a9c7161d
|
@ -40,10 +40,6 @@ class SerializerMutation(ClientIDMutation):
|
|||
class Meta:
|
||||
abstract = True
|
||||
|
||||
errors = graphene.List(
|
||||
ErrorType, description="May contain more than one error for same field."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def __init_subclass_with_meta__(
|
||||
cls,
|
||||
|
@ -55,7 +51,6 @@ class SerializerMutation(ClientIDMutation):
|
|||
exclude_fields=(),
|
||||
**options
|
||||
):
|
||||
|
||||
if not serializer_class:
|
||||
raise Exception("serializer_class is required for the SerializerMutation")
|
||||
|
||||
|
@ -78,6 +73,26 @@ class SerializerMutation(ClientIDMutation):
|
|||
serializer, only_fields, exclude_fields, is_input=False
|
||||
)
|
||||
|
||||
error_fields = {
|
||||
key: graphene.List(graphene.NonNull(graphene.String))
|
||||
for key in input_fields.keys()
|
||||
}
|
||||
|
||||
# TODO: from settings
|
||||
error_fields['non_field_errors'] = graphene.List(
|
||||
graphene.NonNull(graphene.String)
|
||||
)
|
||||
|
||||
base_name = cls.__name__
|
||||
|
||||
cls.Errors = type(
|
||||
"{}Errors".format(base_name),
|
||||
(graphene.ObjectType, ),
|
||||
yank_fields_from_attrs(error_fields, _as=Field),
|
||||
)
|
||||
|
||||
output_fields['errors'] = graphene.Field(cls.Errors, required=True)
|
||||
|
||||
_meta = SerializerMutationOptions(cls)
|
||||
_meta.lookup_field = lookup_field
|
||||
_meta.model_operations = model_operations
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import datetime
|
||||
|
||||
from graphene import Field, ResolveInfo
|
||||
from graphene import Field, ResolveInfo, NonNull, List, String
|
||||
from graphene.types.inputobjecttype import InputObjectType
|
||||
from py.test import raises
|
||||
from py.test import mark
|
||||
|
@ -177,3 +177,21 @@ def test_invalid_serializer_operations():
|
|||
model_operations = ["Add"]
|
||||
|
||||
assert "model_operations" in str(exc.value)
|
||||
|
||||
|
||||
def test_errors_field():
|
||||
class MyMutation(SerializerMutation):
|
||||
class Meta:
|
||||
serializer_class = MySerializer
|
||||
|
||||
errors_field = MyMutation._meta.fields['errors']
|
||||
|
||||
assert MyMutation.Errors
|
||||
|
||||
assert type(errors_field.type) == NonNull
|
||||
|
||||
errors_field = errors_field.type.of_type
|
||||
|
||||
assert type(errors_field.model.type) == List
|
||||
assert type(errors_field.model.type.of_type) == NonNull
|
||||
# TODO: how to test that the nonnull type is a string?
|
||||
|
|
Loading…
Reference in New Issue
Block a user