MCPcopy Create free account
hub / github.com/dcgtc/dgrants / recursiveGraphFetch

Function recursiveGraphFetch

app/src/utils/utils.ts:451–490  ·  view source on GitHub ↗
(
  url: string,
  key: string,
  query: (filter: string) => string,
  fromBlock: number,
  additionalFilter = '',
  before: any[] = [] // eslint-disable-line @typescript-eslint/no-explicit-any
)

Source from the content-addressed store, hash-verified

449 * @param array before the current array of objects
450 */
451export const recursiveGraphFetch = async (
452 url: string,
453 key: string,
454 query: (filter: string) => string,
455 fromBlock: number,
456 additionalFilter = '',
457 before: any[] = [] // eslint-disable-line @typescript-eslint/no-explicit-any
458): // eslint-disable-next-line @typescript-eslint/no-explicit-any
459Promise<any[]> => {
460 // retrieve the last ID we collected to use as the starting point for this query
461 const fromId = before.length ? before[before.length - 1].id : false;
462
463 // fetch this 'page' of results - please note that the query MUST return an ID
464 const res = await fetch(url, {
465 method: 'POST',
466 headers: { 'Content-Type': 'application/json' },
467 body: JSON.stringify({
468 query: query(`
469 first: 1000,
470 where: {
471 ${fromId ? `id_gt: "${fromId}",` : ''}
472 ${fromBlock ? `lastUpdatedBlockNumber_gte: "${fromBlock}",` : ''}
473 ${additionalFilter}
474 }
475 `),
476 }),
477 });
478
479 // resolve the json
480 const json = await res.json();
481
482 // if there were results on this page then check the next
483 if (!json.data[key].length) {
484 // return the full result
485 return [...before];
486 } else {
487 // return the result combined with the next page
488 return await recursiveGraphFetch(url, key, query, fromBlock, additionalFilter, [...before, ...json.data[key]]);
489 }
490};

Callers 4

getAllGrantsFunction · 0.90
getContributionsFunction · 0.90
getAllGrantRoundsFunction · 0.90
getGrantRoundDonationsFunction · 0.90

Calls 1

fetchFunction · 0.85

Tested by

no test coverage detected