Skip to content

Commit

Permalink
common: add subgraphFeature query to IndexingStatus component
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Mar 28, 2023
1 parent 2e1e0fc commit 42276a8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/indexer-common/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export enum IndexerErrorCode {
IE070 = 'IE070',
IE071 = 'IE071',
IE072 = 'IE072',
IE073 = 'IE073',
}

export const INDEXER_ERROR_MESSAGES: Record<IndexerErrorCode, string> = {
Expand Down Expand Up @@ -159,6 +160,7 @@ export const INDEXER_ERROR_MESSAGES: Record<IndexerErrorCode, string> = {
IE070: 'Failed to query latest valid epoch and block hash',
IE071: 'Add Epoch subgraph support for non-protocol chains',
IE072: 'Failed to execute batch tx (contract: staking)',
IE073: 'Failed to query subgraph features from indexing statuses endpoint',
}

export type IndexerErrorCause = unknown
Expand Down
40 changes: 39 additions & 1 deletion packages/indexer-common/src/indexing-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import gql from 'graphql-tag'
import { Client, createClient } from '@urql/core'
import { Logger, SubgraphDeploymentID } from '@graphprotocol/common-ts'
import { BlockPointer, ChainIndexingStatus, IndexingStatus } from './types'
import { indexerError, IndexerErrorCode } from './errors'
import { indexerError, IndexerErrorCode, INDEXER_ERROR_MESSAGES } from './errors'
import pRetry from 'p-retry'

export interface IndexingStatusFetcherOptions {
Expand All @@ -16,6 +16,11 @@ export interface SubgraphDeploymentAssignment {
node: string
}

export interface SubgraphFeatures {
// `null` is only expected when Graph Node detects validation errors in the Subgraph Manifest.
network: string | null
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const parseGraphQLIndexingStatus = (indexingStatus: any): IndexingStatus => ({
subgraphDeployment: new SubgraphDeploymentID(indexingStatus.subgraphDeployment),
Expand Down Expand Up @@ -316,4 +321,37 @@ export class IndexingStatusResolver {
throw err
}
}

public async subgraphFeatures(
subgraphDeploymentId: SubgraphDeploymentID,
): Promise<SubgraphFeatures> {
const subgraphId = subgraphDeploymentId.ipfsHash
try {
const result = await this.statuses
.query(
gql`
query subgraphFeatures($subgraphId: String!) {
subgraphFeatures(subgraphId: $subgraphId) {
network
}
}
`,
{ subgraphId },
)
.toPromise()

if (result.error) {
throw result.error
}
if (!result.data) {
throw new Error('Subgraph Deployment Not Found')
}
return result.data.subgraphFeatures as SubgraphFeatures
} catch (error) {
const errorCode = IndexerErrorCode.IE073
const err = indexerError(errorCode, error)
this.logger.error(INDEXER_ERROR_MESSAGES[errorCode], { err, subgraphId })
throw err
}
}
}

0 comments on commit 42276a8

Please sign in to comment.