Skip to content
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

Cannot load hasMany relationship for a model with rest adapter combo when overriding findHasMany #7710

Closed
vishwasraj-thyagaraj opened this issue Sep 22, 2021 · 1 comment

Comments

@vishwasraj-thyagaraj
Copy link

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 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.

{ 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 to true on line 1967 to check if it triggers, to my surprise it triggered the findHasMany method of the user adapter

https://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

@runspired
Copy link
Contributor

@vishwasraj-thyagaraj findHasMany 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@runspired @vishwasraj-thyagaraj and others