From 458ce155c787f5ce3a2d35e788d1f0a53d4f5349 Mon Sep 17 00:00:00 2001 From: tilacog Date: Mon, 4 Sep 2023 18:55:02 -0300 Subject: [PATCH] common: update EpochSubgraph to always query the block number --- packages/indexer-common/src/epoch-subgraph.ts | 34 +++++++++++++++---- .../__tests__/helpers.test.ts | 3 +- .../src/indexer-management/monitor.ts | 5 --- packages/indexer-common/src/network.ts | 3 +- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/packages/indexer-common/src/epoch-subgraph.ts b/packages/indexer-common/src/epoch-subgraph.ts index 0f4339888..ee0af7e5c 100644 --- a/packages/indexer-common/src/epoch-subgraph.ts +++ b/packages/indexer-common/src/epoch-subgraph.ts @@ -2,12 +2,26 @@ import axios, { AxiosInstance, AxiosResponse } from 'axios' import { DocumentNode, print } from 'graphql' import { CombinedError } from '@urql/core' import { QueryResult } from './network-subgraph' +import gql from 'graphql-tag' +import { mergeSelectionSets } from './utils' +import { Logger } from '@graphprotocol/common-ts' + +const blockNumberQuery = gql` + { + _meta { + block { + number + } + } + } +` export class EpochSubgraph { - private constructor(private endpointClient: AxiosInstance) {} + endpointClient: AxiosInstance + logger: Logger - public static async create(endpoint: string): Promise { - const endpointClient = axios.create({ + constructor(endpoint: string, logger: Logger) { + this.endpointClient = axios.create({ baseURL: endpoint, headers: { 'content-type': 'application/json' }, @@ -17,8 +31,7 @@ export class EpochSubgraph { // Don't transform responses transformResponse: (data) => data, }) - // Create the Epoch subgraph instance - return new EpochSubgraph(endpointClient) + this.logger = logger } // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -27,11 +40,20 @@ export class EpochSubgraph { // eslint-disable-next-line @typescript-eslint/no-explicit-any variables?: Record, ): Promise> { + // Include block number in all queries + let updatedQuery + try { + updatedQuery = mergeSelectionSets(query, blockNumberQuery) + } catch (e) { + updatedQuery = query + } + const response = await this.endpointClient.post('', { - query: print(query), + query: print(updatedQuery), variables, }) const data = JSON.parse(response.data) + this.logger.trace('Epoch Subgraph query', { data }) if (data.errors) { return { error: new CombinedError({ graphQLErrors: data.errors }) } } diff --git a/packages/indexer-common/src/indexer-management/__tests__/helpers.test.ts b/packages/indexer-common/src/indexer-management/__tests__/helpers.test.ts index 7ab97e4c0..e8faedb6b 100644 --- a/packages/indexer-common/src/indexer-management/__tests__/helpers.test.ts +++ b/packages/indexer-common/src/indexer-management/__tests__/helpers.test.ts @@ -78,8 +78,9 @@ const setupMonitor = async () => { 'https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-goerli', deployment: undefined, }) - epochSubgraph = await EpochSubgraph.create( + epochSubgraph = new EpochSubgraph( 'https://api.thegraph.com/subgraphs/name/graphprotocol/goerli-epoch-block-oracle', + logger, ) graphNode = new GraphNode( logger, diff --git a/packages/indexer-common/src/indexer-management/monitor.ts b/packages/indexer-common/src/indexer-management/monitor.ts index 737bc3abc..255a14320 100644 --- a/packages/indexer-common/src/indexer-management/monitor.ts +++ b/packages/indexer-common/src/indexer-management/monitor.ts @@ -689,11 +689,6 @@ export class NetworkMonitor { } } } - _meta { - block { - number - } - } } `, { diff --git a/packages/indexer-common/src/network.ts b/packages/indexer-common/src/network.ts index c363a035e..13d33e033 100644 --- a/packages/indexer-common/src/network.ts +++ b/packages/indexer-common/src/network.ts @@ -138,13 +138,14 @@ export class Network { // * ----------------------------------------------------------------------- // * Epoch Subgraph // * ----------------------------------------------------------------------- - const epochSubgraph = await EpochSubgraph.create( + const epochSubgraph = new EpochSubgraph( /* eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- * Accept the non-null `url` property of the Epoch Subgraph, as it has * already been validated during parsing. Once indexing is supported, * initialize it in the same way as the NetworkSubgraph */ specification.subgraphs.epochSubgraph.url!, + logger.child({ component: 'EpochSubgraph' }), ) // * -----------------------------------------------------------------------