Skip to content

Commit

Permalink
feat: continue working on indexer's absence (#752)
Browse files Browse the repository at this point in the history
* 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 50ce9a5.

* try: second failed set got.options.throwHttpErrors to false instead

* Revert "try: second failed set got.options.throwHttpErrors to false instead"

This reverts commit bbe1cbe.
  • Loading branch information
kilted-andres authored Jan 7, 2025
1 parent fa7b63f commit d3c32a9
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions src/utilities/indexer/queryFromIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,44 @@ export const QUERY_SIZE = 100;
// }
// `;

interface FetchedData {
export interface FetchedData<ExpectedQueryResults> {
data: Record<
string,
{
totalCount?: number;
nodes?: Array<Record<string, unknown>>;
nodes?: ExpectedQueryResults[];
}
>;
}

export async function queryFromIndexer(query: string) {
export async function queryFromIndexer<ExpectedQueryResults>(query: string) {
logger.debug(
`Querying from GraphQL under ${indexer.graphqlEndpoint}, using this payload: ${query} `,
);
const { data } = await got
.post(indexer.graphqlEndpoint, {
json: {
query,
},
})
.json<FetchedData>();

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<ExpectedQueryResults>(),
};
}

// handle good responses
const { data } =
await responsePromise.json<FetchedData<ExpectedQueryResults>>();

const entities = Object.entries(data);

Expand Down Expand Up @@ -83,7 +100,8 @@ export async function* matchesGenerator<ExpectedQueryResults>(
return;
}
const query = buildQuery(0);
const { totalCount, matches } = await queryFromIndexer(query);
const { totalCount, matches } =
await queryFromIndexer<ExpectedQueryResults>(query);

if (totalCount === 0) {
logger.debug(
Expand All @@ -94,16 +112,18 @@ export async function* matchesGenerator<ExpectedQueryResults>(

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<ExpectedQueryResults>(
buildQuery(offset),
);

for (const match of matches) {
yield match as ExpectedQueryResults;
yield match;
}
await sleep(QUERY_INTERVAL_MS);
}
Expand Down

0 comments on commit d3c32a9

Please sign in to comment.