Skip to content

Commit 0b37013

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 c64c759 commit 0b37013

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

graphene_mongo/converter.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def convert_field_to_list(field, registry=None):
121121
# Non-relationship field
122122
relations = (mongoengine.ReferenceField, mongoengine.EmbeddedDocumentField)
123123
if not isinstance(base_type, (graphene.List, graphene.NonNull)) and not isinstance(
124-
field.field, relations
124+
field.field, relations
125125
):
126126
base_type = type(base_type)
127127

@@ -135,7 +135,6 @@ def convert_field_to_list(field, registry=None):
135135
@convert_mongoengine_field.register(mongoengine.GenericEmbeddedDocumentField)
136136
@convert_mongoengine_field.register(mongoengine.GenericReferenceField)
137137
def convert_field_to_union(field, registry=None):
138-
139138
_types = []
140139
for choice in field.choices:
141140
if isinstance(field, mongoengine.GenericReferenceField):
@@ -171,11 +170,28 @@ def convert_field_to_union(field, registry=None):
171170
def convert_field_to_dynamic(field, registry=None):
172171
model = field.document_type
173172

173+
def reference_resolver(root, *args, **kwargs):
174+
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)
177+
178+
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)
182+
174183
def dynamic_type():
175184
_type = registry.get_type_for_model(model)
176185
if not _type:
177186
return None
178-
return graphene.Field(_type, description=get_field_description(field, registry))
187+
elif isinstance(field, mongoengine.ReferenceField):
188+
graphene.Field(_type, resolver=reference_resolver,
189+
description=get_field_description(field, registry))
190+
elif isinstance(field, mongoengine.CachedReferenceField):
191+
return graphene.Field(_type, resolver=cached_reference_resolver,
192+
description=get_field_description(field, registry))
193+
return graphene.Field(_type,
194+
description=get_field_description(field, registry))
179195

180196
return graphene.Dynamic(dynamic_type)
181197

graphene_mongo/fields.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import graphene
77
import mongoengine
8-
from graphql.utils.ast_to_dict import ast_to_dict
98
from promise import Promise
109
from graphql_relay import from_global_id
1110
from graphene.relay import ConnectionField

0 commit comments

Comments
 (0)