-
-
Notifications
You must be signed in to change notification settings - Fork 74
Fix pagination for JSON:API related routes #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting. Seems like maybe we actually do support pagination already? Can you confirm your tests don't already pass against this original behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look. From what I thought was happening was pagination for related routes wasn't quite working but it was for primary routes. I'll work on it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so I reverted get_related.ex and fetch_related in helpers.ex to match the version that still had load_params + put_context(:override_domain_params, load_params) and re-ran:
index_pagination_test.exs → still passed
get_related_test.exs → the two “related endpoint with pagination” cases fail: with
?page[limit]=2&page[offset]=0 the response still returns all 5 related records instead of 2 (same for the offset case).
So that path wasn’t applying pagination to the related-route load; primary/index pagination was already working.
With the changes (fetch_pagination_parameters in GetRelated, for_read + Ash.Query.page on the destination query), those tests pass again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, so the main thing I want to make sure of is that on these related endpoints that we aren't accidentally changing the behavior in some way when
pageis not supplied, like on actions where pagination is marked as required etc. As long as its not changing existing behavior than all is well 😄 (aside from fixing the usage ofpageparam)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. When page isn’t in the query, it doesn't call Ash.Query.page/2. Only Request.opts with a real page (from page[...]) triggers that. The existing related-route tests that don’t pass page are still passing, so its not changing that path just to “always page.” The only subtle difference is now for_read runs on the destination query. So for the most part it shouldn't, might for some edge cases. If you want, I can add an acceptance test or something along the sort to guarantee.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Speaking of that, I'm not sure that it should run on each call.
|> Ash.Query.for_read(request.action.name, request.arguments, read_opts)Isrequest.action.namereferring to the action on the destination resource? If its a related route the action would come from the relationship, and I IIRC that action name refers to the root read action's name? Would need to double check. But I don' thtink we need to runfor_readto make this work do we?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry this is a bit long of a reply, I want to be thorough. After playing around a bit and researching here's what I've come up with:
Is request.action.name referring to the action on the destination resource?
If its a related route the action would come from the relationship, and I IIRC that action name refers to the root read action's name?
But I don't think we need to run for_read to make this work do we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, so this is a problem. You're building a query against the destination resource, using an action name from the source resource. This is only working in the tests because the actions happen to have the same name.
As for
for_read, can we just try removing it? I'm pretty sure this will all "just work" without it.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh I see what you are saying. Yes I think you are right. Let me make that change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed for_read and it still passes everything. I tried running spark.cheat_sheets and spark.formatter again but it still isn't changing anything. Deps are up to date so I'm not sure why it isn't working.