Skip to content

Commit 3b44375

Browse files
Arun S KumarArun S Kumar
authored andcommitted
removed camel_to_snake custom function and used to_snake_case from graphene.utils.str_converters
re-indented documentation.
1 parent 5dfabd0 commit 3b44375

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

graphene_mongo/converter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import uuid
44

55
from graphene.types.json import JSONString
6+
from graphene.utils.str_converters import to_snake_case
67
from mongoengine.base import get_document
7-
88
from . import advanced_types
9-
from .utils import import_single_dispatch, get_field_description, get_query_fields, camel_to_snake
9+
from .utils import import_single_dispatch, get_field_description, get_query_fields
1010

1111
singledispatch = import_single_dispatch()
1212

@@ -172,11 +172,11 @@ def convert_field_to_dynamic(field, registry=None):
172172

173173
def reference_resolver(root, *args, **kwargs):
174174
document = getattr(root, field.name or field.db_name)
175-
only_fields = [camel_to_snake(i) for i in get_query_fields(args[0]).keys()]
175+
only_fields = [to_snake_case(i) for i in get_query_fields(args[0]).keys()]
176176
return field.document_type.objects().no_dereference().only(*only_fields).get(pk=document.id)
177177

178178
def cached_reference_resolver(root, *args, **kwargs):
179-
only_fields = [camel_to_snake(i) for i in get_query_fields(args[0]).keys()]
179+
only_fields = [to_snake_case(i) for i in get_query_fields(args[0]).keys()]
180180
return field.document_type.objects().no_dereference().only(*only_fields).get(
181181
pk=getattr(root, field.name or field.db_name))
182182

@@ -202,7 +202,7 @@ def convert_lazy_field_to_dynamic(field, registry=None):
202202

203203
def lazy_resolver(root, *args, **kwargs):
204204
document = getattr(root, field.name or field.db_name)
205-
only_fields = [camel_to_snake(i) for i in get_query_fields(args[0]).keys()]
205+
only_fields = [to_snake_case(i) for i in get_query_fields(args[0]).keys()]
206206
return document.document_type.objects().no_dereference().only(*only_fields).get(pk=document.pk)
207207

208208
def dynamic_type():

graphene_mongo/fields.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import graphene
77
import mongoengine
8+
from graphene.utils.str_converters import to_snake_case
89
from promise import Promise
910
from graphql_relay import from_global_id
1011
from graphene.relay import ConnectionField
@@ -21,7 +22,7 @@
2122
)
2223
from .converter import convert_mongoengine_field, MongoEngineConversionError
2324
from .registry import get_global_registry
24-
from .utils import get_model_reference_fields, get_node_from_global_id, get_query_fields, camel_to_snake
25+
from .utils import get_model_reference_fields, get_node_from_global_id, get_query_fields
2526

2627

2728
class MongoengineConnectionField(ConnectionField):
@@ -242,8 +243,8 @@ def default_resolver(self, _root, info, only_fields=list(), **args):
242243
def chained_resolver(self, resolver, is_partial, root, info, **args):
243244
only_fields = list()
244245
for field in get_query_fields(info):
245-
if camel_to_snake(field) in self.model._fields_ordered:
246-
only_fields.append(camel_to_snake(field))
246+
if to_snake_case(field) in self.model._fields_ordered:
247+
only_fields.append(to_snake_case(field))
247248
if not bool(args) or not is_partial:
248249
# XXX: Filter nested args
249250
resolved = resolver(root, info, **args)

graphene_mongo/utils.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,15 @@ def collect_query_fields(node, fragments):
113113
114114
Returns:
115115
A dict mapping each field found, along with their sub fields.
116-
117-
{'name': {},
118-
'image': {'id': {},
119-
'name': {},
120-
'description': {}},
121-
'slug': {}}
116+
{
117+
'name': {},
118+
'image': {
119+
'id': {},
120+
'name': {},
121+
'description': {}
122+
},
123+
'slug': {}
124+
}
122125
"""
123126

124127
field = {}
@@ -156,7 +159,3 @@ def get_query_fields(info):
156159
if "edges" in query:
157160
return query["edges"]["node"].keys()
158161
return query
159-
160-
161-
def camel_to_snake(field):
162-
return ''.join(['_' + c.lower() if c.isupper() else c for c in field]).lstrip('_')

0 commit comments

Comments
 (0)