How do you handle relations / nested objects? #15
-
Hi, great articles, I found them very helpful! I have a question regarding how do you handle relations in a REST API. I find myself thinking about this a lot while making REST APIs and it quite often it gets me stuck.
I generally have issues planning out my API endpoints and end up overthinking / accounting for stuff I won't need / not focusing on the important endpoints (because I don't know which are going to be "important"). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I typically start with returning nested objects & relations. Usually just parent relationships (e.g. attaching a
No, generally I don't return child relationships because of N+1 queries. The size of your data can really explode too. I find that having the caller request extra data with a query param or just an additional API call works better.
It depends on the exact details of the restrictions and authorization. For simple cases like clearing individual fields on an object, I'll add a helper function in my domain package called something like For more complex cases, I usually find myself adding it into the data access layer so I can filter results by what the current user can see or do. |
Beta Was this translation helpful? Give feedback.
I typically start with returning nested objects & relations. Usually just parent relationships (e.g. attaching a
Customer
onto a returnedOrder
object). Most APIs only have one or two endpoints that are called a lot so I'll optimize those with a query param if I really need to restrict the returned data.