Skip to content

Commit b72dcf4

Browse files
Arun S KumarArun S Kumar
authored andcommitted
Query efficiency and performance - Retrieving only the queried fields from the database by implementing deafult resolvers for both ReferenceField & CachedReferenceField.
1 parent 0b37013 commit b72dcf4

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

graphene_mongo/converter.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from mongoengine.base import get_document
77

88
from . import advanced_types
9-
from .utils import import_single_dispatch, get_field_description, get_query_fields
9+
from .utils import import_single_dispatch, get_field_description, get_query_fields, camel_to_snake
1010

1111
singledispatch = import_single_dispatch()
1212

@@ -172,13 +172,13 @@ 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 = get_query_fields(args[0]).keys()
176-
return field.document_type.objects().only(*only_fields).get(pk=document.id)
175+
only_fields = [camel_to_snake(i) for i in get_query_fields(args[0]).keys()]
176+
return field.document_type.objects().no_dereference().only(*only_fields).get(pk=document.id)
177177

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

183183
def dynamic_type():
184184
_type = registry.get_type_for_model(model)
@@ -201,10 +201,9 @@ def convert_lazy_field_to_dynamic(field, registry=None):
201201
model = field.document_type
202202

203203
def lazy_resolver(root, *args, **kwargs):
204-
if getattr(root, field.name or field.db_name):
205-
only_fields = get_query_fields(args[0]).keys()
206-
document = getattr(root, field.name or field.db_name)
207-
return document.document_type.objects().only(*only_fields).get(pk=document.pk)
204+
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()]
206+
return document.document_type.objects().no_dereference().only(*only_fields).get(pk=document.pk)
208207

209208
def dynamic_type():
210209
_type = registry.get_type_for_model(model)

graphene_mongo/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def get_queryset(self, model, info, only_fields=list(), **args):
199199
else:
200200
args.update(queryset_or_filters)
201201

202-
return model.objects(**args).order_by(self.order_by)
202+
return model.objects(**args).only(*only_fields).order_by(self.order_by)
203203

204204
def default_resolver(self, _root, info, only_fields=list(), **args):
205205
args = args or {}

0 commit comments

Comments
 (0)