From bd10212e8935b71ec5986203bb18d18bf1c424cb Mon Sep 17 00:00:00 2001 From: cyc60 Date: Tue, 13 Jan 2026 16:09:42 +0300 Subject: [PATCH 1/2] Graph: Add cursor-based pagination --- pyproject.toml | 2 +- sw_utils/graph/client.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 33adda9..223cfa4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sw-utils" -version = "v0.12.0" +version = "v0.12.1" description = "StakeWise Python utils" authors = ["StakeWise Labs "] license = "GPL-3.0-or-later" diff --git a/sw_utils/graph/client.py b/sw_utils/graph/client.py index 1d71897..e9a2518 100644 --- a/sw_utils/graph/client.py +++ b/sw_utils/graph/client.py @@ -42,22 +42,27 @@ async def fetch_pages( query: DocumentNode, params: dict | None = None, page_size: int | None = None, + cursor_pagination: bool = False, ) -> list[dict]: """ Fetches all pages of the query. Returns concatenated result. + Supports both offset-based and cursor-based pagination. """ if page_size is None: page_size = self.page_size params = params.copy() if params else {} - skip = 0 # page offset + skip = 0 + last_id = '' all_items = [] - while True: - params.update({'first': page_size, 'skip': skip}) + if cursor_pagination: + params.update({'first': page_size, 'lastID': last_id}) + else: + params.update({'first': page_size, 'skip': skip}) + skip += page_size res = await self.run_query(query, params) - entity = list(res.keys())[0] items = res[entity] all_items.extend(items) @@ -65,7 +70,10 @@ async def fetch_pages( if len(items) < page_size: break - skip += page_size + if cursor_pagination: + last_id = items[-1]['id'] + else: + skip += page_size return all_items From 7d47404248b752bef8443defd1cf5e6dda37e04b Mon Sep 17 00:00:00 2001 From: cyc60 Date: Tue, 13 Jan 2026 16:17:09 +0300 Subject: [PATCH 2/2] Review fix --- sw_utils/graph/client.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sw_utils/graph/client.py b/sw_utils/graph/client.py index e9a2518..09a2813 100644 --- a/sw_utils/graph/client.py +++ b/sw_utils/graph/client.py @@ -54,14 +54,13 @@ async def fetch_pages( params = params.copy() if params else {} skip = 0 - last_id = '' + cursor = '' all_items = [] while True: if cursor_pagination: - params.update({'first': page_size, 'lastID': last_id}) + params.update({'first': page_size, 'lastID': cursor}) else: params.update({'first': page_size, 'skip': skip}) - skip += page_size res = await self.run_query(query, params) entity = list(res.keys())[0] items = res[entity] @@ -71,7 +70,7 @@ async def fetch_pages( break if cursor_pagination: - last_id = items[-1]['id'] + cursor = items[-1]['id'] else: skip += page_size