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
264 stars 109 forks source link

Create includes dynamically #131

Closed dungi closed 7 years ago

dungi commented 7 years ago

Hello,

My class looks a little big like this

User (mail: String, person: Resource) Person (name: String, Role: Resource) Role (name: String, Team: Resource) Team (name: String)

So I have 3 nested Relations. To get everything I use Query with include: /users/1?include=person,person.role,person.role.team

But thats difficult to program. And write nice code.

I tried just to load Query, with ID and find. Then take that information and load Person (but for Person, I have no Person-ID, to call Query(resourceType, resourceIDs) so I have to use the include-method to get something like this:

...
"relationships": {
      "person": {
        "links": {
          "self": "/users/1/relationships/person",
          "related": "/users/1/person"
        },
        "data": {
          "type": "people",
          "id": "2637"
        }
      }
}
...

Is there an easier way?

wvteijlingen commented 7 years ago

You can use Query.include for this. This is the way JSON:API is supposed to work.