0

Let's suppose that I have a model named Collection. I can create a collection, this collection have two important fields: shared_with_company, shared_list. currently I have an endpoint: /collections and this endpoint returns something like it:

    {
       shared_with_company:{collections: ..., count: 5}
       shared_list:{collections: ..., count: 12}
       my_collections:{collections: ..., count: 20}
    }

but, in the frontend the user want to paginate just my_collections or shared_list or shared_with_company. Should I create a specifics endpoints to each type of collections? Remember, everytime the user load the collections page will show 12 (max per page) collections of each (my_collections, shared etc.), and then he will be able to paginate it. I don't know if this is the best way to do it, I think a lot of users send 3 requests everytime the page is loaded.

Another approach: use an endpoint to load the initial page, and this endpoint will make one request to the first page and the paginations will be made with different endpoints.

I really don't know if there is a better approach or something like that.

I don't know if this can help, but I currently using Django + Django Rest Framework.

3
  • 4
    I'd say you're far better off having a standard pagination approach for all entities with their own endpoints. Mixing different entities in a single endpoint, each with their own pagination, sounds like a complicated mess IMO.
    – Dan Wilson
    Commented Jan 25, 2022 at 20:47
  • Doesn't Django have pagination facilities built in? I thought it did but my memory could be rusty...... Commented Jan 25, 2022 at 21:21
  • Something to keep in mind is that if your sub-entities are going to be relatively short, it might be worth just downloading all the sub-objects and doing the pagination completely in the UI for those sub-entities. That doesn't apply when the list becomes several hundred items long though. Commented Jan 26, 2022 at 20:35

1 Answer 1

2

A REST API is supposed to be stateless, which means that the response given to a request cannot depend on (server-side) history of which requests I made earlier. For paging this means that the user must specify which page of a collection they want to receive.

With that in mind, it is easiest for everyone to have separate endpoints /collections/shared_with_company, /collections/shared_list and /collections/my_collections with each their independent paging.

In addition, you can offer a convenience endpoint (/collections) that returns the first page of each of the lists. That could be used by users to reduce the number of requests that need to be made initially.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.