JsonApiDotNetCore icon indicating copy to clipboard operation
JsonApiDotNetCore copied to clipboard

How to specify the relationshipname for secondary endpoints in case of Nested relationships

Open abhishrutparihar opened this issue 3 years ago • 1 comments

SUMMARY

How to specify the relationshipname for secondary endpoints in case of Nested relationships

DETAILS

How to specify the relationshipname for secondary endpoints in case of Nested relationships.

e.g. I have 4 entities Student-Department-Division-rooms and I want to get all the rooms for Students with the help of URL like Student/{id}/rooms. How can I achieve this?

Please provide solution for this scenario

STEPS TO REPRODUCE

e.g. I have 4 entities Student-Department-Division-rooms and I want to get all the rooms for Students with the help of URL like Student/{id}/rooms.

Student having Id = 1 has Departments with ids 11,12 Departments Having ids 11,12 have Divisions with Ids 21,22 Divisions Having Ids 21,22 have Rooms 31,32 When I Hit URL like Student/1/rooms Then It should return data for Rooms 31,32

abhishrutparihar avatar Sep 16 '22 07:09 abhishrutparihar

Easiest would be to define a database view that contains all the joins, then map the view to a JSON:API resource class. Another approach is like the ReportsExample. Or you could add a custom action method in a JSON:API controller to query your database directly.

If you need to expose the full relational model because you're going to update the individual tables through the API, then instead use a combination of nested includes. Something along the lines of: /students/1?include=departments.divisions.rooms. You can constrain the returned fields of intermediate relationships using sparse fieldsets, but it'll still be somewhat verbose.

If your database is small enough, you could use [EagerLoad] on all the relationships, then model Rooms on Student as a calculated relationship property based on the other relationships.

We don't have an example that exactly meets your needs, but the basic concepts are covered in documentation. The IntegrationTests folder contains working examples of these and a lot more, so please take a look and try to mix them together into a solution that meets your needs.

bkoelman avatar Sep 16 '22 08:09 bkoelman

@abhishrutparihar Does this answer your question? Do you need this issue to remain open?

bkoelman avatar Sep 28 '22 02:09 bkoelman

Thank you.

abhishrutparihar avatar Sep 28 '22 05:09 abhishrutparihar