@apollo/client 4.2.0-alpha.2 → 4.2.0-alpha.4

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.
Files changed (147) hide show
  1. package/CHANGELOG.md +240 -0
  2. package/__cjs/cache/core/cache.cjs +1 -1
  3. package/__cjs/cache/inmemory/entityStore.cjs +3 -3
  4. package/__cjs/cache/inmemory/entityStore.cjs.map +1 -1
  5. package/__cjs/cache/inmemory/key-extractor.cjs +1 -1
  6. package/__cjs/cache/inmemory/policies.cjs +4 -4
  7. package/__cjs/cache/inmemory/readFromStore.cjs +2 -2
  8. package/__cjs/cache/inmemory/writeToStore.cjs +4 -4
  9. package/__cjs/core/ApolloClient.cjs +52 -20
  10. package/__cjs/core/ApolloClient.cjs.map +1 -1
  11. package/__cjs/core/ApolloClient.d.cts +159 -22
  12. package/__cjs/core/ObservableQuery.cjs +7 -7
  13. package/__cjs/core/ObservableQuery.cjs.map +1 -1
  14. package/__cjs/core/ObservableQuery.d.cts +20 -1
  15. package/__cjs/core/QueryManager.cjs +12 -12
  16. package/__cjs/core/QueryManager.cjs.map +1 -1
  17. package/__cjs/core/RefetchEventManager.cjs +134 -0
  18. package/__cjs/core/RefetchEventManager.cjs.map +1 -0
  19. package/__cjs/core/RefetchEventManager.d.cts +130 -0
  20. package/__cjs/core/index.cjs +7 -1
  21. package/__cjs/core/index.cjs.map +1 -1
  22. package/__cjs/core/index.d.cts +4 -1
  23. package/__cjs/core/refetchSources/onlineSource.cjs +10 -0
  24. package/__cjs/core/refetchSources/onlineSource.cjs.map +1 -0
  25. package/__cjs/core/refetchSources/onlineSource.d.cts +3 -0
  26. package/__cjs/core/refetchSources/windowFocusSource.cjs +13 -0
  27. package/__cjs/core/refetchSources/windowFocusSource.cjs.map +1 -0
  28. package/__cjs/core/refetchSources/windowFocusSource.d.cts +3 -0
  29. package/__cjs/core/types.d.cts +20 -0
  30. package/__cjs/invariantErrorCodes.cjs +69 -44
  31. package/__cjs/link/ws/index.cjs +9 -1
  32. package/__cjs/link/ws/index.cjs.map +1 -1
  33. package/__cjs/link/ws/index.d.cts +1 -1
  34. package/__cjs/react/hooks/useBackgroundQuery.cjs.map +1 -1
  35. package/__cjs/react/hooks/useBackgroundQuery.d.cts +1486 -66
  36. package/__cjs/react/hooks/useLazyQuery.cjs +1 -0
  37. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  38. package/__cjs/react/hooks/useLazyQuery.d.cts +366 -40
  39. package/__cjs/react/hooks/useLoadableQuery.cjs.map +1 -1
  40. package/__cjs/react/hooks/useLoadableQuery.d.cts +512 -50
  41. package/__cjs/react/hooks/useMutation.cjs +5 -48
  42. package/__cjs/react/hooks/useMutation.cjs.map +1 -1
  43. package/__cjs/react/hooks/useMutation.d.cts +239 -130
  44. package/__cjs/react/hooks/useQuery.cjs.map +1 -1
  45. package/__cjs/react/hooks/useQuery.d.cts +590 -41
  46. package/__cjs/react/hooks/useSubscription.cjs +1 -1
  47. package/__cjs/react/hooks/useSubscription.cjs.map +1 -1
  48. package/__cjs/react/hooks/useSubscription.d.cts +2 -2
  49. package/__cjs/react/hooks/useSuspenseQuery.cjs.map +1 -1
  50. package/__cjs/react/hooks/useSuspenseQuery.d.cts +754 -46
  51. package/__cjs/react/internal/cache/QueryReference.cjs +1 -0
  52. package/__cjs/react/internal/cache/QueryReference.cjs.map +1 -1
  53. package/__cjs/react/internal/cache/QueryReference.d.cts +1 -1
  54. package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
  55. package/__cjs/react/query-preloader/createQueryPreloader.d.cts +20 -1
  56. package/__cjs/react/types/types.documentation.d.cts +19 -0
  57. package/__cjs/version.cjs +1 -1
  58. package/cache/core/cache.js +1 -1
  59. package/cache/inmemory/entityStore.js +3 -3
  60. package/cache/inmemory/entityStore.js.map +1 -1
  61. package/cache/inmemory/key-extractor.js +1 -1
  62. package/cache/inmemory/policies.js +4 -4
  63. package/cache/inmemory/readFromStore.js +2 -2
  64. package/cache/inmemory/writeToStore.js +4 -4
  65. package/core/ApolloClient.d.ts +159 -22
  66. package/core/ApolloClient.js +53 -21
  67. package/core/ApolloClient.js.map +1 -1
  68. package/core/ObservableQuery.d.ts +20 -1
  69. package/core/ObservableQuery.js +7 -7
  70. package/core/ObservableQuery.js.map +1 -1
  71. package/core/QueryManager.js +12 -12
  72. package/core/QueryManager.js.map +1 -1
  73. package/core/RefetchEventManager.d.ts +130 -0
  74. package/core/RefetchEventManager.js +126 -0
  75. package/core/RefetchEventManager.js.map +1 -0
  76. package/core/index.d.ts +4 -1
  77. package/core/index.js +3 -0
  78. package/core/index.js.map +1 -1
  79. package/core/refetchSources/onlineSource.d.ts +3 -0
  80. package/core/refetchSources/onlineSource.js +6 -0
  81. package/core/refetchSources/onlineSource.js.map +1 -0
  82. package/core/refetchSources/windowFocusSource.d.ts +3 -0
  83. package/core/refetchSources/windowFocusSource.js +9 -0
  84. package/core/refetchSources/windowFocusSource.js.map +1 -0
  85. package/core/types.d.ts +20 -0
  86. package/core/types.js.map +1 -1
  87. package/invariantErrorCodes.js +69 -44
  88. package/link/ws/index.d.ts +1 -1
  89. package/link/ws/index.js +9 -1
  90. package/link/ws/index.js.map +1 -1
  91. package/package.json +3 -7
  92. package/react/hooks/useBackgroundQuery.d.ts +1486 -66
  93. package/react/hooks/useBackgroundQuery.js.map +1 -1
  94. package/react/hooks/useLazyQuery.d.ts +366 -40
  95. package/react/hooks/useLazyQuery.js +1 -0
  96. package/react/hooks/useLazyQuery.js.map +1 -1
  97. package/react/hooks/useLoadableQuery.d.ts +512 -50
  98. package/react/hooks/useLoadableQuery.js.map +1 -1
  99. package/react/hooks/useMutation.d.ts +239 -130
  100. package/react/hooks/useMutation.js +5 -48
  101. package/react/hooks/useMutation.js.map +1 -1
  102. package/react/hooks/useQuery.d.ts +590 -41
  103. package/react/hooks/useQuery.js.map +1 -1
  104. package/react/hooks/useSubscription.d.ts +2 -2
  105. package/react/hooks/useSubscription.js +1 -1
  106. package/react/hooks/useSubscription.js.map +1 -1
  107. package/react/hooks/useSuspenseQuery.d.ts +754 -46
  108. package/react/hooks/useSuspenseQuery.js.map +1 -1
  109. package/react/hooks-compiled/useBackgroundQuery.d.ts +1486 -66
  110. package/react/hooks-compiled/useBackgroundQuery.js.map +1 -1
  111. package/react/hooks-compiled/useLazyQuery.d.ts +366 -40
  112. package/react/hooks-compiled/useLazyQuery.js +1 -0
  113. package/react/hooks-compiled/useLazyQuery.js.map +1 -1
  114. package/react/hooks-compiled/useLoadableQuery.d.ts +512 -50
  115. package/react/hooks-compiled/useLoadableQuery.js.map +1 -1
  116. package/react/hooks-compiled/useMutation.d.ts +239 -130
  117. package/react/hooks-compiled/useMutation.js +4 -47
  118. package/react/hooks-compiled/useMutation.js.map +1 -1
  119. package/react/hooks-compiled/useQuery.d.ts +590 -41
  120. package/react/hooks-compiled/useQuery.js.map +1 -1
  121. package/react/hooks-compiled/useSubscription.d.ts +2 -2
  122. package/react/hooks-compiled/useSubscription.js +1 -1
  123. package/react/hooks-compiled/useSubscription.js.map +1 -1
  124. package/react/hooks-compiled/useSuspenseQuery.d.ts +754 -46
  125. package/react/hooks-compiled/useSuspenseQuery.js.map +1 -1
  126. package/react/internal/cache/QueryReference.d.ts +1 -1
  127. package/react/internal/cache/QueryReference.js +1 -0
  128. package/react/internal/cache/QueryReference.js.map +1 -1
  129. package/react/query-preloader/createQueryPreloader.d.ts +20 -1
  130. package/react/query-preloader/createQueryPreloader.js.map +1 -1
  131. package/react/types/types.documentation.d.ts +19 -0
  132. package/react/types/types.documentation.js.map +1 -1
  133. package/skills/apollo-client/SKILL.md +168 -0
  134. package/skills/apollo-client/references/caching.md +560 -0
  135. package/skills/apollo-client/references/error-handling.md +350 -0
  136. package/skills/apollo-client/references/fragments.md +804 -0
  137. package/skills/apollo-client/references/integration-client.md +336 -0
  138. package/skills/apollo-client/references/integration-nextjs.md +325 -0
  139. package/skills/apollo-client/references/integration-react-router.md +256 -0
  140. package/skills/apollo-client/references/integration-tanstack-start.md +378 -0
  141. package/skills/apollo-client/references/mutations.md +549 -0
  142. package/skills/apollo-client/references/queries.md +416 -0
  143. package/skills/apollo-client/references/state-management.md +428 -0
  144. package/skills/apollo-client/references/suspense-hooks.md +773 -0
  145. package/skills/apollo-client/references/troubleshooting.md +487 -0
  146. package/skills/apollo-client/references/typescript-codegen.md +133 -0
  147. package/version.js +1 -1
@@ -41,7 +41,7 @@ function keyFieldsFnFromSpecifier(specifier) {
41
41
  // context.readField for this extraction.
42
42
  extracted = extractKeyPath(object, schemaKeyPath, extractKey);
43
43
  }
44
- (0, invariant_1.invariant)(extracted !== void 0, 102, schemaKeyPath.join("."), object);
44
+ (0, invariant_1.invariant)(extracted !== void 0, 107, schemaKeyPath.join("."), object);
45
45
  return extracted;
46
46
  }));
47
47
  return `${context.typename}:${JSON.stringify(keyObject)}`;
@@ -229,7 +229,7 @@ class Policies {
229
229
  const rootId = "ROOT_" + which.toUpperCase();
230
230
  const old = this.rootTypenamesById[rootId];
231
231
  if (typename !== old) {
232
- (0, invariant_1.invariant)(!old || old === which, 103, which);
232
+ (0, invariant_1.invariant)(!old || old === which, 108, which);
233
233
  // First, delete any old __typename associated with this rootId from
234
234
  // rootIdsByTypename.
235
235
  if (old)
@@ -372,7 +372,7 @@ class Policies {
372
372
  if (supertypeSet.has(supertype)) {
373
373
  if (!typenameSupertypeSet.has(supertype)) {
374
374
  if (checkingFuzzySubtypes) {
375
- __DEV__ && invariant_1.invariant.warn(104, typename, supertype);
375
+ __DEV__ && invariant_1.invariant.warn(109, typename, supertype);
376
376
  }
377
377
  // Record positive results for faster future lookup.
378
378
  // Unfortunately, we cannot safely cache negative results,
@@ -625,7 +625,7 @@ function normalizeReadFieldOptions(readFieldArgs, objectOrReference, variables)
625
625
  }
626
626
  }
627
627
  if (environment_1.__DEV__ && options.from === void 0) {
628
- __DEV__ && invariant_1.invariant.warn(105, (0, internal_2.stringifyForDisplay)(Array.from(readFieldArgs)));
628
+ __DEV__ && invariant_1.invariant.warn(110, (0, internal_2.stringifyForDisplay)(Array.from(readFieldArgs)));
629
629
  }
630
630
  if (void 0 === options.variables) {
631
631
  options.variables = variables;
@@ -635,7 +635,7 @@ function normalizeReadFieldOptions(readFieldArgs, objectOrReference, variables)
635
635
  function makeMergeObjectsFunction(store) {
636
636
  return function mergeObjects(existing, incoming) {
637
637
  if ((0, internal_2.isArray)(existing) || (0, internal_2.isArray)(incoming)) {
638
- throw (0, invariant_1.newInvariantError)(106);
638
+ throw (0, invariant_1.newInvariantError)(111);
639
639
  }
640
640
  // These dynamic checks are necessary because the parameters of a
641
641
  // custom merge function can easily have the any type, so the type
@@ -197,7 +197,7 @@ class StoreReader {
197
197
  else {
198
198
  const fragment = (0, internal_1.getFragmentFromSelection)(selection, context.lookupFragment);
199
199
  if (!fragment && selection.kind === graphql_1.Kind.FRAGMENT_SPREAD) {
200
- throw (0, invariant_1.newInvariantError)(107, selection.name.value);
200
+ throw (0, invariant_1.newInvariantError)(112, selection.name.value);
201
201
  }
202
202
  if (fragment && policies.fragmentMatches(fragment, typename)) {
203
203
  fragment.selectionSet.selections.forEach(workSet.add, workSet);
@@ -281,7 +281,7 @@ function assertSelectionSetForIdValue(store, field, fieldValue) {
281
281
  if ((0, internal_1.isNonNullObject)(value)) {
282
282
  (0, invariant_1.invariant)(
283
283
  !(0, utilities_1.isReference)(value),
284
- 108,
284
+ 113,
285
285
  (0, helpers_js_1.getTypenameFromStoreObject)(store, value),
286
286
  field.name.value
287
287
  );
@@ -75,7 +75,7 @@ class StoreWriter {
75
75
  path: [],
76
76
  });
77
77
  if (!(0, utilities_1.isReference)(ref)) {
78
- throw (0, invariant_1.newInvariantError)(109, result);
78
+ throw (0, invariant_1.newInvariantError)(114, result);
79
79
  }
80
80
  // So far, the store has not been modified, so now it's time to process
81
81
  // context.incomingById and merge those incoming fields into context.store.
@@ -237,7 +237,7 @@ class StoreWriter {
237
237
  // provide a default value, so its absence from the written data should
238
238
  // not be cause for alarm.
239
239
  !policies.getReadFunction(typename, field.name.value)) {
240
- invariant_1.invariant.error(110, (0, internal_1.resultKeyNameFromField)(field), result);
240
+ invariant_1.invariant.error(115, (0, internal_1.resultKeyNameFromField)(field), result);
241
241
  }
242
242
  });
243
243
  // Identify the result object, even if dataId was already provided,
@@ -385,7 +385,7 @@ class StoreWriter {
385
385
  else {
386
386
  const fragment = (0, internal_1.getFragmentFromSelection)(selection, context.lookupFragment);
387
387
  if (!fragment && selection.kind === graphql_1.Kind.FRAGMENT_SPREAD) {
388
- throw (0, invariant_1.newInvariantError)(111, selection.name.value);
388
+ throw (0, invariant_1.newInvariantError)(116, selection.name.value);
389
389
  }
390
390
  if (fragment &&
391
391
  policies.fragmentMatches(fragment, typename, result, context.variables)) {
@@ -558,7 +558,7 @@ function warnAboutDataLoss(existingRef, incomingObj, storeFieldName, store) {
558
558
  }
559
559
  });
560
560
  }
561
- __DEV__ && invariant_1.invariant.warn(112, fieldName, parentType, childTypenames.length ?
561
+ __DEV__ && invariant_1.invariant.warn(117, fieldName, parentType, childTypenames.length ?
562
562
  "either ensure all objects of type " +
563
563
  childTypenames.join(" and ") +
564
564
  " have an ID or a custom merge function, or "
@@ -46,6 +46,7 @@ class ApolloClient {
46
46
  queryDeduplication;
47
47
  defaultOptions;
48
48
  devtoolsConfig;
49
+ refetchEventManager;
49
50
  queryManager;
50
51
  devToolsHookCb;
51
52
  resetStoreCallbacks = [];
@@ -80,7 +81,7 @@ class ApolloClient {
80
81
  (0, invariant_1.invariant)(options.cache, 68);
81
82
  (0, invariant_1.invariant)(options.link, 69);
82
83
  }
83
- const { cache, documentTransform, ssrMode = false, ssrForceFetchDelay = 0, queryDeduplication = true, defaultOptions, defaultContext, assumeImmutableResults = cache.assumeImmutableResults, localState, devtools, dataMasking, link, incrementalHandler = new incremental_1.NotImplementedHandler(), experiments = [], } = options;
84
+ const { cache, documentTransform, ssrMode = false, ssrForceFetchDelay = 0, queryDeduplication = true, defaultOptions, defaultContext, assumeImmutableResults = cache.assumeImmutableResults, localState, devtools, dataMasking, link, incrementalHandler = new incremental_1.NotImplementedHandler(), experiments = [], refetchEventManager, } = options;
84
85
  this.link = link;
85
86
  this.cache = cache;
86
87
  this.queryDeduplication = queryDeduplication;
@@ -130,6 +131,8 @@ class ApolloClient {
130
131
  if (this.devtoolsConfig.enabled)
131
132
  this.connectToDevTools();
132
133
  experiments.forEach((experiment) => experiment.call(this, options));
134
+ this.refetchEventManager = refetchEventManager;
135
+ this.refetchEventManager?.connect(this);
133
136
  }
134
137
  connectToDevTools() {
135
138
  if (typeof window === "undefined") {
@@ -200,9 +203,11 @@ class ApolloClient {
200
203
  * - Unsubscribes all active `ObservableQuery` instances by emitting a `completed` event
201
204
  * - Rejects all currently running queries with "QueryManager stopped while query was in flight"
202
205
  * - Removes all queryRefs from the suspense cache
206
+ * - Disconnects the `RefetchEventManager` if configured.
203
207
  */
204
208
  stop() {
205
209
  this.queryManager.stop();
210
+ this.refetchEventManager?.disconnect(this);
206
211
  }
207
212
  /**
208
213
  * This watches the cache store of the query according to the options specified and
@@ -225,7 +230,34 @@ class ApolloClient {
225
230
  */
226
231
  watchQuery(options) {
227
232
  if (this.defaultOptions.watchQuery) {
233
+ 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;
228
240
  options = (0, internal_1.mergeOptions)(this.defaultOptions.watchQuery, options);
241
+ if (mergedRefetchOn) {
242
+ options.refetchOn = mergedRefetchOn;
243
+ }
244
+ }
245
+ if (environment_1.__DEV__) {
246
+ const { refetchOn, query } = options;
247
+ const { refetchEventManager } = this;
248
+ if (refetchOn) {
249
+ const operationName = (0, internal_1.getOperationName)(query, "(anonymous)");
250
+ if (!refetchEventManager) {
251
+ __DEV__ && invariant_1.invariant.warn(70, operationName);
252
+ }
253
+ else if (typeof refetchOn === "object") {
254
+ Object.keys(refetchOn).forEach((source) => {
255
+ if (!refetchEventManager.hasSource(source)) {
256
+ __DEV__ && invariant_1.invariant.warn(71, source, operationName);
257
+ }
258
+ });
259
+ }
260
+ }
229
261
  }
230
262
  return this.queryManager.watchQuery(options);
231
263
  }
@@ -243,37 +275,37 @@ class ApolloClient {
243
275
  options = (0, internal_1.mergeOptions)(this.defaultOptions.query, options);
244
276
  }
245
277
  if (environment_1.__DEV__) {
246
- (0, invariant_1.invariant)(options.fetchPolicy !== "cache-and-network", 70);
247
- (0, invariant_1.invariant)(options.fetchPolicy !== "standby", 71);
248
- (0, invariant_1.invariant)(options.query, 72);
249
- (0, invariant_1.invariant)(options.query.kind === "Document", 73);
250
- (0, invariant_1.invariant)(!options.returnPartialData, 74);
251
- (0, invariant_1.invariant)(!options.pollInterval, 75);
252
- (0, invariant_1.invariant)(!options.notifyOnNetworkStatusChange, 76);
278
+ (0, invariant_1.invariant)(options.fetchPolicy !== "cache-and-network", 72);
279
+ (0, invariant_1.invariant)(options.fetchPolicy !== "standby", 73);
280
+ (0, invariant_1.invariant)(options.query, 74);
281
+ (0, invariant_1.invariant)(options.query.kind === "Document", 75);
282
+ (0, invariant_1.invariant)(!options.returnPartialData, 76);
283
+ (0, invariant_1.invariant)(!options.pollInterval, 77);
284
+ (0, invariant_1.invariant)(!options.notifyOnNetworkStatusChange, 78);
253
285
  }
254
286
  return this.queryManager.query(options);
255
287
  };
256
288
  /**
257
- * This resolves a single mutation according to the options specified and returns a
258
- * Promise which is either resolved with the resulting data or rejected with an
259
- * error. In some cases both `data` and `errors` might be undefined, for example
260
- * when `errorPolicy` is set to `'ignore'`.
261
- *
262
- * It takes options as an object with the following keys and values:
263
- */
264
- mutate(options) {
289
+ * This resolves a single mutation according to the options specified and returns a
290
+ * Promise which is either resolved with the resulting data or rejected with an
291
+ * error. In some cases both `data` and `errors` might be undefined, for example
292
+ * when `errorPolicy` is set to `'ignore'`.
293
+ *
294
+ * It takes options as an object with the following keys and values:
295
+ */
296
+ mutate = (options) => {
265
297
  const optionsWithDefaults = (0, internal_1.mergeOptions)((0, internal_1.compact)({
266
298
  fetchPolicy: "network-only",
267
299
  errorPolicy: "none",
268
300
  }, this.defaultOptions.mutate), options);
269
301
  if (environment_1.__DEV__) {
270
- (0, invariant_1.invariant)(optionsWithDefaults.mutation, 77);
302
+ (0, invariant_1.invariant)(optionsWithDefaults.mutation, 79);
271
303
  (0, invariant_1.invariant)(optionsWithDefaults.fetchPolicy === "network-only" ||
272
- optionsWithDefaults.fetchPolicy === "no-cache", 78);
304
+ optionsWithDefaults.fetchPolicy === "no-cache", 80);
273
305
  }
274
306
  (0, internal_1.checkDocument)(optionsWithDefaults.mutation, graphql_1.OperationTypeNode.MUTATION);
275
307
  return this.queryManager.mutate(optionsWithDefaults);
276
- }
308
+ };
277
309
  /**
278
310
  * This subscribes to a graphql subscription according to the options specified and returns an
279
311
  * `Observable` which either emits received data or an error.
@@ -475,7 +507,7 @@ class ApolloClient {
475
507
  // result.queries and result.results instead, you shouldn't have to worry
476
508
  // about preventing uncaught rejections for the Promise.all result.
477
509
  result.catch((error) => {
478
- __DEV__ && invariant_1.invariant.debug(79, error);
510
+ __DEV__ && invariant_1.invariant.debug(81, error);
479
511
  });
480
512
  return result;
481
513
  }