Skip to content

Commit 5a7b7a2

Browse files
committed
Separate related resources in a top-level linked object.
It's important to separate the primary resource from related resources because: * It allows the client to easily identify the primary resource among related resources of the same type. This is especially important when creating a resource without prior knowledge of its server-assigned id. * It clarifies issues like pagination that are specific to the primary resource. This can be achieved by nesting related resources in a `linked` object. For instance: ```javascript { "posts": [{ "id": "1", "name": "Rails is Omakase", "links": { "author": "9", "related": ["2"] } }], "linked": { "posts": [{ "id": "2", "name": "The Parley Letter", "links": { "author": "9", "related": ["1"] } }], "people": [{ "id": "9", "name": "DHH" }] } } ``` It's easy to imagine a scenario in which resources that are members of the primary array are secondary resources for other members of the primary array (e.g. people + friends). In those scenarios, primary resources should not be duplicated as secondary resources in `linked`. In other words, a single canonical representation of a resource should continue to be returned with each response. [Closes #74]
1 parent 3c779c7 commit 5a7b7a2

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

format/index.md

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ attributes section and should not be used as attribute names.
2424
The top-level of a JSON API document **MAY** have the following keys:
2525

2626
* `meta`: meta-information about a resource, such as pagination
27-
* Other resource names (`posts`, `comments`, `people`, etc.)
27+
* `linked`: a collection of documents, grouped by type, that are related to the
28+
primary document and/or each other
2829

2930
### Reserved Attributes
3031

@@ -197,7 +198,8 @@ The top-level of a JSON API document **MAY** have the following keys:
197198

198199
* `meta`: meta-information about a resource, such as pagination
199200
* `links`: URL templates to be used for expanding resources' relationships URLs
200-
* Other resource names (`posts`, `comments`, `people`, etc.)
201+
* `linked`: a collection of documents, grouped by type, that are related to the
202+
primary document and/or each other
201203

202204
### Singular Resources
203205

@@ -397,7 +399,10 @@ Top-level URL templates allow you to specify relationships as IDs, but without r
397399

398400
To save HTTP requests, it may be convenient to send related documents along with the requested documents.
399401

400-
In this case, a bit of extra metadata for each relationship can link together the documents.
402+
Related documents **MUST** be included in a top level `"linked"` object, in which they are grouped together in arrays according to their type.
403+
404+
The type of each relationship **MAY** be specified in the `"links"` object with
405+
the `"type"` key. This facilitates lookups of related documents by the client.
401406

402407
```javascript
403408
{
@@ -431,35 +436,42 @@ In this case, a bit of extra metadata for each relationship can link together th
431436
"comments": [ "6" ]
432437
}
433438
}],
434-
"author": [{
435-
"id": "9",
436-
"name": "@d2h"
437-
}],
438-
"comments": [{
439-
"id": "1",
440-
"body": "Mmmmmakase"
441-
}, {
442-
"id": "2",
443-
"body": "I prefer unagi"
444-
}, {
445-
"id": "3",
446-
"body": "What's Omakase?"
447-
}, {
448-
"id": "4",
449-
"body": "Parley is a discussion, especially one between enemies"
450-
}, {
451-
"id": "5",
452-
"body": "The parsley letter"
453-
}, {
454-
"id": "6",
455-
"body": "Dependency Injection is Not a Vice"
456-
}]
439+
"linked": {
440+
"people": [{
441+
"id": "9",
442+
"name": "@d2h"
443+
}],
444+
"comments": [{
445+
"id": "1",
446+
"body": "Mmmmmakase"
447+
}, {
448+
"id": "2",
449+
"body": "I prefer unagi"
450+
}, {
451+
"id": "3",
452+
"body": "What's Omakase?"
453+
}, {
454+
"id": "4",
455+
"body": "Parley is a discussion, especially one between enemies"
456+
}, {
457+
"id": "5",
458+
"body": "The parsley letter"
459+
}, {
460+
"id": "6",
461+
"body": "Dependency Injection is Not a Vice"
462+
}]
463+
}
457464
}
458465
```
459466

460-
The benefit of this approach is that when the same document is referenced multiple times (in this example, the author of the three posts), it only needs to be presented a single time in the document.
467+
This approach ensures that a single canonical representation of each document
468+
is returned with each response, even when the same document is referenced
469+
multiple times (in this example, the author of the three posts). Along these
470+
lines, if a primary document is linked to another primary or related document,
471+
it should not be duplicated within the `"linked"` object.
461472

462-
By always combining documents in this way, a client can consistently extract and wire up references.
473+
By always combining documents in this way, a client can consistently extract and
474+
wire up references.
463475

464476
JSON API documents **MAY** specify the URL for a document in a compound
465477
response by specifying a `"href"` key:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy