When I’m facing situation like that I’m always starting to think that it would be better to split everything to relationships because it will be easier to extend app functionality later, it’s cheaper to find related resources by IDs than by string values, but on the other side I’m starting to think that I’m making very complicated API which will be hard to work with.
For example with wage another one approach was to split complex attribute to many relationships.
GET /jobs/1?include=wage.currency,wage.system,wage.unit
{
"data": {
"type": "job",
"id": "1",
"relationships": {
"wage": {
"data": {
"type": "wage",
"id": "2"
}
}
}
},
"included": [
{
"type": "wage",
"id": "2",
"attributes": {
"rate": "300"
},
"relationships": {
"currency": {
"data": {
"type": "currency",
"id": "31"
},
"system": {
"data": {
"type": "wage-system",
"id": "4"
}
},
"unit": {
"data": {
"type": "wage-unit",
"id": "5"
}
}
}
}
},
{
"type": "currency",
"id": "31",
"attributes": {
"code": "USD"
}
},
{
"type": "wage-system",
"id": "4",
"attributes": {
"name": "time-based"
}
},
{
"type": "wage-unit",
"id": "5",
"attributes": {
"name": "hourly"
},
"relationships": {
"system": {
"data": {
"type": "wage-system",
"id": "4"
}
}
}
}
]
}
But wouldn’t it be harder for developers to work with it?
With complex attribute it will look like this:
GET /jobs/1
{
"data": {
"type": "job",
"id": "1",
"attributes": {
"wage": {
"system": "time-based",
"unit": "hourly",
"currency_code": "USD",
"rate": 300
}
}
}
}
What questions should developer to ask to clearly understand is it complex attribute or relationships?
Sorry for that off-topic question, but it’s rasing every time I’m trying to find the right word not so common as type: “Maybe it’s better to create one more relationship with it’s own type instead of introducing complex attribute?”.