@apollo/client 4.3.0-alpha.0 → 4.3.0-alpha.2

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 (105) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/__cjs/cache/core/Scalar.cjs +36 -0
  3. package/__cjs/cache/core/Scalar.cjs.map +1 -0
  4. package/__cjs/cache/core/Scalar.d.cts +20 -0
  5. package/__cjs/cache/core/cache.cjs +8 -1
  6. package/__cjs/cache/core/cache.cjs.map +1 -1
  7. package/__cjs/cache/core/cache.d.cts +16 -0
  8. package/__cjs/cache/index.cjs +3 -1
  9. package/__cjs/cache/index.cjs.map +1 -1
  10. package/__cjs/cache/index.d.cts +2 -1
  11. package/__cjs/cache/inmemory/entityStore.cjs +39 -2
  12. package/__cjs/cache/inmemory/entityStore.cjs.map +1 -1
  13. package/__cjs/cache/inmemory/entityStore.d.cts +2 -0
  14. package/__cjs/cache/inmemory/helpers.d.cts +9 -1
  15. package/__cjs/cache/inmemory/inMemoryCache.cjs +73 -4
  16. package/__cjs/cache/inmemory/inMemoryCache.cjs.map +1 -1
  17. package/__cjs/cache/inmemory/inMemoryCache.d.cts +35 -3
  18. package/__cjs/cache/inmemory/policies.cjs +8 -1
  19. package/__cjs/cache/inmemory/policies.cjs.map +1 -1
  20. package/__cjs/cache/inmemory/policies.d.cts +4 -1
  21. package/__cjs/cache/inmemory/readFromStore.cjs +17 -3
  22. package/__cjs/cache/inmemory/readFromStore.cjs.map +1 -1
  23. package/__cjs/cache/inmemory/types.d.cts +20 -4
  24. package/__cjs/cache/inmemory/writeToStore.cjs +4 -4
  25. package/__cjs/core/ApolloClient.cjs +2 -2
  26. package/__cjs/core/ApolloClient.cjs.map +1 -1
  27. package/__cjs/core/QueryInfo.cjs +40 -30
  28. package/__cjs/core/QueryInfo.cjs.map +1 -1
  29. package/__cjs/core/QueryInfo.d.cts +2 -2
  30. package/__cjs/core/QueryManager.cjs +2 -1
  31. package/__cjs/core/QueryManager.cjs.map +1 -1
  32. package/__cjs/core/QueryManager.d.cts +1 -1
  33. package/__cjs/core/index.cjs +2 -1
  34. package/__cjs/core/index.cjs.map +1 -1
  35. package/__cjs/core/index.d.cts +2 -2
  36. package/__cjs/core/types.d.cts +1 -2
  37. package/__cjs/invariantErrorCodes.cjs +12 -7
  38. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  39. package/__cjs/react/hooks/useLazyQuery.d.cts +13 -13
  40. package/__cjs/react/types/deprecated.d.cts +5 -4
  41. package/__cjs/utilities/internal/getUnwrappedType.cjs +20 -0
  42. package/__cjs/utilities/internal/getUnwrappedType.cjs.map +1 -0
  43. package/__cjs/utilities/internal/getUnwrappedType.d.cts +8 -0
  44. package/__cjs/utilities/internal/index.cjs +4 -2
  45. package/__cjs/utilities/internal/index.cjs.map +1 -1
  46. package/__cjs/utilities/internal/index.d.cts +2 -0
  47. package/__cjs/utilities/internal/types/IsLooselyEqual.cjs +3 -0
  48. package/__cjs/utilities/internal/types/IsLooselyEqual.cjs.map +1 -0
  49. package/__cjs/utilities/internal/types/IsLooselyEqual.d.cts +10 -0
  50. package/__cjs/version.cjs +1 -1
  51. package/cache/core/Scalar.d.ts +20 -0
  52. package/cache/core/Scalar.js +32 -0
  53. package/cache/core/Scalar.js.map +1 -0
  54. package/cache/core/cache.d.ts +16 -0
  55. package/cache/core/cache.js +8 -1
  56. package/cache/core/cache.js.map +1 -1
  57. package/cache/index.d.ts +2 -1
  58. package/cache/index.js +1 -0
  59. package/cache/index.js.map +1 -1
  60. package/cache/inmemory/entityStore.d.ts +2 -0
  61. package/cache/inmemory/entityStore.js +40 -3
  62. package/cache/inmemory/entityStore.js.map +1 -1
  63. package/cache/inmemory/helpers.d.ts +9 -1
  64. package/cache/inmemory/inMemoryCache.d.ts +35 -3
  65. package/cache/inmemory/inMemoryCache.js +74 -5
  66. package/cache/inmemory/inMemoryCache.js.map +1 -1
  67. package/cache/inmemory/policies.d.ts +4 -1
  68. package/cache/inmemory/policies.js +8 -1
  69. package/cache/inmemory/policies.js.map +1 -1
  70. package/cache/inmemory/readFromStore.js +12 -2
  71. package/cache/inmemory/readFromStore.js.map +1 -1
  72. package/cache/inmemory/types.d.ts +20 -4
  73. package/cache/inmemory/types.js.map +1 -1
  74. package/cache/inmemory/writeToStore.js +4 -4
  75. package/core/ApolloClient.js +2 -2
  76. package/core/ApolloClient.js.map +1 -1
  77. package/core/QueryInfo.d.ts +2 -2
  78. package/core/QueryInfo.js +40 -30
  79. package/core/QueryInfo.js.map +1 -1
  80. package/core/QueryManager.d.ts +1 -1
  81. package/core/QueryManager.js +2 -1
  82. package/core/QueryManager.js.map +1 -1
  83. package/core/index.d.ts +2 -2
  84. package/core/index.js +1 -1
  85. package/core/index.js.map +1 -1
  86. package/core/types.d.ts +1 -2
  87. package/core/types.js.map +1 -1
  88. package/invariantErrorCodes.js +12 -7
  89. package/package.json +1 -1
  90. package/react/hooks/useLazyQuery.d.ts +13 -13
  91. package/react/hooks/useLazyQuery.js.map +1 -1
  92. package/react/hooks-compiled/useLazyQuery.d.ts +13 -13
  93. package/react/hooks-compiled/useLazyQuery.js.map +1 -1
  94. package/react/types/deprecated.d.ts +5 -4
  95. package/react/types/deprecated.js.map +1 -1
  96. package/utilities/internal/getUnwrappedType.d.ts +8 -0
  97. package/utilities/internal/getUnwrappedType.js +17 -0
  98. package/utilities/internal/getUnwrappedType.js.map +1 -0
  99. package/utilities/internal/index.d.ts +2 -0
  100. package/utilities/internal/index.js +1 -0
  101. package/utilities/internal/index.js.map +1 -1
  102. package/utilities/internal/types/IsLooselyEqual.d.ts +10 -0
  103. package/utilities/internal/types/IsLooselyEqual.js +2 -0
  104. package/utilities/internal/types/IsLooselyEqual.js.map +1 -0
  105. package/version.js +1 -1
@@ -23,6 +23,8 @@ export declare abstract class EntityStore implements NormalizedCache {
23
23
  evict(options: Cache.EvictOptions, limit: EntityStore): boolean;
24
24
  clear(): void;
25
25
  extract(): NormalizedCacheObject;
26
+ private coerceStoreObject;
27
+ private coerceValue;
26
28
  replace(newData: NormalizedCacheObject | null): void;
27
29
  abstract getStorage(idOrObj: string | StoreObject, ...storeFieldNames: (string | number)[]): StorageType;
28
30
  private rootIds;
@@ -7,7 +7,15 @@ import type { KeyFieldsContext } from "./policies.cjs";
7
7
  import type { InMemoryCacheConfig, NormalizedCache } from "./types.cjs";
8
8
  export declare const hasOwn: (v: PropertyKey) => boolean;
9
9
  export declare function defaultDataIdFromObject({ __typename, id, _id }: Readonly<StoreObject>, context?: KeyFieldsContext): string | undefined;
10
- export declare function normalizeConfig(config: InMemoryCacheConfig): InMemoryCacheConfig;
10
+ export declare function normalizeConfig(config: InMemoryCacheConfig): import("./types.cjs").ApolloReducerConfig & {
11
+ resultCaching?: boolean;
12
+ possibleTypes?: import("./policies.cjs").PossibleTypesMap;
13
+ typePolicies?: import("./policies.cjs").TypePolicies;
14
+ fragments?: FragmentRegistryAPI;
15
+ inputObjects?: import("./types.cjs").InputObjectsOption;
16
+ } & {
17
+ scalars?: import("@apollo/client").InMemoryCache.ScalarsOption;
18
+ };
11
19
  export declare function getTypenameFromStoreObject(store: NormalizedCache, objectOrReference: StoreObject | Reference): string | undefined;
12
20
  export declare const TypeOrFieldNameRegExp: RegExp;
13
21
  export declare function fieldNameFromStoreName(storeFieldName: string): string;
@@ -35,9 +35,9 @@ class InMemoryCache extends cache_js_1.ApolloCache {
35
35
  // cache.policies.addPossibletypes.
36
36
  policies;
37
37
  makeVar = reactiveVars_js_1.makeVar;
38
- constructor(config = {}) {
38
+ constructor(...args) {
39
39
  super();
40
- this.config = (0, helpers_js_1.normalizeConfig)(config);
40
+ this.config = (0, helpers_js_1.normalizeConfig)(args[0] ?? {});
41
41
  this.policies = new policies_js_1.Policies({
42
42
  cache: this,
43
43
  dataIdFromObject: this.config.dataIdFromObject,
@@ -97,6 +97,68 @@ class InMemoryCache extends cache_js_1.ApolloCache {
97
97
  // all CacheGroup dependency information.
98
98
  new Set([this.data.group, this.optimisticData.group]).forEach((group) => group.resetCaching());
99
99
  }
100
+ getScalar(key) {
101
+ return this.config.scalars?.[key];
102
+ }
103
+ serializeVariables(document, variables) {
104
+ if (!variables ||
105
+ Object.keys(variables).length === 0 ||
106
+ (!this.config.scalars && !this.config.inputObjects)) {
107
+ return variables;
108
+ }
109
+ const variableTypes = (0, internal_1.getOperationDefinition)(document)?.variableDefinitions?.reduce((memo, node) => {
110
+ memo[node.variable.name.value] = (0, internal_1.getUnwrappedType)(node.type);
111
+ return memo;
112
+ }, {});
113
+ if (!variableTypes || Object.keys(variableTypes).length === 0) {
114
+ return variables;
115
+ }
116
+ return this.serializeVariablesValue(variables, variableTypes);
117
+ }
118
+ serializeVariablesValue(value, variableTypes, scalar) {
119
+ if (Array.isArray(value)) {
120
+ return this.serializeInputArray(value, variableTypes, scalar);
121
+ }
122
+ if (scalar && value != null) {
123
+ return scalar.coerceToSerialized(value);
124
+ }
125
+ if ((0, internal_1.isPlainObject)(value)) {
126
+ return this.serializeInputObject(value, variableTypes);
127
+ }
128
+ return value;
129
+ }
130
+ serializeInputArray(value, variableTypes, scalar) {
131
+ let changed = false;
132
+ const newValue = value.map((item) => {
133
+ const newItem = this.serializeVariablesValue(item, variableTypes, scalar);
134
+ changed ||= newItem !== item;
135
+ return newItem;
136
+ });
137
+ return changed ? newValue : value;
138
+ }
139
+ serializeInputObject(value, variableTypes) {
140
+ let changed = false;
141
+ const entries = Object.entries(value).map(([name, value]) => {
142
+ const type = variableTypes[name];
143
+ if (!type) {
144
+ return [name, value];
145
+ }
146
+ const inputObject = this.config.inputObjects?.[type];
147
+ if (inputObject) {
148
+ const newValue = this.serializeVariablesValue(value, inputObject.fields);
149
+ changed ||= newValue !== value;
150
+ return [name, newValue];
151
+ }
152
+ const scalar = this.getScalar(type);
153
+ if (scalar) {
154
+ const newValue = this.serializeVariablesValue(value, variableTypes, scalar);
155
+ changed ||= newValue !== value;
156
+ return [name, newValue];
157
+ }
158
+ return [name, value];
159
+ });
160
+ return changed ? Object.fromEntries(entries) : value;
161
+ }
100
162
  restore(data) {
101
163
  this.init();
102
164
  // Since calling this.init() discards/replaces the entire StoreReader, along
@@ -110,7 +172,7 @@ class InMemoryCache extends cache_js_1.ApolloCache {
110
172
  return (optimistic ? this.optimisticData : this.data).extract();
111
173
  }
112
174
  read(options) {
113
- const {
175
+ const { query, variables,
114
176
  // Since read returns data or null, without any additional metadata
115
177
  // about whether/where there might have been missing fields, the
116
178
  // default behavior cannot be returnPartialData = true (like it is
@@ -121,15 +183,20 @@ class InMemoryCache extends cache_js_1.ApolloCache {
121
183
  returnPartialData = false, } = options;
122
184
  return this.storeReader.diffQueryAgainstStore({
123
185
  ...options,
186
+ variables: this.serializeVariables(query, variables),
124
187
  store: options.optimistic ? this.optimisticData : this.data,
125
188
  config: this.config,
126
189
  returnPartialData,
127
190
  }).result;
128
191
  }
129
192
  write(options) {
193
+ const { query, variables } = options;
130
194
  try {
131
195
  ++this.txCount;
132
- return this.storeWriter.writeToStore(this.data, options);
196
+ return this.storeWriter.writeToStore(this.data, {
197
+ ...options,
198
+ variables: this.serializeVariables(query, variables),
199
+ });
133
200
  }
134
201
  finally {
135
202
  if (!--this.txCount && options.broadcast !== false) {
@@ -165,8 +232,10 @@ class InMemoryCache extends cache_js_1.ApolloCache {
165
232
  }
166
233
  }
167
234
  diff(options) {
235
+ const { variables } = options;
168
236
  return this.storeReader.diffQueryAgainstStore({
169
237
  ...options,
238
+ variables: this.serializeVariables(options.query, variables),
170
239
  store: options.optimistic ? this.optimisticData : this.data,
171
240
  rootId: options.id || "ROOT_QUERY",
172
241
  config: this.config,