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 bd56107
Showing 1 changed file with 38 additions and 9 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 @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<any>
// 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?
Expand Down

0 comments on commit bd56107

Please sign in to comment.