REST: How to paginate a list in a list?

My product detail page contains a list. This list entails all products. That’s a lot of products for one page.
Therefore, I set a max number of records this list can hold.
This works, but using the ?page= parameter does not seem to work for this list.
How can I navigate across pages of this list?

Hi Bas,

The pagination works on the most top-level definition.
From the image you provided, it seems to be that you have a Field on some data definition, and that field is of type List of objects (that means you nest data definitions).
You can paginate and iterate over the pages (using the ?page parameter in your query) of the data definition on which this field is on, but currently paginating this nested definition is not possible.

I would suggest to see if you can improve the filter expression for this nested field, so you can reduce the number of matching records returned.

I would also suggest you to raise a Service request so I can investigate further if it would be possible to implement pagination for these nested definitions - I can understand the necessity of it, however out of the top of my head, I am not sure if there is something optimal that can be implemented.

The difficulty comes from the fact you could have (multiple)nested list of objects on multiple levels, each of those lists with different amount of records returned, so usage of single parameter “page” is probably not a good idea, because of the possibility of different page sizes and number of records matching the defined filter.

1 Like

Hi Bas,

In my experience, pagination for sub-nodes is not a good practice. I think I understand your use case, and suggest a slightly different approach which I also applied at similar projects.

Looking at your question, it looks like you have an end point for a Product Detail Page (PDP) and want to list accessoires or alternative products, right?

In that case, your end point will be something like this:

/rest/product/123

{
 "code" : 123,
 "name" : "My product",
 "price" : 99.95,
 "number_of_related_products" : 201,
 "related_products" : [
  {  
   "code" : 101,
   "name" : "My first related product",
   "price" : 29.95
  },
  {  
   "code" : 102,
   "name" : "My second related product",
   "price" : 19.95
  }
 ],
"stock" : [
 { 
  "name": "store 1",
  "stock" : 6
 }
]
}

Now, the related products are made with the Data Definition ‘related products’ which will show the first x in your PDP.

Then, you will create a second REST Data defintion (end point), using the same Data Definition as you used in the PDP definition, with the pagination, for example to get the next two.

/rest/related_products/123?page=2&show=2

The advantage is that now you will only load the related products. If you would have used pagination in the core end point, you would fetch all data (including things like stock) with every pagination, which is heavier on your end point,

[
  {  
   "code" : 103,
   "name" : "My third related product",
   "price" : 49.95
  },
  {  
   "code" : 104,
   "name" : "My fourth related product",
   "price" : 9.95
  }
 ]

Because of this, IMHO there is no use case to apply pagination in sub-definitions, as the way of working as desribed here is actually more efficient. As you can re-use Data Definitions, you are sure you have the same definition and it only requires you to define the REST End point, and you don’t need to make a new Data Definition.

3 Likes