Spine icon indicating copy to clipboard operation
Spine copied to clipboard

Relationship updates in the same request

Open invliD opened this issue 8 years ago • 5 comments

Currently, when saving an existing resource, there is one request fired for every relationship and when finished, another request for the update of the actual resource data. Since JSON-API specs allow updating relationships in the main PATCH request (the same way relationships are currently created: in the POST request's data), I think it would make sense to support that use case (maybe even make it the default) as it potentially saves a lot of requests.

invliD avatar Sep 13 '16 16:09 invliD

If I read the spec correctly, this won't work. The problem is that when you do a PATCH request, the entire relationship is replaced. So if you'd want to add a new item to a has-many relationship, you'd have to first fetch all the current relations, add the new one, and then PATCH that. With the current system, we can add and remove links without having to know all the related items.

wvteijlingen avatar Sep 14 '16 09:09 wvteijlingen

That's true for to-many relationships, but not to-one relationships. It does not make a difference there.

Currently, the attribute PATCH request currently includes the relationships, so it overwrites any changes from the individual requests anyway.

invliD avatar Sep 14 '16 10:09 invliD

Ah yes, for to-one relationships we could make this more efficient.

Currently, the attribute PATCH request currently includes the relationships, so it overwrites any changes from the individual requests anyway.

Are you sure about this? That would be a serious bug!

wvteijlingen avatar Sep 20 '16 12:09 wvteijlingen

I think I was wrong about that. I can't reproduce it, maybe I was looking at the serialization for a POST request instead.

invliD avatar Sep 20 '16 12:09 invliD

@wvteijlingen I'm interesting in making a PR to send to-one relationships in the PATCH as the main model being patched. Do you think it's ok to assume all to-one relationshits can go in the same PATCH? Should this be an option?

urbany avatar Feb 24 '17 15:02 urbany