rest-bundle icon indicating copy to clipboard operation
rest-bundle copied to clipboard

Example for Pagination

Open HellPat opened this issue 3 years ago • 7 comments

A very (very) common task for a list-Endpoint (an endpoint that returns a collection) is pagination.

While it's possible and recommended (by me) to use Link-Headers for pagination (and then smth like https://www.npmjs.com/package/parse-link-header) it is very common to add meta information to the request-body itself.

Example stolen from stackoverflow https://stackoverflow.com/questions/12168624/pagination-response-payload-from-a-restful-api ```json { "_metadata": { "page": 5, "per_page": 20, "page_count": 20, "total_count": 521, "Links": [ {"self": "/products?page=5&per_page=20"}, {"first": "/products?page=0&per_page=20"}, {"previous": "/products?page=4&per_page=20"}, {"next": "/products?page=6&per_page=20"}, {"last": "/products?page=26&per_page=20"}, ] }, "records": [ { "id": 1, "name": "Widget #1", "uri": "/products/1" }, { "id": 2, "name": "Widget #2", "uri": "/products/2" }, { "id": 3, "name": "Widget #3", "uri": "/products/3" } ] } ```

I think it's at least worth an example to do so with symfony/serializer since you'll have to create a PaginatedResponse or something which is serializeable by symfony/serializer.

HellPat avatar Mar 13 '21 21:03 HellPat

I'm not sure if the bundle does already anything for pagination except that you can get an object populated from the query_string (instead of request body).

So it would be a question, what the bundle can do to make implementing pagination as easy as possible without setting any "self-made" convention.

Do you know, if there is an HTTP-standard for handling pagination? Because if, i would love to think of a solution. If not, I would maybe leave this up to the user.

If you have an idea how to implement this already with some easy steps, feel free to send a PR to https://github.com/violines/rest-bundle-showcase

You can even change some functionality there if you want. The whole purpose is that the features are shown, so the functionality is absolutely secondary.

simon-schubert avatar Mar 14 '21 14:03 simon-schubert

I'm not sure if the bundle does already anything for pagination

It doesn't, but it's possible by creating an (serializable) object, which holds the information and the collection for the current page.

I think there should be at least an example, since close to every API I know has some sort of pagination somewhere.

Do you know, if there is an HTTP-standard for handling pagination? Because if, i would love to think of a solution. There isn't (as far as I know), but one can use the Link Header for it: https://tools.ietf.org/html/rfc5988#page-6

Pagination in general is opinionated, and could depend on a QueryBuilder or something - so I don't think it needs to be in you bundle.

But an example in the Readme could help people getting started :-) (I could help with providing possible solutions. but maybe you have already something in place in your use cases?)

HellPat avatar Mar 14 '21 20:03 HellPat

the read from persistance layer will definitely be outside of the scope.

I think I should first implement a flexible solution for https://github.com/violines/rest-bundle-showcase and see if it makes sense to add it to the docs. I generally like the idea, because i definitely agree that this is a very common use case.

simon-schubert avatar Mar 15 '21 14:03 simon-schubert

I'll add an example to the showcase repo when I have some time.

HellPat avatar Mar 15 '21 18:03 HellPat

Hey @HellPat , i started working on some parts that will play into that (the internals for filtering, paginate, on db level). So if you plan to do anything here, maybe drop a quick message to avoid conflicting/duplicate work ;) best, Simon

simon-schubert avatar Apr 14 '21 19:04 simon-schubert

Hey Simon, I'm not it at the moment, so there will be no conflicting work :-)

HellPat avatar Apr 17 '21 11:04 HellPat

Hey, maybe some inspiration: https://tools.ietf.org/html/rfc5988

5.5.  Examples

 For example:

 Link: <http://example.com/TheBook/chapter2>; rel="previous";
       title="previous chapter"

 indicates that "chapter2" is previous to this resource in a logical
 navigation path.

 Similarly,

 Link: </>; rel="http://example.net/foo"

 indicates that the root resource ("/") is related to this resource
 with the extension relation type "http://example.net/foo".

HellPat avatar May 06 '21 20:05 HellPat