Skip to content

Commit 565f729

Browse files
authored
Merge pull request #370 from opsmill/wvd-hfid-card-many
Add hfid support for cardinality many relationships
2 parents 06fff9d + 0d2110a commit 565f729

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Set the HFID on related nodes for cardinality many relationships and add hfid support to the RelationshipManager add, extend and remove methods

infrahub_sdk/node.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def _generate_query_data(cls, peer_data: dict[str, Any] | None = None, property:
479479
"""
480480
data: dict[str, Any] = {
481481
"count": None,
482-
"edges": {"node": {"id": None, "display_label": None, "__typename": None}},
482+
"edges": {"node": {"id": None, "hfid": None, "display_label": None, "__typename": None}},
483483
}
484484

485485
properties: dict[str, Any] = {}
@@ -569,7 +569,9 @@ def add(self, data: str | RelatedNode | dict) -> None:
569569
raise UninitializedError("Must call fetch() on RelationshipManager before editing members")
570570
new_node = RelatedNode(schema=self.schema, client=self.client, branch=self.branch, data=data)
571571

572-
if new_node.id and new_node.id not in self.peer_ids:
572+
if (new_node.id and new_node.id not in self.peer_ids) or (
573+
new_node.hfid and new_node.hfid not in self.peer_hfids
574+
):
573575
self.peers.append(new_node)
574576
self._has_update = True
575577

@@ -591,6 +593,14 @@ def remove(self, data: str | RelatedNode | dict) -> None:
591593
self.peers.pop(idx)
592594
self._has_update = True
593595

596+
elif node_to_remove.hfid and node_to_remove.hfid in self.peer_hfids:
597+
idx = self.peer_hfids.index(node_to_remove.hfid)
598+
if self.peers[idx].hfid != node_to_remove.hfid:
599+
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.hfid}")
600+
601+
self.peers.pop(idx)
602+
self._has_update = True
603+
594604

595605
class RelationshipManagerSync(RelationshipManagerBase):
596606
"""Manages relationships of a node in a synchronous context."""
@@ -664,7 +674,9 @@ def add(self, data: str | RelatedNodeSync | dict) -> None:
664674
raise UninitializedError("Must call fetch() on RelationshipManager before editing members")
665675
new_node = RelatedNodeSync(schema=self.schema, client=self.client, branch=self.branch, data=data)
666676

667-
if new_node.id and new_node.id not in self.peer_ids:
677+
if (new_node.id and new_node.id not in self.peer_ids) or (
678+
new_node.hfid and new_node.hfid not in self.peer_hfids
679+
):
668680
self.peers.append(new_node)
669681
self._has_update = True
670682

@@ -682,6 +694,13 @@ def remove(self, data: str | RelatedNodeSync | dict) -> None:
682694
idx = self.peer_ids.index(node_to_remove.id)
683695
if self.peers[idx].id != node_to_remove.id:
684696
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.id}")
697+
self.peers.pop(idx)
698+
self._has_update = True
699+
700+
elif node_to_remove.hfid and node_to_remove.hfid in self.peer_hfids:
701+
idx = self.peer_hfids.index(node_to_remove.hfid)
702+
if self.peers[idx].hfid != node_to_remove.hfid:
703+
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.hfid}")
685704

686705
self.peers.pop(idx)
687706
self._has_update = True

tests/unit/sdk/test_node.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,7 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
11051105
},
11061106
"node": {
11071107
"id": None,
1108+
"hfid": None,
11081109
"display_label": None,
11091110
"__typename": None,
11101111
},
@@ -1157,6 +1158,7 @@ async def test_query_data_include(client, location_schema: NodeSchemaAPI, client
11571158
"edges": {
11581159
"node": {
11591160
"id": None,
1161+
"hfid": None,
11601162
"display_label": None,
11611163
"__typename": None,
11621164
},

0 commit comments

Comments
 (0)