Reputation: 11659
I am reading this before building an API endpoints. I read this quote about compound documents:
To reduce the number of HTTP requests, servers MAY allow responses that include related resources along with the requested primary resources. Such responses are called "compound documents".
Here is a sample JSON response using the JSON API specification:
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"links": {
"self": "http://example.com/articles/1"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
},
"comments": {
"links": {
"self": "http://example.com/articles/1/relationships/comments",
"related": "http://example.com/articles/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
}
}],
"included": [{
"type": "people",
"id": "9",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
"twitter": "dgeb"
},
"links": {
"self": "http://example.com/people/9"
}
}, {
"type": "comments",
"id": "5",
"attributes": {
"body": "First!"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "2" }
}
},
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"attributes": {
"body": "I like XML better"
},
"relationships": {
"author": {
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/comments/12"
}
}]
}
So from what I can see, the relationships sections give basic/sparse information about the associations between the articles table and other tables. It looks like an article belongs_to an author and has_many comments.
What will the links be used for? Will the API have to use the link in order to receive more detailed JSON about the relationship? Doesn't this require an additional API call? Is this efficient?
The "included" section seems like it contains more detailed information about the relationships/associations?
Are both "included" and "relationships" necessary? What's the intuition behind needing both of these sections?
Upvotes: 2
Views: 3026
Reputation: 560
The idea is that a relationship
in a resource
simply gives linkage data (that is basic data to uniquely identify the related resource
– these data are the id
and the type
), in order to keep it to a minimum.
On the other hand, the included
section is here in case you want to send along detailed information about some related resources
(for instance to minimise the number of HTTP requests). Note that the included
section is expected to contain only resources
that are related to either a primary resource
(i.e. within the data
section), or an included
resource (this constraint is called full linkage
in the spec).
To put it simply, the relationships
section of a resource
tell you which resources
are related to a given resource
, and the included
section tells you what those resources
are.
As far as links are concerned, they may come in handy when you have a has_many
relationship, for which the linkage data itself might contain several thousands of id
/type
records, thus making your response document quite big. In case those are not necessarily needed by your client when they request the base resource
, you might decide to make them available through a link
.
Upvotes: 6