I’m working on something that I want to use JSON API for, as it seems mostly like it’s the best of the various competing Hypermedia standards currently around. However, the one thing that I’m struggling with is the data about a relationship between two resources. I know that there have been various discussions about this, but they all seem to have been a year or more ago, so I’m wondering what the current consensus is on this.
Say, for example, I have a computer game. In this game, I have a resource for a Character
and one for a Skill
. I want to allow my Character
to have Skills
, where each Skill
currently has a certain level.
What seems like the most logical way to represent this is to have attributes
on the relationship that describes this - as such:
{
"links": {},
"data": {
"type": "characters",
"id": "1234",
"attributes": {}
"relationships": {
"skills": [{
"links": {},
"data": {
"type": "skills",
"id": "swords",
"attributes": {
"level": 20
}
}
}, {
"links": {},
"data": {
"type": "skills",
"id": "bows",
"attributes": {
"level": 3
}
}
}]
}
}
}
Now, from the spec this isn’t allowed. And the recommended way of doing this is to instead fabricate a new resource type - character-skills
that acts as the link between them:
{
"links": {},
"data": {
"type": "characters",
"id": "1234",
"attributes": {}
"relationships": {
"skills": [{
"links": {},
"data": {
"type": "character-skills",
"id": "1234-swords"
}
}, {
"links": {},
"data": {
"type": "character-skills",
"id": "1234-bows"
}
}]
}
},
"included": [{
"type": "character-skills",
"id": "1234-swords",
"attributes": {
"level": 20
},
"relationships": {
"skill": {
"links": {},
"data": {
"type": "skills",
"id": "swords"
}
}
}
}, {
"type": "character-skills",
"id": "1234-bows",
"attributes": {
"level": 3
},
"relationships": {
"skill": {
"links": {},
"data": {
"type": "skills",
"id": "bows"
}
}
}
}]
}
This works, but feels very clunky and verbose. It’s basically adding a whole new fabricated resource type purely to provide some data about the link.
And then, on top of that, I’ve got concerns about actually creating and updating these. I’m going to want to create the Character
resource with skills already assigned, so that means the create will need to create multiple resources in one go. I’m also going to want to possibly update several of these in one go, which means editing multiple resources in one go. Technically it’s possible that I could create the character and then create the links in separate requests, but that then becomes very request heavy. However, editing the links in several requests possibly causes problems if there are inter-dependencies.
Since the spec gives no provision for creating or updating multiple resources in a single go, what would be the recommended way of handling this?