@@ -730,48 +730,43 @@ async def generate_query_data_node(
730730 continue
731731
732732 rel_schema = self ._schema .get_relationship (name = rel_name )
733+
733734 if not rel_schema or (not inherited and rel_schema .inherited ):
734735 continue
735736
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?
738737 if (
739738 rel_schema .cardinality == RelationshipCardinality .MANY # type: ignore[union-attr]
740739 and rel_schema .kind not in [RelationshipKind .ATTRIBUTE , RelationshipKind .PARENT ] # type: ignore[union-attr]
741740 and not (include and rel_name in include )
742741 ):
743742 continue
744743
744+ peer_data : dict [str , Any ] = {}
745745 should_fetch_relationship = prefetch_relationships or (include is not None and rel_name in include )
746746 if rel_schema and should_fetch_relationship :
747747 peer_schema = await self ._client .schema .get (kind = rel_schema .peer , branch = self ._branch )
748748 peer_node = InfrahubNode (client = self ._client , schema = peer_schema , branch = self ._branch )
749749 peer_data = await peer_node .generate_query_data_node (
750- include = include ,
751- exclude = exclude ,
752750 property = property ,
753751 )
754752
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 } " )
753+ if rel_schema and rel_schema .cardinality == "one" :
754+ rel_data = RelatedNode ._generate_query_data (peer_data = peer_data , property = property )
755+ # Nodes involved in a hierarchy are required to inherit from a common ancestor node, and graphql
756+ # tries to resolve attributes in this ancestor instead of actual node. To avoid
757+ # invalid queries issues when attribute is missing in the common ancestor, we use a fragment
758+ # to explicit actual node kind we are querying.
759+ if rel_schema .kind == RelationshipKind .HIERARCHY :
760+ data_node = rel_data ["node" ]
761+ rel_data ["node" ] = {}
762+ rel_data ["node" ][f"...on { rel_schema .peer } " ] = data_node
763+ elif rel_schema and rel_schema .cardinality == "many" :
764+ rel_data = RelationshipManager ._generate_query_data (peer_data = peer_data , property = property )
770765
771- data [rel_name ] = rel_data
766+ data [rel_name ] = rel_data
772767
773- if insert_alias :
774- data [rel_name ]["@alias" ] = f"__alias__{ self ._schema .kind } __{ rel_name } "
768+ if insert_alias :
769+ data [rel_name ]["@alias" ] = f"__alias__{ self ._schema .kind } __{ rel_name } "
775770
776771 return data
777772
@@ -890,7 +885,12 @@ async def update(
890885 await self ._process_mutation_result (mutation_name = mutation_name , response = response , timeout = timeout )
891886
892887 async def _process_relationships (
893- self , node_data : dict [str , Any ], branch : str , related_nodes : list [InfrahubNode ], timeout : int | None = None
888+ self ,
889+ node_data : dict [str , Any ],
890+ branch : str ,
891+ related_nodes : list [InfrahubNode ],
892+ timeout : int | None = None ,
893+ include : list [str ] | None = None ,
894894 ) -> None :
895895 """Processes the Relationships of a InfrahubNode and add Related Nodes to a list.
896896
@@ -901,6 +901,8 @@ async def _process_relationships(
901901 timeout (int, optional): Overrides default timeout used when querying the graphql API. Specified in seconds.
902902 """
903903 for rel_name in self ._relationships :
904+ if include is not None and rel_name not in include :
905+ continue
904906 rel = getattr (self , rel_name )
905907 if rel and isinstance (rel , RelatedNode ):
906908 relation = node_data ["node" ].get (rel_name , None )
0 commit comments