Skip to content

Commit 595b945

Browse files
authored
Add QueryRefetchArgs. (#863)
A simple refactor for _save.
1 parent 05564de commit 595b945

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

gel/_internal/_save.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,11 +1156,40 @@ def feed_db_data(self, obj_data: Iterable[Any]) -> None:
11561156
pass
11571157

11581158

1159+
# Refetching links will only refetch linked objects which are already in the
1160+
# link, or are in the sync objects.
1161+
#
1162+
# A refetched object will therefore need, per link:
1163+
# - whether that link is being being refetched, and if so,
1164+
# - the objects already in the link.
1165+
#
1166+
# The order of links is kept consistent with the refetch shape.
1167+
RefetchSpecEntry = TypeAliasType(
1168+
"RefetchSpecEntry",
1169+
tuple[
1170+
uuid.UUID, # Refetched obj id
1171+
list[
1172+
tuple[
1173+
bool, # Whether the link is being refetched
1174+
list[uuid.UUID], # Objects in the link
1175+
]
1176+
],
1177+
],
1178+
)
1179+
1180+
1181+
@dataclasses.dataclass(kw_only=True, frozen=True)
1182+
class QueryRefetchArgs:
1183+
spec: list[RefetchSpecEntry]
1184+
new: list[uuid.UUID]
1185+
existing: list[uuid.UUID]
1186+
1187+
11591188
@_struct
11601189
class QueryRefetch:
11611190
executor: SaveExecutor
11621191
query: TypeWrapper[type[GelModel]]
1163-
args: dict[str, Any]
1192+
args: QueryRefetchArgs
11641193
shape: RefetchShape
11651194

11661195
def feed_db_data(self, obj_data: Iterable[Any]) -> None:
@@ -1305,7 +1334,7 @@ def _compile_refetch(
13051334
tuple[
13061335
type[GelModel],
13071336
TypeWrapper[type[GelModel]],
1308-
list[Any],
1337+
list[RefetchSpecEntry],
13091338
RefetchShape,
13101339
]
13111340
]:
@@ -1411,7 +1440,7 @@ def _compile_refetch(
14111440
else:
14121441
obj_link_ids.append(self._get_id(s_link))
14131442

1414-
spec_arg = [
1443+
spec_arg: list[RefetchSpecEntry] = [
14151444
(
14161445
obj_id,
14171446
[
@@ -1469,11 +1498,11 @@ def get_refetch_queries(
14691498
QueryRefetch(
14701499
executor=self,
14711500
query=q[1],
1472-
args={
1473-
"spec": q[2],
1474-
"new": new_ids,
1475-
"existing": list(self.existing_objects),
1476-
},
1501+
args=QueryRefetchArgs(
1502+
spec=q[2],
1503+
new=new_ids,
1504+
existing=list(self.existing_objects),
1505+
),
14771506
shape=q[3],
14781507
)
14791508
for q in self._compile_refetch()

gel/asyncio_client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,12 @@ async def _save_impl(
664664
if refetch:
665665
ref_queries = executor.get_refetch_queries()
666666
for ref in ref_queries:
667-
await tx.send_query(ref.query, **ref.args)
667+
await tx.send_query(
668+
ref.query,
669+
spec=ref.args.spec,
670+
new=ref.args.new,
671+
existing=ref.args.existing,
672+
)
668673

669674
refetch_data = await tx.wait()
670675
for ref_data, ref in zip(

gel/blocking_client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,12 @@ def _save_impl(
683683
if refetch:
684684
ref_queries = executor.get_refetch_queries()
685685
for ref in ref_queries:
686-
tx.send_query(ref.query, **ref.args)
686+
tx.send_query(
687+
ref.query,
688+
spec=ref.args.spec,
689+
new=ref.args.new,
690+
existing=ref.args.existing,
691+
)
687692

688693
refetch_data = tx.wait()
689694
for ref_data, ref in zip(

0 commit comments

Comments
 (0)