Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set the HFID on related nodes for cardinality many relationships and add hfid support to the RelationshipManager add, extend and remove methods
25 changes: 22 additions & 3 deletions infrahub_sdk/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def _generate_query_data(cls, peer_data: dict[str, Any] | None = None, property:
"""
data: dict[str, Any] = {
"count": None,
"edges": {"node": {"id": None, "display_label": None, "__typename": None}},
"edges": {"node": {"id": None, "hfid": None, "display_label": None, "__typename": None}},
}

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

if new_node.id and new_node.id not in self.peer_ids:
if (new_node.id and new_node.id not in self.peer_ids) or (
new_node.hfid and new_node.hfid not in self.peer_hfids
):
self.peers.append(new_node)
self._has_update = True

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

elif node_to_remove.hfid and node_to_remove.hfid in self.peer_hfids:
idx = self.peer_hfids.index(node_to_remove.hfid)
if self.peers[idx].hfid != node_to_remove.hfid:
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.hfid}")

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


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

if new_node.id and new_node.id not in self.peer_ids:
if (new_node.id and new_node.id not in self.peer_ids) or (
new_node.hfid and new_node.hfid not in self.peer_hfids
):
self.peers.append(new_node)
self._has_update = True

Expand All @@ -682,6 +694,13 @@ def remove(self, data: str | RelatedNodeSync | dict) -> None:
idx = self.peer_ids.index(node_to_remove.id)
if self.peers[idx].id != node_to_remove.id:
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.id}")
self.peers.pop(idx)
self._has_update = True

elif node_to_remove.hfid and node_to_remove.hfid in self.peer_hfids:
idx = self.peer_hfids.index(node_to_remove.hfid)
if self.peers[idx].hfid != node_to_remove.hfid:
raise IndexError(f"Unexpected situation, the node with the index {idx} should be {node_to_remove.hfid}")

self.peers.pop(idx)
self._has_update = True
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/sdk/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,7 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
},
"node": {
"id": None,
"hfid": None,
"display_label": None,
"__typename": None,
},
Expand Down Expand Up @@ -1157,6 +1158,7 @@ async def test_query_data_include(client, location_schema: NodeSchemaAPI, client
"edges": {
"node": {
"id": None,
"hfid": None,
"display_label": None,
"__typename": None,
},
Expand Down