Discovered thanks to an email by a user, which I'm surprised I didn't discover the mistake sooner.
Note: Examples below use post
as the parent resource and comment
as the relationship resource
GET
Related spec: https://jsonapi.org/format/#fetching-relationships
GET has a different error compared to POST/PATCH/DELETE as it handles self relationship requests (get id/type/attributes/relationships) post/1/comment
, but not related relationship requests (get id/type) as its implementation only uses the first 3 strings separated by /
, making the request URI correct to post/1
but the type is relationships
instead of comment
.
Potential refactoring for this method would be to store the 4th item and check if the 4th item exists and the 3rd item is "relationships" to turn api.fetch('post/1/relationships/comment')
into GET https://example.api/posts/1/relationships/comments
.
POST
Related spec: https://jsonapi.org/format/#crud-updating-to-many-relationships
Currently with api.create('post/1/comment', '...')
it will attempt to create a resource with the type posts/1/comments
. Correct behaviour is per the spec:
In the following example, the comment with ID 123 is added to the list of comments for the article with ID 1:
POST /articles/1/relationships/comments HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": [
{ "type": "comments", "id": "123" }
]
}
PATCH
Related spec: https://jsonapi.org/format/#crud-updating-to-one-relationships and https://jsonapi.org/format/#crud-updating-to-many-relationships
Same as POST but can also remove relationships with data: null
on one-to-one relationships and data: []
on one-to-many relationships.
DELETE
Related spec: https://jsonapi.org/format/#crud-updating-to-many-relationships
Same as POST, but deleting instead of adding relationships ๐
In the following example, comments with IDs of 12 and 13 are removed from the list of comments for the article with ID 1:
DELETE /articles/1/relationships/comments HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": [
{ "type": "comments", "id": "12" },
{ "type": "comments", "id": "13" }
]
}