[12.x] Allow rendering paginated data where cursor parameters are not in the response#55524
[12.x] Allow rendering paginated data where cursor parameters are not in the response#55524cosmastech wants to merge 1 commit intolaravel:12.xfrom
Conversation
|
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
|
|
||
| $this->assertInstanceOf(CursorPaginator::class, $p); | ||
| $this->assertSame([['id' => 6], ['id' => 7]], $p->items()); | ||
| $this->assertSame([['zid' => 6], ['zid' => 7]], $p->items()); |
There was a problem hiding this comment.
note: If I were to call $p->toArray() it would produce a warning.
|
The more I think about this, the less I like it. I don't like that the resulting shape of the response isn't included as part of the CursorPaginator's template. It feels like it might be cleaner to create a method to map the paginator into a new paginator, keeping the cursor values from the original. |
|
Closing if you don't like it. 😅 |
|
@cosmastech Just out of curiosity, couldn't the example use case you described be solved using either a specific "->select()" or a resource? |
In the project I am working on, we have gone full-on Laravel Data, so we do not use Resources. They are not strictly typed (which has been a pain point in previous projects). Using a selective query doesn't really help unless we are just returning the Model directly, which we have avoided as a practice. |
If you attempt to render a CursorPaginator, but do not have the keys/aliases as properties on the underlying data, a warning is raised. On our production environments, this returns a 500.
Take for instance something like this:
I do not want to expose the integer ID nor the created at timestamps in my response, but this will produce a warning. The reason the warning is raised is because
throughtransforms the items, butAbstractCursorPagination::getParametersForItem()looks to find the cursor parameters as properties on the modified data.With this PR's changes, I can now do this:
This is particularly meaningful if I want to transform my data into
ResponsableDTOs (with something Laravel Data). While Laravel-Data does offer a paginated data collection class, it's not necessarily as easy to work with as the one that comes with the framework.If I have a Responsable object like this:
Then cursor pagination will fail because there's no
idfield in the UserResponse.Other possibilites
Change the
throughmethod so it does not immediately act on the collection, but instead only does it attoArray()time. This would potentially be a breaking change for users that relied on this behavior.Additionally, the signature of
throughcould be changed to:Though this is a breaking change for anyone who has extended
CursorPaginator. We could take the pseudo-argument approach where we check for a second argument being passed in, but not change the signature.We could also create the cursor if one isn't set when the paginator is constructed. That could be done inside of setItems or directly after it. I am starting to think that this is cleaner.