twentyhq / twenty

Building a modern alternative to Salesforce, powered by the community.
https://twenty.com
GNU Affero General Public License v3.0
14.9k stars 1.45k forks source link

Improve pagination on REST API #5798

Closed FelixMalfait closed 5 days ago

FelixMalfait commented 2 weeks ago

On the GraphQL API, when fetching many records we have:

Screenshot 2024-06-10 at 13 43 51

It would be handy to pass that to the REST API.

greptile-apps[bot] commented 2 weeks ago

Disclaimer: This comment was automatically generated with AI. It can give some useful hints as to where to look for, but sometimes it can also give very inaccurate answers so takes this with a pinch of salt :).

To improve pagination on the REST API to match the GraphQL API, follow these steps:

  1. Locate REST API Pagination Logic: Identify where the current pagination logic is implemented in the REST API. This is likely in the controller or service layer handling the REST endpoints.

  2. Update Pagination Response Structure: Modify the response structure to include pageInfo with hasNextPage, hasPreviousPage, startCursor, and endCursor.

  3. Example Code:

// Assuming a typical Express.js setup
const getPaginatedData = async (req, res) => {
  const { page, limit } = req.query;
  const data = await fetchDataFromDatabase(page, limit);
  const pageInfo = {
    hasNextPage: data.hasNextPage,
    hasPreviousPage: data.hasPreviousPage,
    startCursor: data.startCursor,
    endCursor: data.endCursor
  };
  res.json({ data: data.items, pageInfo });
};
  1. Update Fetch Logic: Ensure the database query logic supports fetching cursors and determining if there are next/previous pages.

  2. File References:

    • Check the controller files in src/controllers/.
    • Review service files in src/services/ for data fetching logic.

Implement these changes to align the REST API pagination with the GraphQL API.

References

FelixMalfait commented 5 days ago

Done!