Skip to content

Commit

Permalink
feat: allow to specify the results structure for FetchedData
Browse files Browse the repository at this point in the history
  • Loading branch information
kilted-andres committed Sep 23, 2024
1 parent 9bd444c commit 86a52e4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/backend/revoker/indexer/queryAttestation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jest.mock('../../utilities/configuration', () => ({
},
}));

let postResponse: FetchedData;
let postResponse: FetchedData<QueriedAttestation>;
jest.mock('got', () => ({
post: jest.fn().mockReturnValue({
json: () => postResponse,
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('The function that queries the old attestations issued by SocialKYC fro
data: {
blocks: {
totalCount: count,
nodes: mockAttestations(count).map((b) => ({ ...b })),
nodes: mockAttestations(count),
},
},
};
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('The function that queries the old attestations issued by SocialKYC fro
data: {
blocks: {
totalCount: Math.floor(QUERY_SIZE * 3.33),
nodes: mockAttestations(QUERY_SIZE).map((b) => ({ ...b })),
nodes: mockAttestations(QUERY_SIZE),
},
},
};
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('The function that queries the old attestations issued by SocialKYC fro
data: {
blocks: {
totalCount: cousinNumbers.length,
nodes: mockedMatches.map((m) => ({ ...m })),
nodes: mockedMatches,
},
},
};
Expand Down
20 changes: 10 additions & 10 deletions src/backend/revoker/indexer/queryFromIndexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ function buildBlockQueries(filter?: string) {
`;
}
/** Expected structure of responses for queries defined above. */
interface IndexedBlock {
interface QueriedBlock {
id: string; // Block Ordinal Number, without punctuation
hash: HexString;
timeStamp: string; // ISO8601 Date String, like 2022-02-09T13:09:18.217
}

function mockBlocks(numberOfBlocks: number) {
const mockedBlocks: IndexedBlock[] = [];
const mockedBlocks: QueriedBlock[] = [];

for (let index = numberOfBlocks; index > 0; index--) {
mockedBlocks.push({
Expand All @@ -75,7 +75,7 @@ jest.mock('../../utilities/configuration', () => ({
},
}));

let postResponse: FetchedData;
let postResponse: FetchedData<QueriedBlock>;
jest.mock('got', () => ({
post: jest.fn().mockReturnValue({
json: () => postResponse,
Expand Down Expand Up @@ -198,12 +198,12 @@ describe('The wrapper function that manages big queries to the Indexer', () => {
data: {
blocks: {
totalCount: count,
nodes: mockBlocks(count).map((b) => ({ ...b })),
nodes: mockBlocks(count),
},
},
};
const buildBlockQuery = buildBlockQueries();
const aFewMatches = matchesGenerator<IndexedBlock>(buildBlockQuery);
const aFewMatches = matchesGenerator<QueriedBlock>(buildBlockQuery);

for await (const match of aFewMatches) {
expect(match).toBeDefined();
Expand All @@ -218,13 +218,13 @@ describe('The wrapper function that manages big queries to the Indexer', () => {
data: {
blocks: {
totalCount: count,
nodes: mockBlocks(QUERY_SIZE).map((b) => ({ ...b })),
nodes: mockBlocks(QUERY_SIZE),
},
},
};

const buildBlockQuery = buildBlockQueries();
const aLotOfMatches = matchesGenerator<IndexedBlock>(buildBlockQuery);
const aLotOfMatches = matchesGenerator<QueriedBlock>(buildBlockQuery);

for await (const match of aLotOfMatches) {
expect(match).toBeDefined();
Expand Down Expand Up @@ -264,12 +264,12 @@ describe('The wrapper function that manages big queries to the Indexer', () => {
data: {
blocks: {
totalCount: 37,
nodes: mockBlocks(37).map((b) => ({ ...b })),
nodes: mockBlocks(37),
},
},
};

const noMatches = matchesGenerator<IndexedBlock>((foo) =>
const noMatches = matchesGenerator<QueriedBlock>((foo) =>
foo.toString(),
);

Expand All @@ -290,7 +290,7 @@ describe('The wrapper function that manages big queries to the Indexer', () => {
},
};

const noMatches = matchesGenerator<IndexedBlock>((foo) =>
const noMatches = matchesGenerator<QueriedBlock>((foo) =>
foo.toString(),
);

Expand Down
19 changes: 11 additions & 8 deletions src/backend/revoker/indexer/queryFromIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ export const QUERY_SIZE = 100;
// }
// `;

export interface FetchedData {
export interface FetchedData<ExpectedQueryResults> {
data: Record<
string,
{
totalCount?: number;
nodes?: Array<Record<string, unknown>>;
nodes?: Array<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} `,
);
Expand All @@ -43,7 +43,7 @@ export async function queryFromIndexer(query: string) {
query,
},
})
.json<FetchedData>();
.json<FetchedData<ExpectedQueryResults>>();

const entities = Object.entries(data);

Expand Down Expand Up @@ -83,7 +83,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 +95,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 86a52e4

Please sign in to comment.