Running same GraphQL query multiple times and aggregating results

I'm new to Retool, so sorry if it is a dumb question.
I'd like to fetch all the GitHub star events for a given GitHub repository. I'm using the below GraphQL query. As I can only get 100 star events at a time, I need to run the same kind of query multiple times with pagination, until there are no more star events to fetch.
What is the easiest way in Retool to run the same query multiple times and aggregate the result into a single JS array object?
Thanks a lot.

Sample query (here it fetches the first 100 star events on the Posthog repository):

query { 
  repository (owner: "PostHog", name: "posthog") {
    nameWithOwner
    stargazerCount
    stargazers(first:100, after:null) {
      pageInfo {endCursor, hasNextPage}
      edges {
        starredAt
      }
    }
  }
}

Hey @Guillaume!

Not a dumb question at all :slightly_smiling_face: Our docs describe how to recursively trigger queries and how to return data accumulated data with an array of Promises but since you're using cursor-based pagination you might try using a combination of both!

This may not be the exact syntax you're looking for but hopefully it's in the right direction:

async function runQuery(
  accumulatedData = [], 
  endCursor = null,
  hasNextPage = true,
  currentPage = 1
) {
  if (!hasNextPage) {
    console.log("Finished running all queries");
    return accumulatedData;
  }
  console.log("Running query for row", currentPage);
  const pageData = await yourQuery.trigger({
    additionalScope: { endCursor },
  });
  
  return runQuery(
    [...accumulatedData, ...pageData.edges],
    pageData.pageInfo.endCursor, 
    pageData.pageInfo.hasNextPage, 
    currentPage + 1
  );
}

return runQuery();

Let me know if that works?