Reputation: 127
I am not sure if this was answered before, but I don't know how to search for this type of thing so I'm gonna ask it here. For example, if I have an entity called User that looks something like this:
class User
{
int id;
string name;
int roleId;
}
and an entity called Role
class Role
{
int id;
string name;
string[] permissions;
}
A user contains an id of a role. If I request a user through a rest api, should the user be returned with role's id like so:
{
"id": 1,
"name": "user"
"roleId": 1
}
and then If I need to get that role I should send another api call to fetch that particular role. Or should I map the role to the user and return it like this:
{
"id": 1,
"name": "user"
"role": {
"id": 1,
"permissions": [
"something here"
]
}
}
In this case I don't need to send another request, but I might not need the role.
Upvotes: 1
Views: 751
Reputation: 31606
Depends on the audience, such as internal consumption, yes ids are fine and allow them to build up multiple queries as needed.
Ultimately if you want to tailor to your audience and not provide every permutation of data as rest endpoints, look into GraphQL where the consumer can tailor the response to their needs.
Upvotes: 1