Coalescing Find Requests

I just started using async: true on some of our associations and saw some great performance improvements, one thing that I noticed however, is that ember will do a separate request for each associated object, so if I’d get a Post with some comments id see this:

GET /posts/1
# => { ... comment_ids: [1,2]}

GET /comments/1
GET /comments/2

I noticed that I can use coalesceFindRequests: true to get:

GET /posts/1
# => { ... comment_ids: [1,2]}

GET /comments?ids[]=1&ids[]=2

However, my backend already knows about these associations. It’s now also becoming a bit more difficult to make sure users can’t request any comment from this endpoint. What I would really like is if I would be able to do something like this:

GET /posts/1
# => { ... comment_ids: [1,2]}

GET /posts/1/comments

Do you think this makes sense?

Is anyone doing this, if so, could you point me in the right direction?

Okay, I think I figured it out (should have done a bit more research before posting here)!

What you can do, is in your post instead of providing the id’s, provide a link to where the comments can be found:

GET /posts/1
# => { ... links: { comments: 'posts/1/comments` } }
1 Like

I didn’t know that was possible (providing links). Thanks for sharing!

Yeah, me neither but I’ve been reading a lot about hypermedia recently and I’m pretty excited by this. It also makes caching our json responses way easier since we don’t need to embed any ids anymore

2 Likes