4 ms·
Never nest data (not `post: { author: { … } }` but reference only `post: {author_id: …}`) I'm creating a huge API on my dayjob and we have nested A LOT. So man
by emilsedgh 10y ago
Never nest data (not `post: { author: { … } }` but reference only `post: {author_id: …}`)
I'm creating a huge API on my dayjob and we have nested A LOT. So many levels of nesting, the responses have become too big.
Yet, the clients refuse to call additional endpoints and always insist on this. And it _does_ make sense for them to make 1 call and retrieve all information they need.
How does everyone handle this on REST?
(I know GraphQL is a solution.I'm wondering how people use REST API's)
- yeukhon 10y agoI am not familiar with GraphQL and my APIs are serving only a handful of people. I do have one suggestion: write a really really good client library for your user. This eventually can become the basis of your test harness. In fact, every time I write my tests, I end up writing a client library...
- biot 10y agoIf you must, serve up the authors as an array that is a sibling of the posts array. So if you have 100 posts written by only 3 authors, you have: { posts: [ /* 100 records */ ], authors: [ /* 3 records */ ] } Each post references the author by ID only and all required data is sent in one API call.
- _asummers 10y agoEven better: make authors be a map of ID to author so you can get fast lookup by ID when you need it.