You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CASE 1 - I can eager load the tasks data along with user user: { id: 1, name: 'User 1' tasks: [{ id: 1, name: 'Task 1' }] }
and ask the user serialiser.js to deserialise the tasks records
CASE 2 - I can lazy load the tasks data by sending the task id's in user response user: { id: 1, name: 'User 1' tasks: [1] }
Change async: true in task.js model and this will make N+1 api calls for the tasks id's mentioned
CASE 3 - Load hasMany relationships through pagination
When ever model.tasks is called in a template or route, i expect findHasMany to be triggered where i will set the url to /users/1/tasks?page=1 and fetch the tasks for that particular page.
@vishwasraj-thyagarajfindHasMany is only invoked if the link is already present. This is called out in the docs here and has been the subject of long-standing discussion here. It was hoped there would be an RFC presented that cleaned this up but it fell through here.
Sample App to reproduce the issue
https://ember-twiddle.com/2f467f447a012edfdfb5600bccfe41c9
Description of the issue
Say a user hasMany tasks
CASE 1 - I can eager load the tasks data along with user
user: { id: 1, name: 'User 1' tasks: [{ id: 1, name: 'Task 1' }] }
and ask the user
serialiser.js
to deserialise the tasks recordsCASE 2 - I can lazy load the tasks data by sending the task id's in user response
user: { id: 1, name: 'User 1' tasks: [1] }
Change
async: true
intask.js
model and this will makeN+1
api calls for the tasks id's mentionedCASE 3 - Load hasMany relationships through pagination
When ever
model.tasks
is called in a template or route, i expectfindHasMany
to be triggered where i will set the url to/users/1/tasks?page=1
and fetch the tasks for that particular page.{ tasks: [], meta: { count: 100, page: 1, perPage: 10 } }
I found a similar example on one of the blog post, which was suggesting this solution. But the solution doesn't seem to work
https://emberigniter.com/adding-query-parameters-hasmany-relationship
Debugging the addon
I found that, even after i extend the rest adapter the ember data code flows through jsonapi code
https://github.com/emberjs/data/blob/master/packages/store/addon/-private/system/core-store.ts#L1960
Line number 1960 checks for valid links
(resource.links.related)
which is not available for the model as it doesn't follow JSON API spec.I overrode
shouldFindViaLink
totrue
on line 1967 to check if it triggers, to my surprise it triggered thefindHasMany
method of the user adapterhttps://github.com/emberjs/data/blob/master/packages/store/addon/-private/system/core-store.ts#L1967
Solution ?
So how do i achieve CASE 3, Am i missing something or is this a bug?
This addon also doesn't work https://github.com/mdehoog/ember-data-has-many-query
Versions
3.18.0
The text was updated successfully, but these errors were encountered: