Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine subgraph query result shape validation #798

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
}
}
}
Loading