@apollo/client 4.1.0-alpha.9 → 4.1.0-rc.0

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 (99) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/__cjs/cache/core/types/Cache.d.cts +3 -2
  3. package/__cjs/cache/index.cjs.map +1 -1
  4. package/__cjs/cache/index.d.cts +1 -1
  5. package/__cjs/cache/inmemory/policies.cjs +72 -22
  6. package/__cjs/cache/inmemory/policies.cjs.map +1 -1
  7. package/__cjs/cache/inmemory/policies.d.cts +31 -9
  8. package/__cjs/cache/inmemory/types.d.cts +3 -1
  9. package/__cjs/cache/inmemory/writeToStore.cjs +18 -4
  10. package/__cjs/cache/inmemory/writeToStore.cjs.map +1 -1
  11. package/__cjs/cache/inmemory/writeToStore.d.cts +0 -1
  12. package/__cjs/core/QueryInfo.cjs +6 -1
  13. package/__cjs/core/QueryInfo.cjs.map +1 -1
  14. package/__cjs/core/QueryInfo.d.cts +4 -3
  15. package/__cjs/core/QueryManager.cjs +13 -3
  16. package/__cjs/core/QueryManager.cjs.map +1 -1
  17. package/__cjs/core/QueryManager.d.cts +2 -1
  18. package/__cjs/incremental/handlers/graphql17Alpha9.cjs +39 -0
  19. package/__cjs/incremental/handlers/graphql17Alpha9.cjs.map +1 -1
  20. package/__cjs/incremental/handlers/graphql17Alpha9.d.cts +1 -0
  21. package/__cjs/incremental/types.d.cts +9 -0
  22. package/__cjs/react/query-preloader/createQueryPreloader.cjs +41 -1
  23. package/__cjs/react/query-preloader/createQueryPreloader.cjs.map +1 -1
  24. package/__cjs/utilities/internal/constants.cjs +6 -1
  25. package/__cjs/utilities/internal/constants.cjs.map +1 -1
  26. package/__cjs/utilities/internal/constants.d.cts +5 -0
  27. package/__cjs/utilities/internal/index.cjs +2 -1
  28. package/__cjs/utilities/internal/index.cjs.map +1 -1
  29. package/__cjs/utilities/internal/index.d.cts +3 -1
  30. package/__cjs/utilities/internal/ponyfills/FinalizationRegistry.cjs +61 -0
  31. package/__cjs/utilities/internal/ponyfills/FinalizationRegistry.cjs.map +1 -0
  32. package/__cjs/utilities/internal/ponyfills/FinalizationRegistry.d.cts +11 -0
  33. package/__cjs/utilities/internal/ponyfills/index.cjs +6 -0
  34. package/__cjs/utilities/internal/ponyfills/index.cjs.map +1 -0
  35. package/__cjs/utilities/internal/ponyfills/index.d.cts +3 -0
  36. package/__cjs/utilities/internal/ponyfills/index.react-native.cjs +6 -0
  37. package/__cjs/utilities/internal/ponyfills/index.react-native.cjs.map +1 -0
  38. package/__cjs/utilities/internal/ponyfills/index.react-native.d.cts +2 -0
  39. package/__cjs/utilities/internal/types/ExtensionsWithStreamDetails.cjs +3 -0
  40. package/__cjs/utilities/internal/types/ExtensionsWithStreamDetails.cjs.map +1 -0
  41. package/__cjs/utilities/internal/types/ExtensionsWithStreamDetails.d.cts +12 -0
  42. package/__cjs/utilities/internal/types/StreamInfoTrie.cjs +3 -0
  43. package/__cjs/utilities/internal/types/StreamInfoTrie.cjs.map +1 -0
  44. package/__cjs/utilities/internal/types/StreamInfoTrie.d.cts +16 -0
  45. package/__cjs/version.cjs +1 -1
  46. package/__cjs/version.cjs.map +1 -1
  47. package/cache/core/types/Cache.d.ts +3 -2
  48. package/cache/core/types/Cache.js.map +1 -1
  49. package/cache/index.d.ts +1 -1
  50. package/cache/index.js.map +1 -1
  51. package/cache/inmemory/policies.d.ts +31 -9
  52. package/cache/inmemory/policies.js +60 -11
  53. package/cache/inmemory/policies.js.map +1 -1
  54. package/cache/inmemory/types.d.ts +3 -1
  55. package/cache/inmemory/types.js.map +1 -1
  56. package/cache/inmemory/writeToStore.d.ts +0 -1
  57. package/cache/inmemory/writeToStore.js +20 -6
  58. package/cache/inmemory/writeToStore.js.map +1 -1
  59. package/core/QueryInfo.d.ts +4 -3
  60. package/core/QueryInfo.js +7 -2
  61. package/core/QueryInfo.js.map +1 -1
  62. package/core/QueryManager.d.ts +2 -1
  63. package/core/QueryManager.js +14 -4
  64. package/core/QueryManager.js.map +1 -1
  65. package/incremental/handlers/graphql17Alpha9.d.ts +1 -0
  66. package/incremental/handlers/graphql17Alpha9.js +40 -1
  67. package/incremental/handlers/graphql17Alpha9.js.map +1 -1
  68. package/incremental/types.d.ts +9 -0
  69. package/incremental/types.js.map +1 -1
  70. package/legacyEntryPoints/utilities/internal/ponyfills/index.d.ts +1 -0
  71. package/legacyEntryPoints/utilities/internal/ponyfills/index.js +1 -0
  72. package/legacyEntryPoints/utilities/internal/ponyfills/ponyfills.cjs +1 -0
  73. package/legacyEntryPoints/utilities/internal/ponyfills/ponyfills.d.cts +1 -0
  74. package/package.json +15 -1
  75. package/react/query-preloader/createQueryPreloader.js +41 -1
  76. package/react/query-preloader/createQueryPreloader.js.map +1 -1
  77. package/utilities/internal/constants.d.ts +5 -0
  78. package/utilities/internal/constants.js +5 -0
  79. package/utilities/internal/constants.js.map +1 -1
  80. package/utilities/internal/index.d.ts +3 -1
  81. package/utilities/internal/index.js +1 -1
  82. package/utilities/internal/index.js.map +1 -1
  83. package/utilities/internal/ponyfills/FinalizationRegistry.d.ts +11 -0
  84. package/utilities/internal/ponyfills/FinalizationRegistry.js +57 -0
  85. package/utilities/internal/ponyfills/FinalizationRegistry.js.map +1 -0
  86. package/utilities/internal/ponyfills/index.d.ts +3 -0
  87. package/utilities/internal/ponyfills/index.js +3 -0
  88. package/utilities/internal/ponyfills/index.js.map +1 -0
  89. package/utilities/internal/ponyfills/index.react-native.d.ts +2 -0
  90. package/utilities/internal/ponyfills/index.react-native.js +2 -0
  91. package/utilities/internal/ponyfills/index.react-native.js.map +1 -0
  92. package/utilities/internal/types/ExtensionsWithStreamDetails.d.ts +12 -0
  93. package/utilities/internal/types/ExtensionsWithStreamDetails.js +2 -0
  94. package/utilities/internal/types/ExtensionsWithStreamDetails.js.map +1 -0
  95. package/utilities/internal/types/StreamInfoTrie.d.ts +16 -0
  96. package/utilities/internal/types/StreamInfoTrie.js +2 -0
  97. package/utilities/internal/types/StreamInfoTrie.js.map +1 -0
  98. package/version.js +1 -1
  99. package/version.js.map +1 -1
@@ -1,5 +1,6 @@
1
1
  import type { FieldNode, FragmentDefinitionNode, InlineFragmentNode, SelectionSetNode } from "graphql";
2
2
  import type { OperationVariables } from "@apollo/client";
3
+ import type { Incremental } from "@apollo/client/incremental";
3
4
  import type { Reference, StoreObject, StoreValue } from "@apollo/client/utilities";
4
5
  import { isReference } from "@apollo/client/utilities";
5
6
  import type { FragmentMap } from "@apollo/client/utilities/internal";
@@ -36,10 +37,10 @@ export type KeyArgsFunction = (args: Record<string, any> | null, context: {
36
37
  field: FieldNode | null;
37
38
  variables?: Record<string, any>;
38
39
  }) => KeySpecifier | false | ReturnType<IdGetter>;
39
- export type FieldPolicy<TExisting = any, TIncoming = TExisting, TReadResult = TIncoming, TOptions extends FieldFunctionOptions = FieldFunctionOptions> = {
40
+ export type FieldPolicy<TExisting = any, TIncoming = TExisting, TReadResult = TIncoming, TReadOptions extends FieldReadFunctionOptions = FieldReadFunctionOptions, TMergeOptions extends FieldMergeFunctionOptions = FieldMergeFunctionOptions> = {
40
41
  keyArgs?: KeySpecifier | KeyArgsFunction | false;
41
- read?: FieldReadFunction<TExisting, TReadResult, TOptions>;
42
- merge?: FieldMergeFunction<TExisting, TIncoming, TOptions> | boolean;
42
+ read?: FieldReadFunction<TExisting, TReadResult, TReadOptions>;
43
+ merge?: FieldMergeFunction<TExisting, TIncoming, TMergeOptions> | boolean;
43
44
  };
44
45
  export type StorageType = Record<string, any>;
45
46
  export interface FieldFunctionOptions<TArgs = Record<string, any>, TVariables extends OperationVariables = Record<string, any>> {
@@ -55,15 +56,36 @@ export interface FieldFunctionOptions<TArgs = Record<string, any>, TVariables ex
55
56
  readField: ReadFieldFunction;
56
57
  canRead: CanReadFunction;
57
58
  mergeObjects: MergeObjectsFunction;
59
+ }
60
+ export interface FieldReadFunctionOptions<TArgs = Record<string, any>, TVariables extends OperationVariables = Record<string, any>> extends FieldFunctionOptions<TArgs, TVariables> {
61
+ }
62
+ export interface FieldMergeFunctionOptions<TArgs = Record<string, any>, TVariables extends OperationVariables = Record<string, any>> extends FieldFunctionOptions<TArgs, TVariables> {
63
+ /**
64
+ * Any `extensions` provided when writing the cache.
65
+ */
66
+ extensions: Record<string, unknown> | undefined;
67
+ /**
68
+ * Details about the field when the `@stream` directive is used. Useful with
69
+ * custom merge functions to determine how to merge existing cache data with
70
+ * the incoming stream array.
71
+ *
72
+ * This field is only available in `merge` functions when the `@stream`
73
+ * directive is used on the field.
74
+ *
75
+ * > [!NOTE]
76
+ * > This field is not available when using the `Defer20220824Handler`
77
+ */
78
+ streamFieldInfo?: Incremental.StreamFieldInfo;
58
79
  /**
59
- * Any `extensions` provided when writing the cache. `extensions` is only
60
- * available in `merge` functions and never available in `read` functions.
80
+ * The same value as the `existing` argument, but preserves the `existing`
81
+ * value on refetches when `refetchWritePolicy` is `overwrite` (the default).
61
82
  */
62
- extensions?: Record<string, unknown>;
83
+ existingData: unknown;
63
84
  }
64
85
  type MergeObjectsFunction = <T extends StoreObject | Reference>(existing: T, incoming: T) => T;
65
- export type FieldReadFunction<TExisting = any, TReadResult = TExisting, TOptions extends FieldFunctionOptions = FieldFunctionOptions> = (existing: SafeReadonly<TExisting> | undefined, options: TOptions) => TReadResult | undefined;
66
- export type FieldMergeFunction<TExisting = any, TIncoming = TExisting, TOptions extends FieldFunctionOptions = FieldFunctionOptions> = (existing: SafeReadonly<TExisting> | undefined, incoming: SafeReadonly<TIncoming>, options: TOptions) => SafeReadonly<TExisting>;
86
+ export type FieldReadFunction<TExisting = any, TReadResult = TExisting, TOptions extends FieldReadFunctionOptions = FieldReadFunctionOptions> = (existing: SafeReadonly<TExisting> | undefined, options: TOptions) => TReadResult | undefined;
87
+ export type FieldMergeFunction<TExisting = any, TIncoming = TExisting, TOptions extends FieldMergeFunctionOptions = FieldMergeFunctionOptions> = (existing: SafeReadonly<TExisting> | undefined, incoming: SafeReadonly<TIncoming>, options: TOptions) => SafeReadonly<TExisting>;
88
+ export declare const defaultStreamFieldMergeFn: FieldMergeFunction<Array<any>>;
67
89
  export type PossibleTypesMap = {
68
90
  [supertype: string]: string[];
69
91
  };
@@ -97,7 +119,7 @@ export declare class Policies {
97
119
  readField<V = StoreValue>(options: ReadFieldOptions, context: ReadMergeModifyContext): SafeReadonly<V> | undefined;
98
120
  getReadFunction(typename: string | undefined, fieldName: string): FieldReadFunction | undefined;
99
121
  getMergeFunction(parentTypename: string | undefined, fieldName: string, childTypename: string | undefined): FieldMergeFunction | undefined;
100
- runMergeFunction(existing: StoreValue, incoming: StoreValue, { field, typename, merge }: MergeInfo, context: WriteContext, storage?: StorageType): any;
122
+ runMergeFunction(existing: StoreValue, incoming: StoreValue, { field, typename, merge, path }: MergeInfo, context: WriteContext, storage?: StorageType): any;
101
123
  }
102
124
  export declare function normalizeReadFieldOptions(readFieldArgs: any[], objectOrReference: StoreObject | Reference | undefined, variables?: ReadMergeModifyContext["variables"]): ReadFieldOptions;
103
125
  export {};
@@ -1,6 +1,7 @@
1
1
  import type { DocumentNode, FieldNode } from "graphql";
2
2
  import type { OperationVariables } from "@apollo/client";
3
3
  import type { Reference, StoreObject, StoreValue } from "@apollo/client/utilities";
4
+ import type { ExtensionsWithStreamInfo } from "@apollo/client/utilities/internal";
4
5
  import type { Transaction } from "../core/cache.cjs";
5
6
  import type { AllFieldsModifier, CanReadFunction, Modifiers, ToReferenceFunction } from "../core/types/common.cjs";
6
7
  import type { FieldValueGetter } from "./entityStore.cjs";
@@ -93,6 +94,7 @@ export interface MergeInfo {
93
94
  field: FieldNode;
94
95
  typename: string | undefined;
95
96
  merge: FieldMergeFunction;
97
+ path: Array<string | number>;
96
98
  }
97
99
  export interface MergeTree {
98
100
  info?: MergeInfo;
@@ -102,6 +104,6 @@ export interface ReadMergeModifyContext {
102
104
  store: NormalizedCache;
103
105
  variables?: OperationVariables;
104
106
  varString?: string;
105
- extensions?: Record<string, unknown>;
107
+ extensions?: ExtensionsWithStreamInfo;
106
108
  }
107
109
  //# sourceMappingURL=types.d.cts.map
@@ -72,6 +72,7 @@ class StoreWriter {
72
72
  selectionSet: operationDefinition.selectionSet,
73
73
  mergeTree: { map: new Map() },
74
74
  context,
75
+ path: [],
75
76
  });
76
77
  if (!(0, utilities_1.isReference)(ref)) {
77
78
  throw (0, invariant_1.newInvariantError)(109, result);
@@ -129,7 +130,7 @@ class StoreWriter {
129
130
  processSelectionSet({ dataId, result, selectionSet, context,
130
131
  // This object allows processSelectionSet to report useful information
131
132
  // to its callers without explicitly returning that information.
132
- mergeTree, }) {
133
+ mergeTree, path: currentPath, }) {
133
134
  const { policies } = this.cache;
134
135
  // This variable will be repeatedly updated using context.merge to
135
136
  // accumulate all fields that need to be written into the store.
@@ -175,6 +176,7 @@ class StoreWriter {
175
176
  context, typename).forEach((context, field) => {
176
177
  const resultFieldKey = (0, internal_1.resultKeyNameFromField)(field);
177
178
  const value = result[resultFieldKey];
179
+ const path = [...currentPath, field.name.value];
178
180
  fieldNodeSet.add(field);
179
181
  if (value !== void 0) {
180
182
  const storeFieldName = policies.getStoreFieldName({
@@ -189,7 +191,7 @@ class StoreWriter {
189
191
  // values before processing nested selection sets.
190
192
  field.selectionSet ?
191
193
  getContextFlavor(context, false, false)
192
- : context, childTree);
194
+ : context, childTree, path);
193
195
  // To determine if this field holds a child object with a merge function
194
196
  // defined in its type policy (see PR #7070), we need to figure out the
195
197
  // child object's __typename.
@@ -207,6 +209,17 @@ class StoreWriter {
207
209
  field,
208
210
  typename,
209
211
  merge,
212
+ path,
213
+ };
214
+ }
215
+ else if ((0, internal_1.hasDirectives)(["stream"], field) &&
216
+ Array.isArray(incomingValue) &&
217
+ context.extensions?.[internal_1.streamInfoSymbol]) {
218
+ childTree.info = {
219
+ field,
220
+ typename,
221
+ merge: policies_js_1.defaultStreamFieldMergeFn,
222
+ path,
210
223
  };
211
224
  }
212
225
  else {
@@ -292,7 +305,7 @@ class StoreWriter {
292
305
  }
293
306
  return incoming;
294
307
  }
295
- processFieldValue(value, field, context, mergeTree) {
308
+ processFieldValue(value, field, context, mergeTree, path) {
296
309
  if (!field.selectionSet || value === null) {
297
310
  // In development, we need to clone scalar values so that they can be
298
311
  // safely frozen with maybeDeepFreeze in readFromStore.ts. In production,
@@ -301,7 +314,7 @@ class StoreWriter {
301
314
  }
302
315
  if ((0, internal_1.isArray)(value)) {
303
316
  return value.map((item, i) => {
304
- const value = this.processFieldValue(item, field, context, getChildMergeTree(mergeTree, i));
317
+ const value = this.processFieldValue(item, field, context, getChildMergeTree(mergeTree, i), [...path, i]);
305
318
  maybeRecycleChildMergeTree(mergeTree, i);
306
319
  return value;
307
320
  });
@@ -311,6 +324,7 @@ class StoreWriter {
311
324
  selectionSet: field.selectionSet,
312
325
  context,
313
326
  mergeTree,
327
+ path,
314
328
  });
315
329
  }
316
330
  // Implements https://spec.graphql.org/draft/#sec-Field-Collection, but with