Skip to content

Adds documentation to CAMELCASE_ERRORS setting #689

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
merged 3 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.'],
# }
# ]
4 changes: 2 additions & 2 deletions graphene_django/forms/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class Meta:

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):
Expand Down
4 changes: 2 additions & 2 deletions graphene_django/rest_framework/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion graphene_django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"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:
Expand Down
8 changes: 2 additions & 6 deletions graphene_django/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]