wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
266 stars 109 forks source link

Relationship updates in the same request #105

Open invliD opened 8 years ago

invliD commented 8 years ago

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.

wvteijlingen commented 8 years ago

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.

invliD commented 8 years ago

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.

wvteijlingen commented 8 years ago

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!

invliD commented 8 years ago

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

urbany commented 7 years ago

@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?