@maark Thank you for the response. Let me give you an example, I’m considering. Consider two resources,
Load - Parent entity
Stop - A load has (to be delivered to) multiple stops (one to many). Hence, all stops are tied to a load (reference)
Assume a sort of supply chain problem wherein a load has to be delivered to multiple stops along a route. Hence, Load Resource has information like reference numbers which are used to find this load, whereas the stop Resource has address information like street, area, state, country etc. and every stop is tied to a load.
Thus from your given solutions.
When A is created, the server automatically creates the necessary Bs.
I don’t think this is possible as the stop level information has to be sent along with the load information. Hence, stops cannot be automatically created.
When creating B without providing an ID for A, return an error.
This is possible but will need two calls. First to create a Load and then to create all its stops. Is this the best way for dealing with this problem?
Allow the client to create Bs before creating an A.
I cannot allow to create stops before creating a load as then, there can be failure scenarios, like:
If creating stop(s) succeed, but creation of load failed, I cannot rollback directly as they are separate API calls.
Also, it may not be possible to cleanup these dead stops, which no reference to a load. I will need need to use the stop IDs (primary key) returned from the first API call (to create stops) and then use it delete those created stops if load creation failed. But this adds unnecessary complexity to the code.
With this in mind, can you possible me an example (if possible) wherein:
There is only API call to create a load and its stops.
A sample jsonapi create request body for the best approach you suggest.
But is creating a load with stops really impossible? A load could be marked as incomplete or unfinished until all the necessary stops are added. I don’t think multiple API calls are a problem. If some of them fail and it puts your database in a broken state, then your API design might be too close to your database’s schema.
A recurrent mistake I see is to have the JSON API design too close to the database. For example, when it’s impossible to create a new resource by itself without other resources in order to comply with the primary/foreign keys.