Skip to content

Commit 0845b40

Browse files
Arun S KumarArun S Kumar
authored andcommitted
Support Added for GenericReferenceField
1 parent 61c8592 commit 0845b40

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

graphene_mongo/converter.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ def convert_field_to_union(field, registry=None):
161161
)
162162
Meta = type("Meta", (object,), {"types": tuple(_types)})
163163
_union = type(name, (graphene.Union,), {"Meta": Meta})
164+
165+
def reference_resolver(root, *args, **kwargs):
166+
dereferenced = getattr(root, field.name or field.db_name)
167+
document = get_document(dereferenced["_cls"])
168+
document_field= mongoengine.ReferenceField(document)
169+
document_field = convert_mongoengine_field(document_field, registry)
170+
document_field_type = document_field.get_type().type._meta.name
171+
only_fields = [to_snake_case(i) for i in get_query_fields(args[0])[document_field_type].keys()]
172+
return document.objects().no_dereference().only(*only_fields).get(pk=dereferenced["_ref"].id)
173+
174+
if isinstance(field, mongoengine.GenericReferenceField):
175+
return graphene.Field(_union, resolver=reference_resolver)
176+
164177
return graphene.Field(_union)
165178

166179

@@ -172,8 +185,10 @@ def convert_field_to_dynamic(field, registry=None):
172185

173186
def reference_resolver(root, *args, **kwargs):
174187
document = getattr(root, field.name or field.db_name)
175-
only_fields = [to_snake_case(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)
188+
if document:
189+
only_fields = [to_snake_case(i) for i in get_query_fields(args[0]).keys()]
190+
return field.document_type.objects().no_dereference().only(*only_fields).get(pk=document.id)
191+
return None
177192

178193
def cached_reference_resolver(root, *args, **kwargs):
179194
only_fields = [to_snake_case(i) for i in get_query_fields(args[0]).keys()]
@@ -186,7 +201,7 @@ def dynamic_type():
186201
return None
187202
elif isinstance(field, mongoengine.ReferenceField):
188203
return graphene.Field(_type, resolver=reference_resolver,
189-
description=get_field_description(field, registry))
204+
description=get_field_description(field, registry))
190205
elif isinstance(field, mongoengine.CachedReferenceField):
191206
return graphene.Field(_type, resolver=cached_reference_resolver,
192207
description=get_field_description(field, registry))

graphene_mongo/fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,13 @@ def get_queryset(self, model, info, only_fields=list(), **args):
200200
else:
201201
args.update(queryset_or_filters)
202202

203-
return model.objects(**args).only(*only_fields).order_by(self.order_by)
203+
return model.objects(**args).no_dereference().only(*only_fields).order_by(self.order_by)
204204

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

208208
if _root is not None and getattr(_root, info.field_name, []) is not None:
209-
args["pk__in"] = [r.pk for r in getattr(_root, info.field_name, [])]
209+
args["pk__in"] = [r.id for r in getattr(_root, info.field_name, [])]
210210

211211
connection_args = {
212212
"first": args.pop("first", None),

graphene_mongo/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ def collect_query_fields(node, fragments):
135135
elif leaf['kind'] == 'FragmentSpread':
136136
field.update(collect_query_fields(fragments[leaf['name']['value']],
137137
fragments))
138+
elif leaf['kind'] == 'InlineFragment':
139+
field.update({
140+
leaf["type_condition"]["name"]['value']: collect_query_fields(leaf, fragments)
141+
})
142+
pass
138143

139144
return field
140145

0 commit comments

Comments
 (0)