@@ -545,7 +545,7 @@ def convert_field_to_dynamic(field, registry=None, executor: ExecutorEnum = Exec
545
545
model = field .document_type
546
546
547
547
def reference_resolver (root , * args , ** kwargs ):
548
- document = getattr ( root , field .name or field .db_name )
548
+ document = root . _data . get ( field .name or field .db_name , None )
549
549
if document :
550
550
queried_fields = list ()
551
551
_type = registry .get_type_for_model (field .document_type , executor = executor )
@@ -558,16 +558,23 @@ def reference_resolver(root, *args, **kwargs):
558
558
item = to_snake_case (each )
559
559
if item in field .document_type ._fields_ordered + tuple (filter_args ):
560
560
queried_fields .append (item )
561
+
562
+ fields_to_fetch = set (list (_type ._meta .required_fields ) + queried_fields )
563
+ if isinstance (document , field .document_type ) and all (
564
+ document ._data [_field ] is not None for _field in fields_to_fetch
565
+ ):
566
+ return document # Data is already fetched
561
567
return (
562
568
field .document_type .objects ()
563
569
.no_dereference ()
564
- .only (* ( set ( list ( _type . _meta . required_fields ) + queried_fields )) )
570
+ .only (* fields_to_fetch )
565
571
.get (pk = document .id )
566
572
)
567
573
return None
568
574
569
575
def cached_reference_resolver (root , * args , ** kwargs ):
570
- if field :
576
+ document = root ._data .get (field .name or field .db_name , None )
577
+ if document :
571
578
queried_fields = list ()
572
579
_type = registry .get_type_for_model (field .document_type , executor = executor )
573
580
filter_args = list ()
@@ -579,16 +586,22 @@ def cached_reference_resolver(root, *args, **kwargs):
579
586
item = to_snake_case (each )
580
587
if item in field .document_type ._fields_ordered + tuple (filter_args ):
581
588
queried_fields .append (item )
589
+
590
+ fields_to_fetch = set (list (_type ._meta .required_fields ) + queried_fields )
591
+ if isinstance (document , field .document_type ) and all (
592
+ document ._data [_field ] is not None for _field in fields_to_fetch
593
+ ):
594
+ return document # Data is already fetched
582
595
return (
583
596
field .document_type .objects ()
584
597
.no_dereference ()
585
- .only (* ( set ( list ( _type . _meta . required_fields ) + queried_fields )) )
598
+ .only (* fields_to_fetch )
586
599
.get (pk = getattr (root , field .name or field .db_name ))
587
600
)
588
601
return None
589
602
590
603
async def reference_resolver_async (root , * args , ** kwargs ):
591
- document = getattr ( root , field .name or field .db_name )
604
+ document = root . _data . get ( field .name or field .db_name , None )
592
605
if document :
593
606
queried_fields = list ()
594
607
_type = registry .get_type_for_model (field .document_type , executor = executor )
@@ -601,16 +614,20 @@ async def reference_resolver_async(root, *args, **kwargs):
601
614
item = to_snake_case (each )
602
615
if item in field .document_type ._fields_ordered + tuple (filter_args ):
603
616
queried_fields .append (item )
617
+
618
+ fields_to_fetch = set (list (_type ._meta .required_fields ) + queried_fields )
619
+ if isinstance (document , field .document_type ) and all (
620
+ document ._data [_field ] is not None for _field in fields_to_fetch
621
+ ):
622
+ return document # Data is already fetched
604
623
return await sync_to_async (
605
- field .document_type .objects ()
606
- .no_dereference ()
607
- .only (* (set (list (_type ._meta .required_fields ) + queried_fields )))
608
- .get
624
+ field .document_type .objects ().no_dereference ().only (* fields_to_fetch ).get
609
625
)(pk = document .id )
610
626
return None
611
627
612
628
async def cached_reference_resolver_async (root , * args , ** kwargs ):
613
- if field :
629
+ document = root ._data .get (field .name or field .db_name , None )
630
+ if document :
614
631
queried_fields = list ()
615
632
_type = registry .get_type_for_model (field .document_type , executor = executor )
616
633
filter_args = list ()
@@ -622,11 +639,14 @@ async def cached_reference_resolver_async(root, *args, **kwargs):
622
639
item = to_snake_case (each )
623
640
if item in field .document_type ._fields_ordered + tuple (filter_args ):
624
641
queried_fields .append (item )
642
+
643
+ fields_to_fetch = set (list (_type ._meta .required_fields ) + queried_fields )
644
+ if isinstance (document , field .document_type ) and all (
645
+ document ._data [_field ] is not None for _field in fields_to_fetch
646
+ ):
647
+ return document # Data is already fetched
625
648
return await sync_to_async (
626
- field .document_type .objects ()
627
- .no_dereference ()
628
- .only (* (set (list (_type ._meta .required_fields ) + queried_fields )))
629
- .get
649
+ field .document_type .objects ().no_dereference ().only (* fields_to_fetch ).get
630
650
)(pk = getattr (root , field .name or field .db_name ))
631
651
return None
632
652
0 commit comments