Skip to content

Commit d3c32a9

Browse files
authored
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 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.
1 parent fa7b63f commit d3c32a9

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

src/utilities/indexer/queryFromIndexer.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,44 @@ export const QUERY_SIZE = 100;
2323
// }
2424
// `;
2525

26-
interface FetchedData {
26+
export interface FetchedData<ExpectedQueryResults> {
2727
data: Record<
2828
string,
2929
{
3030
totalCount?: number;
31-
nodes?: Array<Record<string, unknown>>;
31+
nodes?: ExpectedQueryResults[];
3232
}
3333
>;
3434
}
3535

36-
export async function queryFromIndexer(query: string) {
36+
export async function queryFromIndexer<ExpectedQueryResults>(query: string) {
3737
logger.debug(
3838
`Querying from GraphQL under ${indexer.graphqlEndpoint}, using this payload: ${query} `,
3939
);
40-
const { data } = await got
41-
.post(indexer.graphqlEndpoint, {
42-
json: {
43-
query,
44-
},
45-
})
46-
.json<FetchedData>();
40+
41+
const responsePromise = got.post(indexer.graphqlEndpoint, {
42+
json: {
43+
query,
44+
},
45+
});
46+
47+
// handle bad responses
48+
try {
49+
await responsePromise;
50+
} catch (error) {
51+
logger.error(
52+
`Error response coming from ${indexer.graphqlEndpoint}: ${JSON.stringify(error, null, 2)}`,
53+
);
54+
logger.info(`Continuing as if there where no matches to the query.`);
55+
return {
56+
totalCount: 0,
57+
matches: Array.of<ExpectedQueryResults>(),
58+
};
59+
}
60+
61+
// handle good responses
62+
const { data } =
63+
await responsePromise.json<FetchedData<ExpectedQueryResults>>();
4764

4865
const entities = Object.entries(data);
4966

@@ -83,7 +100,8 @@ export async function* matchesGenerator<ExpectedQueryResults>(
83100
return;
84101
}
85102
const query = buildQuery(0);
86-
const { totalCount, matches } = await queryFromIndexer(query);
103+
const { totalCount, matches } =
104+
await queryFromIndexer<ExpectedQueryResults>(query);
87105

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

95113
if (totalCount === matches.length) {
96114
for (const match of matches) {
97-
yield match as ExpectedQueryResults;
115+
yield match;
98116
}
99117
return;
100118
}
101119

102120
for (let offset = 0; offset < totalCount; offset += QUERY_SIZE) {
103-
const { matches } = await queryFromIndexer(buildQuery(offset));
121+
const { matches } = await queryFromIndexer<ExpectedQueryResults>(
122+
buildQuery(offset),
123+
);
104124

105125
for (const match of matches) {
106-
yield match as ExpectedQueryResults;
126+
yield match;
107127
}
108128
await sleep(QUERY_INTERVAL_MS);
109129
}

0 commit comments

Comments
 (0)