octokit / graphql.js

GitHub GraphQL API client for browsers and Node
MIT License
465 stars 85 forks source link

Pagination/Next Cursor Automation #61

Closed NickLiffen closed 2 years ago

NickLiffen commented 5 years ago

Hey,

Understand that pagination in GraphQL isn't the same as pagination within REST. One thing that we had to do when using this library is to build our own helper function around how to handle retires/pagination when returning a subset of data which is large.

It would be great if this library could handle some of that an abstract that behind the scenes so we wouldn't have to build our own helper functions which lie on top of this library.

The function we built looked something like this:

const withPagination = (queryFunc, pluckCursor) => async (
  client,
  variables = {}
) => {
  const cursor = variables.cursor || null;
  return queryFunc(client, Object.assign({}, variables, { cursor })).then(
    async results => {
      const intermediate = Array.isArray(results) ? results : [results];
      const nextCursor = pluckCursor(results);
      if (!nextCursor) {
        return intermediate;
      }
      const nextResults = await withPagination(queryFunc, pluckCursor)(
        client,
        Object.assign({}, variables, { cursor: nextCursor })
      );
      return intermediate.concat(nextResults);
    }
  );
};

We would be happy to contribute back to this package, but first, want to understand your thoughts/motivate towards this feature/use case?