From d3c32a95d4d4a358eaa4bca6a4b8089268de6d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= <105802444+kilted-andres@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:50:17 +0100 Subject: [PATCH] feat: continue working on indexer's absence (#752) * feat: continue working on indexer's absence * fix: types of matches * fix: the type of matches but shorter * feat: use `ExpectedQueryResults` on `FetchedData` interface * try: failed set got.options.throwHttpErrors to false instead * Revert "try: failed set got.options.throwHttpErrors to false instead" This reverts commit 50ce9a52891be4805206b36323a2ec34256d8df1. * try: second failed set got.options.throwHttpErrors to false instead * Revert "try: second failed set got.options.throwHttpErrors to false instead" This reverts commit bbe1cbe06c4a21d01bcf9c23b3910ad77a377bb9. --- src/utilities/indexer/queryFromIndexer.ts | 48 ++++++++++++++++------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/utilities/indexer/queryFromIndexer.ts b/src/utilities/indexer/queryFromIndexer.ts index f1e087ae..50059741 100644 --- a/src/utilities/indexer/queryFromIndexer.ts +++ b/src/utilities/indexer/queryFromIndexer.ts @@ -23,27 +23,44 @@ export const QUERY_SIZE = 100; // } // `; -interface FetchedData { +export interface FetchedData { data: Record< string, { totalCount?: number; - nodes?: Array>; + nodes?: ExpectedQueryResults[]; } >; } -export async function queryFromIndexer(query: string) { +export async function queryFromIndexer(query: string) { logger.debug( `Querying from GraphQL under ${indexer.graphqlEndpoint}, using this payload: ${query} `, ); - const { data } = await got - .post(indexer.graphqlEndpoint, { - json: { - query, - }, - }) - .json(); + + const responsePromise = got.post(indexer.graphqlEndpoint, { + json: { + query, + }, + }); + + // handle bad responses + try { + await responsePromise; + } catch (error) { + logger.error( + `Error response coming from ${indexer.graphqlEndpoint}: ${JSON.stringify(error, null, 2)}`, + ); + logger.info(`Continuing as if there where no matches to the query.`); + return { + totalCount: 0, + matches: Array.of(), + }; + } + + // handle good responses + const { data } = + await responsePromise.json>(); const entities = Object.entries(data); @@ -83,7 +100,8 @@ export async function* matchesGenerator( return; } const query = buildQuery(0); - const { totalCount, matches } = await queryFromIndexer(query); + const { totalCount, matches } = + await queryFromIndexer(query); if (totalCount === 0) { logger.debug( @@ -94,16 +112,18 @@ export async function* matchesGenerator( if (totalCount === matches.length) { for (const match of matches) { - yield match as ExpectedQueryResults; + yield match; } return; } for (let offset = 0; offset < totalCount; offset += QUERY_SIZE) { - const { matches } = await queryFromIndexer(buildQuery(offset)); + const { matches } = await queryFromIndexer( + buildQuery(offset), + ); for (const match of matches) { - yield match as ExpectedQueryResults; + yield match; } await sleep(QUERY_INTERVAL_MS); }