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

Nested resource - design question #31

Closed FreakTheMighty closed 9 years ago

FreakTheMighty commented 9 years ago

I'm wading into a a Java implementation and I'm curious what your findOne or find interface looks like for nested URLs. For example when fetch /articles/1/author, as found in this section, how do you call findOne such that you fetch the author from article 1?

wvteijlingen commented 9 years ago

If my memory serves me correctly, this is a two step process which is not automated. First you have to do a findOne for articles/1. The returned resource will have Resource (to-one) or LinkedResourceCollection (to-many) objects for it's relationships fields. If the related resources were not sideloaded, these only include the URLs and optionally IDs.

If you then want to load these related resources you can create a Query for that resource or that collection and load them using the find or findOne methods that take a query. For to-one relationships you can also use the ensure method.

let query = Query(resource: article.author)
spine.findOne(query)
// or
let query = Query(resourceCollection: article.comments)
spine.find(query)

The downside is that for to-many relationships, it will not automatically assign the loaded resources to the 'owning' resource. We could add a method for that though.

FreakTheMighty commented 9 years ago

Awesome, thanks for the information.

On Tue, Sep 1, 2015, 6:59 AM Ward van Teijlingen notifications@github.com wrote:

If my memory serves me correctly, this is a two step process which is not automated. First you have to do a findOne for articles/1. The returned resource will have Resource (to-one) or LinkedResourceCollection (to-many) objects for it's relationships fields. If the related resources were not sideloaded, these only include the URLs and optionally IDs.

If you then want to load these related resources you can create a Query for that resource or that collection and load them using the find or findOne methods that take a query. For to-one relationships you can also use the ensure method.

— Reply to this email directly or view it on GitHub https://github.com/wvteijlingen/Spine/issues/31#issuecomment-136731736.