I'm working to design a REST
API to be consumed by a React
SPA.
The client-side of the SPA queries data about a relationship between two entities: Team
and Player
where Teams
have many Player
s and Player
s can only belong to one Team
.
I want to query all the Team
s and then get all the Player
s for each team (which you could imagine would be a fairly typical use-case for these resources in a SPA.)
I can see 3 main approaches:
Expand the
/teams
endpoint to have some param?expand=player
or something similar that includes a player array for each team. The data comes back nice to be consumed by the react application but now the REST API endpoint is becoming more complex and less compliant to the single-responsibility principle.Query
/teams
to get the IDs of all teams and then query each team/team/:id/players
. But this will increase the # of requests to the backend, although it will separate responsibilities nicer and make things more explicit.Query
/teams
to get the IDs of all teams and then query/players/:ids
where:ids
is the IDs of all the teams. This is also quite explicit but could result in a huge URL and isn't a nice and tidy.
What approach is generally regarded as the best? Of course all of them have trade-offs, but perhaps I am overlooking some trade-offs?
This is also quite explicit but could result in a huge URL and isn't nice and tidy.
Oh dear, looking for aesthetics in URL? Have you ever seen Google's URLs?