mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-22 17:47:12 +03:00
Adds documentation to CAMELCASE_ERRORS
setting (#689)
* Rename setting and add documentation * Add examples * Use `cls`
This commit is contained in:
parent
aa30750d39
commit
0988e0798a
|
@ -101,3 +101,42 @@ Default: ``100``
|
|||
GRAPHENE = {
|
||||
'RELAY_CONNECTION_MAX_LIMIT': 100,
|
||||
}
|
||||
|
||||
|
||||
``CAMELCASE_ERRORS``
|
||||
------------------------------------
|
||||
|
||||
When set to ``True`` field names in the ``errors`` object will be camel case.
|
||||
By default they will be snake case.
|
||||
|
||||
Default: ``False``
|
||||
|
||||
.. code:: python
|
||||
|
||||
GRAPHENE = {
|
||||
'CAMELCASE_ERRORS': False,
|
||||
}
|
||||
|
||||
# result = schema.execute(...)
|
||||
print(result.errors)
|
||||
# [
|
||||
# {
|
||||
# 'field': 'test_field',
|
||||
# 'messages': ['This field is required.'],
|
||||
# }
|
||||
# ]
|
||||
|
||||
.. code:: python
|
||||
|
||||
GRAPHENE = {
|
||||
'CAMELCASE_ERRORS': True,
|
||||
}
|
||||
|
||||
# result = schema.execute(...)
|
||||
print(result.errors)
|
||||
# [
|
||||
# {
|
||||
# 'field': 'testField',
|
||||
# 'messages': ['This field is required.'],
|
||||
# }
|
||||
# ]
|
||||
|
|
|
@ -53,10 +53,10 @@ def test_mutation_error_camelcased():
|
|||
|
||||
result = PetMutation.mutate_and_get_payload(None, None)
|
||||
assert {f.field for f in result.errors} == {"name", "age", "test_field"}
|
||||
graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS = True
|
||||
graphene_settings.CAMELCASE_ERRORS = True
|
||||
result = PetMutation.mutate_and_get_payload(None, None)
|
||||
assert {f.field for f in result.errors} == {"name", "age", "testField"}
|
||||
graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS = False
|
||||
graphene_settings.CAMELCASE_ERRORS = False
|
||||
|
||||
|
||||
class ModelFormMutationTests(TestCase):
|
||||
|
|
|
@ -215,10 +215,10 @@ def test_model_mutate_and_get_payload_error():
|
|||
|
||||
|
||||
def test_mutation_error_camelcased():
|
||||
graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS = True
|
||||
graphene_settings.CAMELCASE_ERRORS = True
|
||||
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{})
|
||||
assert result.errors[0].field == "coolName"
|
||||
graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS = False
|
||||
graphene_settings.CAMELCASE_ERRORS = False
|
||||
|
||||
|
||||
def test_invalid_serializer_operations():
|
||||
|
|
|
@ -35,7 +35,7 @@ DEFAULTS = {
|
|||
"RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST": False,
|
||||
# Max items returned in ConnectionFields / FilterConnectionFields
|
||||
"RELAY_CONNECTION_MAX_LIMIT": 100,
|
||||
"DJANGO_GRAPHENE_CAMELCASE_ERRORS": False,
|
||||
"CAMELCASE_ERRORS": False,
|
||||
}
|
||||
|
||||
if settings.DEBUG:
|
||||
|
|
|
@ -191,9 +191,5 @@ class ErrorType(ObjectType):
|
|||
|
||||
@classmethod
|
||||
def from_errors(cls, errors):
|
||||
data = (
|
||||
camelize(errors)
|
||||
if graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS
|
||||
else errors
|
||||
)
|
||||
return [ErrorType(field=key, messages=value) for key, value in data.items()]
|
||||
data = camelize(errors) if graphene_settings.CAMELCASE_ERRORS else errors
|
||||
return [cls(field=key, messages=value) for key, value in data.items()]
|
||||
|
|
Loading…
Reference in New Issue
Block a user