@@ -730,19 +730,20 @@ async def generate_query_data_node(
730730 continue
731731
732732 rel_schema = self ._schema .get_relationship (name = rel_name )
733-
734733 if not rel_schema or (not inherited and rel_schema .inherited ):
735734 continue
736735
736+ # Don't fetch many attribute/parent relationships unless they are specified in `include`
737+ # TODO Why wouldn't we we fetch them if prefetch_relationships is True?
737738 if (
738739 rel_schema .cardinality == RelationshipCardinality .MANY # type: ignore[union-attr]
739740 and rel_schema .kind not in [RelationshipKind .ATTRIBUTE , RelationshipKind .PARENT ] # type: ignore[union-attr]
740741 and not (include and rel_name in include )
741742 ):
742743 continue
743744
744- peer_data : dict [ str , Any ] = {}
745- if rel_schema and prefetch_relationships :
745+ should_fetch_relationship = prefetch_relationships or ( include is not None and rel_name in include )
746+ if rel_schema and should_fetch_relationship :
746747 peer_schema = await self ._client .schema .get (kind = rel_schema .peer , branch = self ._branch )
747748 peer_node = InfrahubNode (client = self ._client , schema = peer_schema , branch = self ._branch )
748749 peer_data = await peer_node .generate_query_data_node (
@@ -751,23 +752,26 @@ async def generate_query_data_node(
751752 property = property ,
752753 )
753754
754- if rel_schema and rel_schema .cardinality == "one" :
755- rel_data = RelatedNode ._generate_query_data (peer_data = peer_data , property = property )
756- # Nodes involved in a hierarchy are required to inherit from a common ancestor node, and graphql
757- # tries to resolve attributes in this ancestor instead of actual node. To avoid
758- # invalid queries issues when attribute is missing in the common ancestor, we use a fragment
759- # to explicit actual node kind we are querying.
760- if rel_schema .kind == RelationshipKind .HIERARCHY :
761- data_node = rel_data ["node" ]
762- rel_data ["node" ] = {}
763- rel_data ["node" ][f"...on { rel_schema .peer } " ] = data_node
764- elif rel_schema and rel_schema .cardinality == "many" :
765- rel_data = RelationshipManager ._generate_query_data (peer_data = peer_data , property = property )
755+ # TODO is there a reason why we fetch here even with prefetch_relationships == False?
756+ if rel_schema .cardinality == "one" :
757+ rel_data = RelatedNode ._generate_query_data (peer_data = peer_data , property = property )
758+ # Nodes involved in a hierarchy are required to inherit from a common ancestor node, and graphql
759+ # tries to resolve attributes in this ancestor instead of actual node. To avoid
760+ # invalid queries issues when attribute is missing in the common ancestor, we use a fragment
761+ # to explicit actual node kind we are querying.
762+ if rel_schema .kind == RelationshipKind .HIERARCHY :
763+ data_node = rel_data ["node" ]
764+ rel_data ["node" ] = {}
765+ rel_data ["node" ][f"...on { rel_schema .peer } " ] = data_node
766+ elif rel_schema .cardinality == "many" :
767+ rel_data = RelationshipManager ._generate_query_data (peer_data = peer_data , property = property )
768+ else :
769+ raise ValueError (f"Unknown relationship cardinality { rel_schema .cardinality } " )
766770
767- data [rel_name ] = rel_data
771+ data [rel_name ] = rel_data
768772
769- if insert_alias :
770- data [rel_name ]["@alias" ] = f"__alias__{ self ._schema .kind } __{ rel_name } "
773+ if insert_alias :
774+ data [rel_name ]["@alias" ] = f"__alias__{ self ._schema .kind } __{ rel_name } "
771775
772776 return data
773777
0 commit comments