0

I have 2 resources which each have a GET, and I would like to associate one to the other.

So lets say we have a list of users and a list of schools

GET /api/users /api/users/5

GET /api/schools /api/schools/8

each school can have a list of users. Also, each user can have a list of schools. What is the best endpoint design to do an association?

eg: PUT /api/users/5/schools (send 8 in body JSON obj)

or PATCH /api/schools/5 (send user obj with just user list. Missing users in list will not be deleted)

Appreciate your help. thanks

1 Answers1

0

Looks like it's a many-to-many relationship between your user resource and school resource and in a RESTful interface, you can return results/documents that describe the relationships between these resources by representing those relationships as links.

Therefore a user can have a list of schools and you can represent that as /api/users/5/schools and that can be a list of links to school as in links to school/{id}.

And vice versa if a School has multiple users the /api/schools/8/users would can send back a list of links where each link points to a user/{id}

Technically that is just okay, but beware managing this would be a trouble because:

When you do modifying operations like PUT or PATCH, specially in case of PATCH, if you're using json as your payload format go for JsonPatch. Anyway, in case of these, if you update one like /api/users/5/schools, the cache of the schools/{id}/users has to be invalidated, which in many cases might not be made sure.

The easiest way to go would be to go for a membership resource where you list out the membership between a user and a schoool, its easily cache-able and you can scale it easily too. :)

For more you can have a look here

Community
  • 1
  • 1
Swagata Prateek
  • 1,076
  • 7
  • 15