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
@@ -280,13 +283,13 @@ With this, the cache will look like this after fetching the second page:
280
283
| user:3 | id: 3, name: Peter Parker |
281
284
| user:4 | id: 4, name: Bruce Wayne |
282
285
283
-
The `RecordMerger` shown above is simplistic: it can only append new pages to the end of the existing list.
284
-
285
-
A more sophisticated implementation could look at the contents of the incoming page and decide where to append / insert the items into the existing list.
286
+
The `RecordMerger` shown above is simplistic: it will always append new items to the end of the existing list.
287
+
In a real app, we need to look at the contents of the incoming page and decide if and where to append / insert the items.
286
288
287
-
To do that it is often necessary to have access to the arguments used to fetch a record inside the `RecordMerger`, to decide where to insert the new items.
289
+
To do that it is usually necessary to have access to the arguments that were used to fetch the existing/incoming lists (e.g. the page number), to decide what to do with the new items.
290
+
For instance if the existing list is for page 1 and the incoming one is for page 2, we should append.
288
291
289
-
Fields in records can actually have arbitrary metadata associated with them, in addition to their values. We'll use this to implement more sophisticated merging.
292
+
Fields in records can have arbitrary metadata attached to them, in addition to their value. We'll use this to implement a more capable merging strategy.
290
293
291
294
#### Metadata
292
295
@@ -307,10 +310,10 @@ This is done by passing a `MetadataGenerator` to the `ApolloStore` constructor:
@@ -325,35 +328,46 @@ class ConnectionMetadataGenerator : MetadataGenerator {
325
328
}
326
329
```
327
330
328
-
However, this cannot work yet.
329
-
Normalization will make the `edges` field value be a list of **references** to the `UserEdge` records, and not the actual edges - so the
330
-
cast in `val edges = obj["edges"] as List<Map<String, Any?>>` will fail.
331
+
However, this cannot work yet.
332
+
333
+
Normalization will make the `usersConnection` field value be a **reference** to the `UserConnection` record, and not the actual connection.
334
+
Because of this, we won't be able to access its metadata inside the `RecordMerger` implementation.
335
+
Furthermore, the `edges` field value will be a list of **references** to the `UserEdge` records which will contain the item's list index in their cache key (e.g. `usersConnection.edges.0`, `usersConnection.edges.1`) which will break the merging logic.
331
336
332
337
#### Embedded fields
333
338
334
-
To remediate this, we can use `embeddedFields` to configure the cache to skip normalization and store the actual list of edges instead of references:
339
+
To remediate this, we can configure the cache to skip normalization for certain fields. When doing so, the value will be embedded directly into the record instead of being referenced.
340
+
341
+
This is done with the `embeddedFields` argument of the `@typePolicy` directive:
335
342
336
343
```graphql
344
+
# Embed the value of the `usersConnection` field in the record
Copy file name to clipboardExpand all lines: libraries/apollo-normalized-cache-api-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/api/EmbeddedFieldsProvider.kt
* A provider for fields whose value should be embedded in their [Record], rather than being dereferenced during normalization.
10
+
*
11
+
* An [EmbeddedFieldsProvider] can be used in conjunction with [RecordMerger] and [MetadataGenerator] to access multiple fields and their metadata in a single
12
+
* [Record].
13
+
*/
8
14
@ApolloExperimental
9
15
interfaceEmbeddedFieldsProvider {
16
+
/**
17
+
* Returns the fields that should be embedded, given a [context]`.parentType`.
Copy file name to clipboardExpand all lines: libraries/apollo-normalized-cache-api-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/api/FieldNameGenerator.kt
Copy file name to clipboardExpand all lines: libraries/apollo-normalized-cache-api-incubating/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/api/MetadataGenerator.kt
0 commit comments