Levi H
Levi H

Reputation: 3586

Is it permissible to have relationships to parents of parents for JSON:API resources?

I have the following hierarchy:

venue -> concert -> booking

And within booking I have a relationship for the concert. But should the booking also have a relationship to the venue? Or is it bad practice to have relationships at that depth?

What about if there was also a seat in a booking? Would a relationship to venue still be reasonable?

Or is it better to instead do two requests to get the venue? E.g. use the booking -> concert relationship to then get the relationship between the concert and venue?

Upvotes: 0

Views: 66

Answers (1)

jelhan
jelhan

Reputation: 6338

The relationships in your JSON:API response object should match the real world. I don't see any value of having a direct relationship between booking and venue if the relationship is through concert in reality.

You don't need a direct relationship to avoid additional request. JSON:API specification supports including deeply nested relationships.

Assuming the bookings are available from a GET /bookings endpoint, a client could ask the backend to include venues doing a GET /bookings?include=concert.venue request (assuming that the relationships is named concert on booking and venue on concert). The concert will be included implicitly.

Please find more information about sideloading related data in Inclusion of Related Resources and Compound Documents chapters in JSON:API specification.

Upvotes: 1

Related Questions