data
data copied to clipboard
JSON-API adapter not grouping together relationship-gets for coalescing
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.
This sounds like a bug. @igorT can you confirm that coalesceFindRequests should coalesce async relationship requests?
@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
@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?
@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 Were you ever able to resolve this? I am running into the same issue now
It looks like in my case it is because my back end is sending links and data and ember-data prioritizes the links.
@mukk85 we can't easily group link requests, however you probably can if you implement findBelongsTo / findHasMany.
@runspired I know we cant group link requests, but can we have it so it prioritizes IDs if the data is provided?
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.