Skip to content

Commit

Permalink
common: add pagination support to allocation queries
Browse files Browse the repository at this point in the history
  • Loading branch information
dwerner committed Nov 18, 2024
1 parent b663c65 commit 45c17c9
Showing 1 changed file with 56 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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 */

Expand Down Expand Up @@ -66,8 +66,13 @@ interface AllocationInfo {

const ALLOCATION_QUERIES = {
[AllocationQuery.all]: gql`
query allocations($indexer: String!) {
allocations(where: { indexer: $indexer }, first: 1000) {
query allocations($indexer: String!, $lastId: String!) {
allocations(
where: { indexer: $indexer, id_gt: $lastId }
orderBy: id
orderDirection: asc
first: 1000
) {
id
subgraphDeployment {
id
Expand All @@ -87,8 +92,13 @@ const ALLOCATION_QUERIES = {
}
`,
[AllocationQuery.active]: gql`
query allocations($indexer: String!) {
allocations(where: { indexer: $indexer, status: Active }, first: 1000) {
query allocations($indexer: String!, $lastId: String!) {
allocations(
where: { indexer: $indexer, id_gt: $lastId, status: Active }
orderBy: id
orderDirection: asc
first: 1000
) {
id
subgraphDeployment {
id
Expand All @@ -108,8 +118,13 @@ const ALLOCATION_QUERIES = {
}
`,
[AllocationQuery.closed]: gql`
query allocations($indexer: String!) {
allocations(where: { indexer: $indexer, status: Closed }, first: 1000) {
query allocations($indexer: String!, $lastId: String!) {
allocations(
where: { indexer: $indexer, id_gt: $lastId, status: Closed }
orderBy: id
orderDirection: asc
first: 1000
) {
id
subgraphDeployment {
id
Expand All @@ -129,8 +144,13 @@ const ALLOCATION_QUERIES = {
}
`,
[AllocationQuery.allocation]: gql`
query allocations($allocation: String!) {
allocations(where: { id: $allocation }, first: 1000) {
query allocations($allocation: String!, $lastId: String!) {
allocations(
where: { id: $allocation, id_gt: $lastId }
orderBy: id
orderDirection: asc
first: 1000
) {
id
subgraphDeployment {
id
Expand Down Expand Up @@ -203,27 +223,40 @@ async function queryAllocations(
)
}

const result = await networkSubgraph.checkedQuery(
ALLOCATION_QUERIES[filterType],
filterVars,
)
let lastId = ''
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const resultAllocations: any[] = []
for (; ;) {
const pageVars = {
...filterVars,
lastId,
}
const result = await networkSubgraph.checkedQuery(ALLOCATION_QUERIES[filterType], pageVars)

if (result.error) {
logger.warning('Query failed', {
error: result.error,
})
throw result.error
}

if (result.data.allocations.length == 0) {
if (result.data.allocations.length == 0) {
break
}
// merge results
resultAllocations.push(...result.data.allocations)
lastId = result.data.allocations.slice(-1)[0].id
}

if (resultAllocations.length == 0) {
// TODO: Is 'Claimable' still the correct term here, after Exponential Rebates?
logger.info(`No 'Claimable' allocations found`)
return []
}

if (result.error) {
logger.warning('Query failed', {
error: result.error,
})
throw result.error
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return pMap(
result.data.allocations,
resultAllocations,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async (allocation: any): Promise<AllocationInfo> => {
const deadlineEpoch = allocation.createdAtEpoch + context.maxAllocationEpochs
Expand Down Expand Up @@ -493,8 +526,7 @@ export default {
if (receipt === 'paused' || receipt === 'unauthorized') {
throw indexerError(
IndexerErrorCode.IE062,
`Allocation not created. ${
receipt === 'paused' ? 'Network paused' : 'Operator not authorized'
`Allocation not created. ${receipt === 'paused' ? 'Network paused' : 'Operator not authorized'
}`,
)
}
Expand Down

0 comments on commit 45c17c9

Please sign in to comment.