単独で存在するか、ツリーのような階層に編成できる複数のリソースがあるとします。便宜上、根、枝、葉と呼びました。今、葉のデータを取得したい:
GET /leaf/1
Accept: application/vnd.api+json
JSON API仕様に従って、次のようなものを返さなければなりません:
{
"data": [{
"type": "leaf",
"id": "1",
"title": "Leaf 1",
"links": {
"self": "http://example.com/leaf/1",
"branch": {
"self": "http://example.com/leaf/1/links/branch",
"related": "http://example.com/leaf/1/branch",
"linkage": { "type": "branch", "id": "5" }
},
"root": {
"self": "http://example.com/leaf/1/links/root",
"related": "http://example.com/leaf/1/root",
"linkage": { "type": "root", "id": "7" }
}
}
}],
"included": [{
"type": "branch",
"id": "5",
"title": "Branch 5",
"links": {
"self": "http://example.com/branch/5"
}
}, {
"type": "root",
"id": "7",
"title": "Root 7",
"links": {
"self": "http://example.com/root/7"
}
}]
}
応答データからは、データが階層的であるとは言えず、リーフのルートを変更するのが適切と思われます:
PATCH /leaf/1
Content-Type: application/vnd.api+json
{
"data": {
"type": "leaf",
"id": 1,
"links": {
"root": {
"linkage": { "type": "root", "id": "3"}
}
}
}
}
もちろん、このリーフはブランチに接続されてからルートに接続されるため、これは不可能です。以下のスキーマでルートを変更するには、このルートのブランチの ID が必要です。質問は次のとおりです。
- 関係の変化に追加のデータが必要になる可能性があることを API ユーザーに明確にするために、リソース表現で階層を表現する方法 (可能な場合) は?