@@ -121,7 +121,7 @@ def convert_field_to_list(field, registry=None):
121
121
# Non-relationship field
122
122
relations = (mongoengine .ReferenceField , mongoengine .EmbeddedDocumentField )
123
123
if not isinstance (base_type , (graphene .List , graphene .NonNull )) and not isinstance (
124
- field .field , relations
124
+ field .field , relations
125
125
):
126
126
base_type = type (base_type )
127
127
@@ -135,7 +135,6 @@ def convert_field_to_list(field, registry=None):
135
135
@convert_mongoengine_field .register (mongoengine .GenericEmbeddedDocumentField )
136
136
@convert_mongoengine_field .register (mongoengine .GenericReferenceField )
137
137
def convert_field_to_union (field , registry = None ):
138
-
139
138
_types = []
140
139
for choice in field .choices :
141
140
if isinstance (field , mongoengine .GenericReferenceField ):
@@ -171,11 +170,28 @@ def convert_field_to_union(field, registry=None):
171
170
def convert_field_to_dynamic (field , registry = None ):
172
171
model = field .document_type
173
172
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
+
174
183
def dynamic_type ():
175
184
_type = registry .get_type_for_model (model )
176
185
if not _type :
177
186
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 ))
179
195
180
196
return graphene .Dynamic (dynamic_type )
181
197
0 commit comments