Skip to content

Commit

Permalink
common: refine subgraph query result shape validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Oct 9, 2023
1 parent 580c78d commit cd4ae95
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions packages/indexer-common/src/subgraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,22 @@ export class SubgraphFreshnessChecker {
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 Down Expand Up @@ -485,4 +493,27 @@ export class SubgraphFreshnessChecker {
}
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 cd4ae95

Please sign in to comment.