Skip to content

Commit 0988e07

Browse files
authored
Adds documentation to CAMELCASE_ERRORS setting (#689)
* Rename setting and add documentation * Add examples * Use `cls`
1 parent aa30750 commit 0988e07

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

docs/settings.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,42 @@ Default: ``100``
101101
GRAPHENE = {
102102
'RELAY_CONNECTION_MAX_LIMIT': 100,
103103
}
104+
105+
106+
``CAMELCASE_ERRORS``
107+
------------------------------------
108+
109+
When set to ``True`` field names in the ``errors`` object will be camel case.
110+
By default they will be snake case.
111+
112+
Default: ``False``
113+
114+
.. code:: python
115+
116+
GRAPHENE = {
117+
'CAMELCASE_ERRORS': False,
118+
}
119+
120+
# result = schema.execute(...)
121+
print(result.errors)
122+
# [
123+
# {
124+
# 'field': 'test_field',
125+
# 'messages': ['This field is required.'],
126+
# }
127+
# ]
128+
129+
.. code:: python
130+
131+
GRAPHENE = {
132+
'CAMELCASE_ERRORS': True,
133+
}
134+
135+
# result = schema.execute(...)
136+
print(result.errors)
137+
# [
138+
# {
139+
# 'field': 'testField',
140+
# 'messages': ['This field is required.'],
141+
# }
142+
# ]

graphene_django/forms/tests/test_mutation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class Meta:
5353

5454
result = PetMutation.mutate_and_get_payload(None, None)
5555
assert {f.field for f in result.errors} == {"name", "age", "test_field"}
56-
graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS = True
56+
graphene_settings.CAMELCASE_ERRORS = True
5757
result = PetMutation.mutate_and_get_payload(None, None)
5858
assert {f.field for f in result.errors} == {"name", "age", "testField"}
59-
graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS = False
59+
graphene_settings.CAMELCASE_ERRORS = False
6060

6161

6262
class ModelFormMutationTests(TestCase):

graphene_django/rest_framework/tests/test_mutation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ def test_model_mutate_and_get_payload_error():
215215

216216

217217
def test_mutation_error_camelcased():
218-
graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS = True
218+
graphene_settings.CAMELCASE_ERRORS = True
219219
result = MyModelMutation.mutate_and_get_payload(None, mock_info(), **{})
220220
assert result.errors[0].field == "coolName"
221-
graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS = False
221+
graphene_settings.CAMELCASE_ERRORS = False
222222

223223

224224
def test_invalid_serializer_operations():

graphene_django/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST": False,
3636
# Max items returned in ConnectionFields / FilterConnectionFields
3737
"RELAY_CONNECTION_MAX_LIMIT": 100,
38-
"DJANGO_GRAPHENE_CAMELCASE_ERRORS": False,
38+
"CAMELCASE_ERRORS": False,
3939
}
4040

4141
if settings.DEBUG:

graphene_django/types.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,5 @@ class ErrorType(ObjectType):
191191

192192
@classmethod
193193
def from_errors(cls, errors):
194-
data = (
195-
camelize(errors)
196-
if graphene_settings.DJANGO_GRAPHENE_CAMELCASE_ERRORS
197-
else errors
198-
)
199-
return [ErrorType(field=key, messages=value) for key, value in data.items()]
194+
data = camelize(errors) if graphene_settings.CAMELCASE_ERRORS else errors
195+
return [cls(field=key, messages=value) for key, value in data.items()]

0 commit comments

Comments
 (0)