Hi, I’m new to the forum and relatively new to JSON:API and I’m struggling to understand what the root URI of an API should return. I couldn’t find an answer using search.
The page JSON:API — Recommendations (jsonapi.org) mentions a top-level reference document and states:
Resources are grouped by type at the top level of this document. Individual resources are keyed by ID within these typed collections.
I don’t understand what it means that »resources are keyed by ID« – do these collections use a data
object instead of a data
array as elsewhere in the spec?
The next paragraph goes on to say
For instance, collections in the reference document are represented as sets because members must be addressable by ID, while collections are represented as arrays in transport documents because order is significant.
Here, I don’t understand how these »sets« should be physically represented in the protocol? There is no example what such a »reference document« should look like. It sounds like it should be a »collection of typed collections«, so the API root could perhaps return something like this:
{
"data": [{
"type": "collections",
"id": "articles",
"links": {
"self": "http://example.com/articles"
}
}, {
"type": "collections",
"id": "comments",
"links": {
"self": "http://example.com/comments"
}
}],
"links": {
"self": "http://example.com"
}
}
But making up a type
for this kind of resource seems arbitrary.
Another way would be to represent the root document as having relations to all the »typed collections«:
{
"data": {
"type": "root",
"id": "root",
"relationships": {
"articles": {
"links": {
"related": "http://example.com/articles"
}
},
"comments": {
"links": {
"related": "http://example.com/comments"
}
}
}
},
"links": {
"self": "http://example.com"
}
}
But in this case I can’t work out what the "self"
link of those relationship objects should be?
And even then, I still don’t understand how resources should be »keyed by ID« in those typed collections?
Alternatively, the root URI could forward to the API’s »main collection«, http://example.com/articles
in this example.
I would really appreciate any advice on what others are returning from their API’s root URI to provide an »entrance point« for the clients.