data icon indicating copy to clipboard operation
data copied to clipboard

JSON-API adapter not grouping together relationship-gets for coalescing

Open mwpastore opened this issue 9 years ago • 8 comments

I have an API endpoint /api/foos that returns a collection of Foos. Each Foo belongsTo a (has one) Bar, and the related link comes over in the JSON-API payload as /api/bars/N where N is the ID of the Bar. When I iterate over the collection of Foos in my template and call foo.get('bar'), Ember Data dispatches many requests to the backend, one for each Bar. This is undesirable.

I have namespace set to /api and coalesceFindRequests set to true in my ApplicationAdapter. Digging into the JSON-API and REST adapters' code a bit, I would expect groupRecordsForFindMany to map this sequence of similar requests to a single request with a list of Bar IDs. Either this function isn't being called or it isn't working correctly. Or it's possible I'm not using Ember Data correctly!

Any suggestions on where to go from here? Is this an actual bug or missing feature or PEBKAC? Thanks in advance.

mwpastore avatar Oct 31 '15 19:10 mwpastore

This sounds like a bug. @igorT can you confirm that coalesceFindRequests should coalesce async relationship requests?

bmac avatar Nov 02 '15 13:11 bmac

@bmac in my project, coalesceFindRequests can coalesce async relationship requests, but in ?filter[id]=180,181,260,262,263,282,290 format, not like id[]=1&id[]=2&id[]=3

zachzhao1984 avatar Nov 03 '15 02:11 zachzhao1984

@nouh: That's just the JSON-API-compliant behavior I'm looking for! Did you have to do anything to get it working (besides setting coalesceFindRequests to true in your ApplicationAdapter)? What versions of Ember and Ember Data are you on and what does your JSON-API payload look like?

mwpastore avatar Nov 03 '15 03:11 mwpastore

@runspired suggests that this might be because these are belongsTo relationships and findBelongsTo does not invoke groupRecordsForFindMany when resolving. Thoughts on this and how I might implement that?

mwpastore avatar Nov 10 '15 23:11 mwpastore

@mwpastore Were you ever able to resolve this? I am running into the same issue now

mukk85 avatar Mar 08 '21 22:03 mukk85

It looks like in my case it is because my back end is sending links and data and ember-data prioritizes the links.

mukk85 avatar Mar 08 '21 23:03 mukk85

@mukk85 we can't easily group link requests, however you probably can if you implement findBelongsTo / findHasMany.

runspired avatar May 22 '21 06:05 runspired

@runspired I know we cant group link requests, but can we have it so it prioritizes IDs if the data is provided?

mukk85 avatar May 22 '21 14:05 mukk85

With the RequestManager RFC https://github.com/emberjs/rfcs/pull/860 coalescing and request-management become fully customizable to handle fulfillment and could coalesce on ids even when links are present.

runspired avatar Nov 17 '22 02:11 runspired