Skip to content

Commit

Permalink
common: harden freshness subgraph queries and fix recursion bug
Browse files Browse the repository at this point in the history
- refine subgraph query result shape validation
- account for error property named 'errors' and 'error'
- pass 'variables' value when recursing
  • Loading branch information
tilacog committed Oct 10, 2023
1 parent 580c78d commit 2e23f93
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions packages/indexer-common/src/subgraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,18 +442,26 @@ export class SubgraphFreshnessChecker {
])

// Return it early if query results contains errors
if (subgraphQueryResult.errors) {
if (subgraphQueryResult.errors || subgraphQueryResult.error) {
return subgraphQueryResult
}

const latestIndexedBlock = subgraphQueryResult?.data?._meta?.block?.number
if (!latestIndexedBlock) {
const errorMsg = `Failed to infer block number for ${this.subgraphName} query`
this.logger.error(errorMsg, { query: print(updatedQuery) })
// Check for unexpected missing block data as a precaution.
// Check for missing block metadata
const queryShapeError = this.checkMalformedQueryResult(subgraphQueryResult)
if (queryShapeError) {
const errorMsg = `Failed to infer block number for ${this.subgraphName} query: ${queryShapeError}`
this.logger.error(errorMsg, {
query: print(updatedQuery),
subgraph: this.subgraphName,
error: queryShapeError,
subgraphQueryResult,
})
throw new Error(errorMsg)
}

// At this point we have validated that this value exists and is numeric.
const latestIndexedBlock: number = subgraphQueryResult.data._meta.block.number

// Check subgraph freshness
const blockDistance = latestNetworkBlock - latestIndexedBlock
const logInfo = {
Expand All @@ -479,10 +487,38 @@ export class SubgraphFreshnessChecker {
logInfo,
)
await sleep(this.sleepDurationMillis)
return this.checkedQueryRecursive(updatedQuery, subgraph, retriesLeft - 1)
return this.checkedQueryRecursive(
updatedQuery,
subgraph,
retriesLeft - 1,
variables,
)
} else {
this.logger.trace(`${this.subgraphName} is fresh`, logInfo)
}
return subgraphQueryResult
}

// Checks if the query result has the expecte
// eslint-disable-next-line @typescript-eslint/no-explicit-any
checkMalformedQueryResult(subgraphQueryResult: any): string | undefined {
if (!subgraphQueryResult) {
return 'Subgraph query result is null or undefined'
}
if (!subgraphQueryResult.data) {
return 'Subgraph query data is null or undefined'
}
if (!subgraphQueryResult.data._meta) {
return 'Query metadata is null or undefined'
}
if (!subgraphQueryResult.data._meta.block) {
return 'Block metadata is null or undefined'
}
if (
!subgraphQueryResult.data._meta.block.number &&
typeof subgraphQueryResult.data._meta.block.number === 'number'
) {
return 'Block number is null or undefined'
}
}
}

0 comments on commit 2e23f93

Please sign in to comment.