Skip to content

Commit 27604d4

Browse files
committed
a bit more documentation on relationship metadata options
1 parent 3cc7ea2 commit 27604d4

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

documentation/usage.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,41 @@ There are three types of `Relationships`: `MetaRelationship`, `ToOneRelationship
151151

152152
In addition to identifying resource objects by ID and type, `Relationships` can contain `Meta` or `Links` that follow the same rules as [`Meta`](#jsonapimeta) and [`Links`](#jsonapilinks) elsewhere in the JSON:API Document.
153153

154+
Metadata can be specified both in the Relationship Object and in the Resource Identifier Object. You specify the two types of metadata differently. As always, you can use `NoMetadata` to indicate you do not intend the JSON:API relationship to contain metadata.
155+
156+
```swift
157+
// No metadata in the Resource Identifer or the Relationship:
158+
// {
159+
// "data" : {
160+
// "id" : "1234",
161+
// "type": "people"
162+
// }
163+
// }
164+
let relationship1: ToOneRelationship<Person, NoIdMetadata, NoMetadata, NoLinks>
165+
166+
// No metadata in the Resource Identifier but some metadata in the Relationship:
167+
// {
168+
// "data" : {
169+
// "id" : "1234",
170+
// "type": "people"
171+
// },
172+
// "meta": { ... }
173+
// }
174+
let relationship2: ToOneRelationship<Person, NoIdMetadata, RelMetadata, NoLinks>
175+
// ^ assumes `RelMetadata` is a `Codable` struct defined elsewhere
176+
177+
// Metadata in the Resource Identifier but not the Relationship:
178+
// {
179+
// "data" : {
180+
// "id" : "1234",
181+
// "type": "people",
182+
// "meta": { ... }
183+
// }
184+
// }
185+
let relationship3: ToOneRelationship<Person, CoolMetadata, NoMetadata, NoLinks>
186+
// ^ assumes `CoolMetadata` is a `Codable` struct defined elsewhere
187+
```
188+
154189
The `MetaRelationship` is special in that it represents a Relationship Object with no `data` (it must contain at least one of `meta` or `links`). The other two relationship types are Relationship Objects with either singular resource linkages (`ToOneRelationship`) or arrays of resource linkages (`ToManyRelationship`).
155190

156191
To describe a relationship that may be omitted (i.e. the key is not even present in the JSON object), you make the entire `MetaRelationship`, `ToOneRelationship` or `ToManyRelationship` optional.

0 commit comments

Comments
 (0)