Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested API resources #261

Closed
romanych opened this issue Jan 28, 2017 · 6 comments
Closed

Nested API resources #261

romanych opened this issue Jan 28, 2017 · 6 comments

Comments

@romanych
Copy link

romanych commented Jan 28, 2017

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.

@fzaninotto
Copy link
Member

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>.

@nik-lampe
Copy link
Contributor

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?

@fzaninotto
Copy link
Member

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/

@simonschllng
Copy link

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

@fzaninotto
Copy link
Member

@simonschllng no, that's not planned.

@fzaninotto
Copy link
Member

Note that this is possible with react-admin v4, see #8616

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants