You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -149,6 +150,33 @@ Note that I am calling an unidentified person is a "new" person. This is general
149
150
150
151
There are three types of `Relationships`: `MetaRelationship`, `ToOneRelationship` and `ToManyRelationship`. A `ResourceObjectDescription`'s `Relationships` type can contain any number of `Relationship` properties of any of these types. Do not store anything other than `Relationship` properties in the `Relationships` struct of a `ResourceObjectDescription`.
151
152
153
+
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`).
154
+
155
+
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.
156
+
```swift
157
+
// note the question mark at the very end of the line.
158
+
let optionalRelative: ToOneRelationship<Person, NoIdMetadata, NoMetadata, NoLinks>?
159
+
```
160
+
161
+
A `ToOneRelationship` can be marked as nullable (i.e. the value could be either `null` or a resource identifier) like this:
162
+
```swift
163
+
// note the question mark just after `Person`.
164
+
let nullableRelative: ToOneRelationship<Person?, NoIdMetadata, NoMetadata, NoLinks>
165
+
```
166
+
167
+
A `ToManyRelationship` can naturally represent the absence of related values with an empty array, so `ToManyRelationship` do not support nullability.
168
+
169
+
A `ResourceObject` that does not have relationships can be described by adding the following to a `ResourceObjectDescription`:
170
+
```swift
171
+
typealiasRelationships= NoRelationships
172
+
```
173
+
174
+
`Relationship` values boil down to `Ids` of other resource objects. To access the `Id` of a related `ResourceObject`, you can use the custom `~>` operator with the `KeyPath` of the `Relationship` from which you want the `Id`. The friends of the above `Person``ResourceObject` can be accessed as follows (type annotations for clarity):
175
+
```swift
176
+
let friendIds: [Person.Id] = person ~> \.friends
177
+
```
178
+
179
+
#### Relationship Metadata
152
180
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.
153
181
154
182
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.
@@ -186,30 +214,16 @@ let relationship3: ToOneRelationship<Person, CoolMetadata, NoMetadata, NoLinks>
186
214
// ^ assumes `CoolMetadata` is a `Codable` struct defined elsewhere
187
215
```
188
216
189
-
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`).
217
+
When you need metadata out of a to-one relationship, you can access the Relationship Object metadata with the `meta` property and the Resource Identifer metadata with the `idMeta` property. When you need metadata out of a to-many relationship, you can access the Relationship Object metadata with the `meta` property (there is only one such metadata object) and you can access the Resource Identifier metadata (of which there is one per related resource) by asking each element of the `idsWithMeta` property for its `meta` property.
190
218
191
-
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.
192
219
```swift
193
-
//note the question mark at the very end of the line.
A `ToOneRelationship` can be marked as nullable (i.e. the value could be either `null` or a resource identifier) like this:
198
-
```swift
199
-
// note the question mark just after `Person`.
200
-
let nullableRelative: ToOneRelationship<Person?, NoIdMetadata, NoMetadata, NoLinks>
201
-
```
202
-
203
-
A `ToManyRelationship` can naturally represent the absence of related values with an empty array, so `ToManyRelationship` do not support nullability.
204
-
205
-
A `ResourceObject` that does not have relationships can be described by adding the following to a `ResourceObjectDescription`:
206
-
```swift
207
-
typealiasRelationships= NoRelationships
208
-
```
209
-
210
-
`Relationship` values boil down to `Ids` of other resource objects. To access the `Id` of a related `ResourceObject`, you can use the custom `~>` operator with the `KeyPath` of the `Relationship` from which you want the `Id`. The friends of the above `Person``ResourceObject` can be accessed as follows (type annotations for clarity):
211
-
```swift
212
-
let friendIds: [Person.Id] = person ~> \.friends
224
+
// to-many
225
+
let relations = entity.relationships.friends
226
+
let idMeta = relations.idsWithMeta.map { $0.meta }
0 commit comments