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
49 changes: 47 additions & 2 deletions infrahub_sdk/node/relationship.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import annotations

from collections import defaultdict
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any

from ..exceptions import (
Error,
UninitializedError,
)
from ..types import Order
from .constants import PROPERTIES_FLAG, PROPERTIES_OBJECT
from .related_node import RelatedNode, RelatedNodeSync

Expand Down Expand Up @@ -156,8 +159,29 @@ async def fetch(self) -> None:
self.peers = rm.peers
self.initialized = True

ids_per_kind_map = defaultdict(list)
for peer in self.peers:
await peer.fetch() # type: ignore[misc]
if not peer.id or not peer.typename:
raise Error("Unable to fetch the peer, id and/or typename are not defined")
if peer.typename not in ids_per_kind_map:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the defaultdict you can just do

ids_per_kind_map[peer.typename].append(peer.id)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 😅 will try to include this in another PR

ids_per_kind_map[peer.typename] = [peer.id]
else:
ids_per_kind_map[peer.typename].append(peer.id)

batch = await self.client.create_batch()
for kind, ids in ids_per_kind_map.items():
batch.add(
task=self.client.filters,
kind=kind,
ids=ids,
populate_store=True,
branch=self.branch,
parallel=True,
order=Order(disable=True),
)

async for _ in batch.execute():
pass

def add(self, data: str | RelatedNode | dict) -> None:
"""Add a new peer to this relationship."""
Expand Down Expand Up @@ -261,8 +285,29 @@ def fetch(self) -> None:
self.peers = rm.peers
self.initialized = True

ids_per_kind_map = defaultdict(list)
for peer in self.peers:
peer.fetch()
if not peer.id or not peer.typename:
raise Error("Unable to fetch the peer, id and/or typename are not defined")
if peer.typename not in ids_per_kind_map:
ids_per_kind_map[peer.typename] = [peer.id]
else:
ids_per_kind_map[peer.typename].append(peer.id)

batch = self.client.create_batch()
for kind, ids in ids_per_kind_map.items():
batch.add(
task=self.client.filters,
kind=kind,
ids=ids,
populate_store=True,
branch=self.branch,
parallel=True,
order=Order(disable=True),
)

for _ in batch.execute():
pass

def add(self, data: str | RelatedNodeSync | dict) -> None:
"""Add a new peer to this relationship."""
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/sdk/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,19 @@ async def test_node_fetch_relationship(
)

response2 = {
"data": {
"BuiltinTag": {
"count": 1,
}
}
}

httpx_mock.add_response(
method="POST",
json=response2,
)

response3 = {
"data": {
"BuiltinTag": {
"count": 1,
Expand All @@ -1895,7 +1908,7 @@ async def test_node_fetch_relationship(

httpx_mock.add_response(
method="POST",
json=response2,
json=response3,
match_headers={"X-Infrahub-Tracker": "query-builtintag-page1"},
)

Expand Down