From bd561078d0add6edbf372293fa3003a9f279e9a6 Mon Sep 17 00:00:00 2001 From: Daniel Werner Date: Mon, 18 Nov 2024 13:15:31 -0800 Subject: [PATCH] common: add pagination support to allocation queries --- .../resolvers/allocations.ts | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/packages/indexer-common/src/indexer-management/resolvers/allocations.ts b/packages/indexer-common/src/indexer-management/resolvers/allocations.ts index f5ad5da3a..692a2f5fe 100644 --- a/packages/indexer-common/src/indexer-management/resolvers/allocations.ts +++ b/packages/indexer-common/src/indexer-management/resolvers/allocations.ts @@ -1,4 +1,4 @@ -import { epochElapsedBlocks, Network } from '@graphprotocol/indexer-common' +import { epochElapsedBlocks, Network, QueryResult } from '@graphprotocol/indexer-common' /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ /* eslint-disable @typescript-eslint/ban-types */ @@ -67,7 +67,8 @@ interface AllocationInfo { const ALLOCATION_QUERIES = { [AllocationQuery.all]: gql` query allocations($indexer: String!) { - allocations(where: { indexer: $indexer }, first: 1000) { + allocations(where: { indexer: $indexer }, first: $first, after: $after) { + totalCount id subgraphDeployment { id @@ -88,7 +89,12 @@ const ALLOCATION_QUERIES = { `, [AllocationQuery.active]: gql` query allocations($indexer: String!) { - allocations(where: { indexer: $indexer, status: Active }, first: 1000) { + allocations( + where: { indexer: $indexer, status: Active } + first: $first + after: $after + ) { + totalCount id subgraphDeployment { id @@ -109,7 +115,12 @@ const ALLOCATION_QUERIES = { `, [AllocationQuery.closed]: gql` query allocations($indexer: String!) { - allocations(where: { indexer: $indexer, status: Closed }, first: 1000) { + allocations( + where: { indexer: $indexer, status: Closed } + first: $first + after: $after + ) { + totalCount id subgraphDeployment { id @@ -130,7 +141,8 @@ const ALLOCATION_QUERIES = { `, [AllocationQuery.allocation]: gql` query allocations($allocation: String!) { - allocations(where: { id: $allocation }, first: 1000) { + allocations(where: { id: $allocation }, first: $first, after: $after) { + totalCount id subgraphDeployment { id @@ -203,10 +215,27 @@ async function queryAllocations( ) } - const result = await networkSubgraph.checkedQuery( - ALLOCATION_QUERIES[filterType], - filterVars, - ) + const pageSize = 1000 + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let result: QueryResult + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const resultAllocations: any[] = [] + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const filterPage = { + first: pageSize, + skip: 0, + } + do { + const pageVars = { + ...filterVars, + ...filterPage, + } + result = await networkSubgraph.checkedQuery(ALLOCATION_QUERIES[filterType], pageVars) + // merge results + resultAllocations.push(...result.data.allocations) + filterPage.skip += result.data.allocations.length - 1 + } while (result.data.allocations.length == pageSize) if (result.data.allocations.length == 0) { // TODO: Is 'Claimable' still the correct term here, after Exponential Rebates?