@apollo/client 4.2.0-alpha.6 → 4.2.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,71 @@
1
1
  # @apollo/client
2
2
 
3
+ ## 4.2.0-alpha.8
4
+
5
+ ### Patch Changes
6
+
7
+ - [#13229](https://github.com/apollographql/apollo-client/pull/13229) [`9a7f65a`](https://github.com/apollographql/apollo-client/commit/9a7f65a0059433c83307ef2d8117dac67947d791) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Fix `refetchOn` merging when `defaultOptions.watchQuery.refetchOn` is set to a non-object value (`false`, `true`, or a function) and the per-query `refetchOn` is an object. Previously the per-query object completely replaced the default so unspecified events fell back to "enabled" regardless of the default.
8
+
9
+ The `defaultOptions` value now applies to any event the per-query object does not explicitly configure:
10
+
11
+ - `false` - unspecified events stay disabled
12
+ - `true` - unspecified events refetch
13
+ - Callback function - the function is called for unspecified events to determine whether to refetch
14
+
15
+ ```ts
16
+ const client = new ApolloClient({
17
+ // ...
18
+ defaultOptions: {
19
+ watchQuery: {
20
+ refetchOn: false,
21
+ },
22
+ },
23
+ });
24
+
25
+ // Only `windowFocus` refetches. Other events stay disabled per the default.
26
+ useQuery(QUERY, { refetchOn: { windowFocus: true } });
27
+ ```
28
+
29
+ - [#13230](https://github.com/apollographql/apollo-client/pull/13230) [`b25b659`](https://github.com/apollographql/apollo-client/commit/b25b6593f5d968db505b127e7ff7f2bb2419d5ee) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Add the ability to override the default event handler on `RefetchEventManager`. The default handler runs when no per-source handler is configured for an event. Provide a custom handler via the `defaultHandler` constructor option or the `setDefaultEventHandler` instance method.
30
+
31
+ ```ts
32
+ new RefetchEventManager({
33
+ defaultHandler: ({ client, matchesRefetchOn }) => {
34
+ return client.refetchQueries({
35
+ include: "all",
36
+ onQueryUpdated: matchesRefetchOn,
37
+ });
38
+ },
39
+ });
40
+ ```
41
+
42
+ ## 4.2.0-alpha.7
43
+
44
+ ### Minor Changes
45
+
46
+ - [#13222](https://github.com/apollographql/apollo-client/pull/13222) [`b93c172`](https://github.com/apollographql/apollo-client/commit/b93c1723b4b7a9d1296ddd57035bc4fe39c8d971) Thanks [@jerelmiller](https://github.com/jerelmiller)! - Extend the `defaultOptions` type-safety work to `preloadQuery` (returned from `createQueryPreloader`). Defaults declared in `DeclareDefaultOptions.WatchQuery` now work with `preloadQuery` to ensure the `PreloadedQueryRef`'s data states are correctly set.
47
+
48
+ ```ts
49
+ // apollo.d.ts
50
+ import "@apollo/client";
51
+
52
+ declare module "@apollo/client" {
53
+ namespace ApolloClient {
54
+ namespace DeclareDefaultOptions {
55
+ interface WatchQuery {
56
+ errorPolicy: "all";
57
+ }
58
+ }
59
+ }
60
+ }
61
+ ```
62
+
63
+ ```ts
64
+ const preloadQuery = createQueryPreloader(client);
65
+ const queryRef = preloadQuery(QUERY);
66
+ // ^? PreloadedQueryRef<TData, TVariables, "complete" | "streaming" | "empty">
67
+ ```
68
+
3
69
  ## 4.2.0-alpha.6
4
70
 
5
71
  ### Patch Changes
@@ -229,22 +229,42 @@ class ApolloClient {
229
229
  * a description of store reactivity.
230
230
  */
231
231
  watchQuery(options) {
232
+ const { refetchOn } = options;
232
233
  if (this.defaultOptions.watchQuery) {
234
+ // Capture the default `refetchOn` at the time of the `watchQuery`
235
+ // call so that it is unaffected even if defaultOptions.refetchOn is
236
+ // mutated later.
233
237
  const defaultRefetchOn = this.defaultOptions.watchQuery.refetchOn;
234
- const mergedRefetchOn = (options.refetchOn &&
235
- typeof options.refetchOn === "object" &&
236
- defaultRefetchOn &&
237
- typeof defaultRefetchOn === "object") ?
238
- { ...defaultRefetchOn, ...options.refetchOn }
239
- : undefined;
238
+ let mergedRefetchOn;
239
+ if (refetchOn && typeof refetchOn === "object") {
240
+ if (typeof defaultRefetchOn === "object") {
241
+ mergedRefetchOn = { ...defaultRefetchOn, ...refetchOn };
242
+ }
243
+ else if (defaultRefetchOn != null) {
244
+ // We set the merged `refetchOn` option to a callback function in case
245
+ // the client hasn't connected to a RefetchEventManager yet, or
246
+ // sources are added to the manager after watchQuery is called. This
247
+ // ensures we can handle any registered source regardless of when it
248
+ // was registered.
249
+ mergedRefetchOn = (ctx) => {
250
+ const value = refetchOn[ctx.source] ?? defaultRefetchOn;
251
+ if (typeof value === "function") {
252
+ return value(ctx);
253
+ }
254
+ return value;
255
+ };
256
+ }
257
+ }
240
258
  options = (0, internal_1.mergeOptions)(this.defaultOptions.watchQuery, options);
241
259
  if (mergedRefetchOn) {
242
260
  options.refetchOn = mergedRefetchOn;
243
261
  }
244
262
  }
245
263
  if (environment_1.__DEV__) {
246
- const { refetchOn, query } = options;
264
+ const { query } = options;
247
265
  const { refetchEventManager } = this;
266
+ // Note: refetchOn evaluates the original refetchOn value, not the merged
267
+ // refetchOn value.
248
268
  if (refetchOn) {
249
269
  const operationName = (0, internal_1.getOperationName)(query, "(anonymous)");
250
270
  if (!refetchEventManager) {