I have a resource type with an attribute that is very expensive to calculate (incurs a separate database query). Thus, I don’t want this attribute to be available when requesting a collection, because it would incur an extra query for each resource (n+1 query problem). However, I want clients to be able to request any one of the resources individually to get the expensive attribute.
This would manifest as something like:
GET /api/users ->
{
"data": [
{
"type": "users",
"id": "1",
"attributes": {
"name": "Bob"
}
},
{
"type": "users",
"id": "2",
"attributes": {
"name": "George"
}
},
]
}
GET /api/users/1 ->
{
"data": {
"type": "users",
"id": "1",
"attributes": {
"name": "Bob",
"expensiveAttribute": "value"
}
}
}
Is there any theoretical issue with having the attribute available when resources are retrieved individually but not when they are listed within a collection?
Is there an alternative approach that would be preferable? I have thought about implementing a new resource type which represents the additional data, with the ID corresponding to the original resource ID, but this feels kind of hacky to me.