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
When calling `fetchMore` with a query that has a `no-cache` fetch policy, `fetchMore` will now throw if an `updateQuery` function is not provided. This provides a mechanism to merge the results from the `fetchMore` call with the query's previous result.
// Slicing is necessary because the existing data is
205
205
// immutable, and frozen in development.
@@ -218,6 +218,41 @@ const cache = new InMemoryCache({
218
218
219
219
This logic handles sequential page writes the same way the single-line strategy does, but it can also tolerate repeated, overlapping, or out-of-order writes, without duplicating any list items.
220
220
221
+
### Updating the query with the fetch more result
222
+
223
+
At times the call to `fetchMore` may need to perform additional cache updates for your query. While you can use the [`cache.readQuery`](/caching/cache-interaction#readquery) and [`cache.writeQuery`](/caching/cache-interaction#writequery) functions to to do this work yourself, it can be cumbsersome to use both of these together.
224
+
225
+
As a shortcut, you can provide the `updateQuery` option to `fetchMore` to update your query using the result from the `fetchMore` call.
226
+
227
+
<Note>
228
+
229
+
`updateQuery` is not a replacement for your field policy `merge` functions. While you can use `updateQuery` without the need to define a `merge` function, `merge` functions defined for fields in the query will run using the result from `updateQuery`.
230
+
231
+
</Note>
232
+
233
+
Let's see the example above using `updateQuery` to merge results together instead of a field policy merge function:
// Slicing is necessary because the existing data is
240
+
// immutable, and frozen in development.
241
+
const updatedFeed =previousData.feed.slice(0);
242
+
for (let i =0; i<fetchMoreResult.feed.length; ++i) {
243
+
updatedFeed[offset+i] =fetchMoreResult.feed[i];
244
+
}
245
+
return { ...previousData, feed: updatedFeed };
246
+
},
247
+
})
248
+
```
249
+
250
+
<Tip>
251
+
252
+
We recommend defining field policies that contain at least a [`keyArgs`](/pagination/key-args/) value even when you use `updateQuery`. This prevents fragmenting the data unnecessarily in the cache. Setting `keyArgs` to `false` is adequate for most situations to ignore the `offset` and `limit` arguments and write the paginated data as one big array.
253
+
254
+
</Tip>
255
+
221
256
## `read` functions for paginated fields
222
257
223
258
[As shown above](#defining-a-field-policy), a `merge` function helps you combine paginated query results from your GraphQL server into a single list in your client cache. But what if you also want to configure how that locally cached list is _read_? For that, you can define a **`read` function**.
@@ -304,3 +339,38 @@ The `read` function for a paginated field can choose to _ignore_ arguments like
304
339
If you adopt this approach, you might not need to define a `read` function at all, because the cached list can be returned without any processing. That's why the [`offsetLimitPagination` helper function](./offset-based/#the-offsetlimitpagination-helper) is implemented _without_ a `read` function.
305
340
306
341
Regardless of how you configure `keyArgs`, your `read` (and `merge`) functions can always examine any arguments passed to the field using the `options.args` object. See [The `keyArgs` API](./key-args) for a deeper discussion of how to reason about dividing argument-handling responsibility between `keyArgs` and your `read` or `merge` functions.
342
+
343
+
## Using `fetchMore` with queries that set a `no-cache` fetch policy
344
+
345
+
<Note>
346
+
347
+
We recommend upgrading to version 3.11.3 or later to address bugs that exhibit unexpected behavior when using `fetchMore` with queries that set `no-cache` fetch policies. Please see pull request [#11974](https://github.com/apollographql/apollo-client/pull/11974) for more information.
348
+
349
+
</Note>
350
+
351
+
The examples shown above use field policies and `merge` functions to update the result of a paginated field. But what about queries that use a `no-cache` fetch policy? Data is not written to the cache, so field policies have no effect.
352
+
353
+
To update our query, we provide the `updateQuery` option to the `fetchMore` function.
354
+
355
+
Let's use the example above, but instead provide the `updateQuery` function to `fetchMore` to update the query.
// Slicing is necessary because the existing data is
362
+
// immutable, and frozen in development.
363
+
constupdatedFeed=previousData.feed.slice(0);
364
+
for (let i =0; i <fetchMoreResult.feed.length; ++i) {
365
+
updatedFeed[offset + i] =fetchMoreResult.feed[i];
366
+
}
367
+
return { ...previousData, feed: updatedFeed };
368
+
},
369
+
})
370
+
```
371
+
372
+
<Note>
373
+
374
+
As of Apollo Client version 3.11.3, the `updateQuery` option is required when using `fetchMore` with a `no-cache` fetch policy. This is required to correctly determine how the results should be merged since field policy `merge` functions are ignored. Calling `fetchMore` without an `updateQuery` function will throw an error.
0 commit comments