From cc44dd849b1294a6b48fe457f65ed2d2cdcfd008 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Tue, 25 Jun 2019 09:59:39 +0100 Subject: [PATCH 1/3] Rename setting and add documentation --- docs/settings.rst | 15 +++++++++++++++ graphene_django/forms/tests/test_mutation.py | 4 ++-- .../rest_framework/tests/test_mutation.py | 4 ++-- graphene_django/settings.py | 2 +- graphene_django/types.py | 6 +----- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index 4d37a996c..6672fc46b 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -101,3 +101,18 @@ 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, + } diff --git a/graphene_django/forms/tests/test_mutation.py b/graphene_django/forms/tests/test_mutation.py index 4c467020a..2de511381 100644 --- a/graphene_django/forms/tests/test_mutation.py +++ b/graphene_django/forms/tests/test_mutation.py @@ -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): diff --git a/graphene_django/rest_framework/tests/test_mutation.py b/graphene_django/rest_framework/tests/test_mutation.py index 0dd5ad380..9d8b95022 100644 --- a/graphene_django/rest_framework/tests/test_mutation.py +++ b/graphene_django/rest_framework/tests/test_mutation.py @@ -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(): diff --git a/graphene_django/settings.py b/graphene_django/settings.py index 1b49dfbe1..af6389056 100644 --- a/graphene_django/settings.py +++ b/graphene_django/settings.py @@ -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: diff --git a/graphene_django/types.py b/graphene_django/types.py index c296707cd..9b1cb0811 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -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 - ) + data = camelize(errors) if graphene_settings.CAMELCASE_ERRORS else errors return [ErrorType(field=key, messages=value) for key, value in data.items()] From c5d170cb8449cdd7f27c6bd1e494847cc9da46d4 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Tue, 25 Jun 2019 10:09:05 +0100 Subject: [PATCH 2/3] Add examples --- docs/settings.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/settings.rst b/docs/settings.rst index 6672fc46b..4776ce004 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -116,3 +116,27 @@ Default: ``False`` 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.'], + # } + # ] From 58e770fbaa85caca781173318a1a8c04df4521e7 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Tue, 25 Jun 2019 10:15:03 +0100 Subject: [PATCH 3/3] Use `cls` --- graphene_django/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene_django/types.py b/graphene_django/types.py index 9b1cb0811..6c100ef0e 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -192,4 +192,4 @@ class ErrorType(ObjectType): @classmethod def from_errors(cls, errors): data = camelize(errors) if graphene_settings.CAMELCASE_ERRORS else errors - return [ErrorType(field=key, messages=value) for key, value in data.items()] + return [cls(field=key, messages=value) for key, value in data.items()]