React-admin: Nested API resources

Created on 28 Jan 2017  路  5Comments  路  Source: marmelab/react-admin

I am exploring this beautiful tool/framework. There is scenario I need to implement and do not understand how.

/companies/
/companies/{companyId}
/users/
/users/{userId}

However it is many-to-many relationship between companies and users.

In order to get users from companies or add/remove user to company I need use following methods

GET|POST|DELETE /companies/{companyId}/users

Or we can retrieve list of companyIds to which company belong using following API request

GET /users/{userId}/companies

And now I need to implement management of user who works in companies. I would like to have a Tabbed form on edit with Users that will display lists of users belong to company GET /companies/{companyId}/users and allow to add remove from this resource.

Most helpful comment

The REST requests from admin-on-rest don't deal with nested resource. That means it's the responsibility of your REST client to turn something like:

GET_LIST companies { filter: { userId: 123 } }

into something like:

GET http://path.to.my.api/users/123/companies

As for displaying a list of related records in edit view, check out <ReferenceManyField>.

All 5 comments

The REST requests from admin-on-rest don't deal with nested resource. That means it's the responsibility of your REST client to turn something like:

GET_LIST companies { filter: { userId: 123 } }

into something like:

GET http://path.to.my.api/users/123/companies

As for displaying a list of related records in edit view, check out <ReferenceManyField>.

And what about accessing a nested resource via the URL?

What I mean is:
My API returns a single comment with the request GET /posts/{postId}/comments/{commentId}
But I can't wrap my head around it how to get this request from a resource.
But maybe this is just bad design from my API? And it should return the comment without the parent post?

You just need to map that in your restClient. You'll receive requests looking like:

GET_LIST comments {"filter":{"post_id":{postIt}, "pagination":{"page":"1","perPage":"6"},"sort":{"field":"id","order":"DESC"}}

It's up to you to map that to your REST API route

GET /posts/{postId}/comments/

Will admin-on-rest understand URLs like example.com/#/companies/{companyId}/users{userId}/show in the browsers address bar?

@simonschllng no, that's not planned.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fzaninotto picture fzaninotto  路  3Comments

kdabir picture kdabir  路  3Comments

aserrallerios picture aserrallerios  路  3Comments

phacks picture phacks  路  3Comments

kikill95 picture kikill95  路  3Comments