@apollo/client 4.3.0-alpha.1 → 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 (97) hide show
  1. package/CHANGELOG.md +130 -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/QueryManager.cjs +2 -1
  30. package/__cjs/core/QueryManager.cjs.map +1 -1
  31. package/__cjs/core/QueryManager.d.cts +1 -1
  32. package/__cjs/core/index.cjs +2 -1
  33. package/__cjs/core/index.cjs.map +1 -1
  34. package/__cjs/core/index.d.cts +2 -2
  35. package/__cjs/invariantErrorCodes.cjs +12 -7
  36. package/__cjs/react/hooks/useLazyQuery.cjs.map +1 -1
  37. package/__cjs/react/hooks/useLazyQuery.d.cts +13 -13
  38. package/__cjs/utilities/internal/getUnwrappedType.cjs +20 -0
  39. package/__cjs/utilities/internal/getUnwrappedType.cjs.map +1 -0
  40. package/__cjs/utilities/internal/getUnwrappedType.d.cts +8 -0
  41. package/__cjs/utilities/internal/index.cjs +4 -2
  42. package/__cjs/utilities/internal/index.cjs.map +1 -1
  43. package/__cjs/utilities/internal/index.d.cts +2 -0
  44. package/__cjs/utilities/internal/types/IsLooselyEqual.cjs +3 -0
  45. package/__cjs/utilities/internal/types/IsLooselyEqual.cjs.map +1 -0
  46. package/__cjs/utilities/internal/types/IsLooselyEqual.d.cts +10 -0
  47. package/__cjs/version.cjs +1 -1
  48. package/cache/core/Scalar.d.ts +20 -0
  49. package/cache/core/Scalar.js +32 -0
  50. package/cache/core/Scalar.js.map +1 -0
  51. package/cache/core/cache.d.ts +16 -0
  52. package/cache/core/cache.js +8 -1
  53. package/cache/core/cache.js.map +1 -1
  54. package/cache/index.d.ts +2 -1
  55. package/cache/index.js +1 -0
  56. package/cache/index.js.map +1 -1
  57. package/cache/inmemory/entityStore.d.ts +2 -0
  58. package/cache/inmemory/entityStore.js +40 -3
  59. package/cache/inmemory/entityStore.js.map +1 -1
  60. package/cache/inmemory/helpers.d.ts +9 -1
  61. package/cache/inmemory/inMemoryCache.d.ts +35 -3
  62. package/cache/inmemory/inMemoryCache.js +74 -5
  63. package/cache/inmemory/inMemoryCache.js.map +1 -1
  64. package/cache/inmemory/policies.d.ts +4 -1
  65. package/cache/inmemory/policies.js +8 -1
  66. package/cache/inmemory/policies.js.map +1 -1
  67. package/cache/inmemory/readFromStore.js +12 -2
  68. package/cache/inmemory/readFromStore.js.map +1 -1
  69. package/cache/inmemory/types.d.ts +20 -4
  70. package/cache/inmemory/types.js.map +1 -1
  71. package/cache/inmemory/writeToStore.js +4 -4
  72. package/core/ApolloClient.js +2 -2
  73. package/core/ApolloClient.js.map +1 -1
  74. package/core/QueryInfo.js +40 -30
  75. package/core/QueryInfo.js.map +1 -1
  76. package/core/QueryManager.d.ts +1 -1
  77. package/core/QueryManager.js +2 -1
  78. package/core/QueryManager.js.map +1 -1
  79. package/core/index.d.ts +2 -2
  80. package/core/index.js +1 -1
  81. package/core/index.js.map +1 -1
  82. package/invariantErrorCodes.js +12 -7
  83. package/package.json +1 -1
  84. package/react/hooks/useLazyQuery.d.ts +13 -13
  85. package/react/hooks/useLazyQuery.js.map +1 -1
  86. package/react/hooks-compiled/useLazyQuery.d.ts +13 -13
  87. package/react/hooks-compiled/useLazyQuery.js.map +1 -1
  88. package/utilities/internal/getUnwrappedType.d.ts +8 -0
  89. package/utilities/internal/getUnwrappedType.js +17 -0
  90. package/utilities/internal/getUnwrappedType.js.map +1 -0
  91. package/utilities/internal/index.d.ts +2 -0
  92. package/utilities/internal/index.js +1 -0
  93. package/utilities/internal/index.js.map +1 -1
  94. package/utilities/internal/types/IsLooselyEqual.d.ts +10 -0
  95. package/utilities/internal/types/IsLooselyEqual.js +2 -0
  96. package/utilities/internal/types/IsLooselyEqual.js.map +1 -0
  97. package/version.js +1 -1
@@ -7,7 +7,15 @@ import type { KeyFieldsContext } from "./policies.js";
7
7
  import type { InMemoryCacheConfig, NormalizedCache } from "./types.js";
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.js").ApolloReducerConfig & {
11
+ resultCaching?: boolean;
12
+ possibleTypes?: import("./policies.js").PossibleTypesMap;
13
+ typePolicies?: import("./policies.js").TypePolicies;
14
+ fragments?: FragmentRegistryAPI;
15
+ inputObjects?: import("./types.js").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;
@@ -1,13 +1,31 @@
1
1
  import type { DocumentNode, FragmentDefinitionNode, InlineFragmentNode } from "graphql";
2
- import type { OperationVariables } from "@apollo/client";
2
+ import type { OperationVariables, TypedDocumentNode } from "@apollo/client";
3
3
  import type { DeepPartial, Reference, StoreObject } from "@apollo/client/utilities";
4
+ import type { IsLooselyEqual, NoInfer } from "@apollo/client/utilities/internal";
4
5
  import { getInMemoryCacheMemoryInternals } from "@apollo/client/utilities/internal";
5
6
  import { ApolloCache } from "../core/cache.js";
7
+ import type { Scalar } from "../core/Scalar.js";
6
8
  import type { Cache } from "../core/types/Cache.js";
7
9
  import { Policies } from "./policies.js";
8
10
  import { makeVar } from "./reactiveVars.js";
9
- import type { InMemoryCacheConfig, NormalizedCacheObject } from "./types.js";
11
+ import type { InMemoryCacheConfig, KnownScalars, NormalizedCacheObject } from "./types.js";
10
12
  type BroadcastOptions = Pick<Cache.BatchOptions<InMemoryCache>, "optimistic" | "onWatchUpdated">;
13
+ export declare namespace InMemoryCache {
14
+ type ScalarsOption = {
15
+ [ScalarName in keyof KnownScalars as IsLooselyEqual<KnownScalars[ScalarName]["serialized"], KnownScalars[ScalarName]["parsed"]> extends true ? ScalarName : never]?: KnownScalars[ScalarName] extends ({
16
+ serialized: infer TSerialized;
17
+ parsed: infer TParsed;
18
+ }) ? Scalar<TSerialized, TParsed> : never;
19
+ } & {
20
+ [ScalarName in keyof KnownScalars as IsLooselyEqual<KnownScalars[ScalarName]["serialized"], KnownScalars[ScalarName]["parsed"]> extends true ? never : ScalarName]: KnownScalars[ScalarName] extends ({
21
+ serialized: infer TSerialized;
22
+ parsed: infer TParsed;
23
+ }) ? Scalar<TSerialized, TParsed> : never;
24
+ } & (ApolloCache.Scalars extends (Record<string, {
25
+ serialized: infer TSerialized;
26
+ parsed: infer TParsed;
27
+ }>) ? Record<string, Scalar<TSerialized, TParsed>> : {});
28
+ }
11
29
  export declare class InMemoryCache extends ApolloCache {
12
30
  private data;
13
31
  private optimisticData;
@@ -20,9 +38,23 @@ export declare class InMemoryCache extends ApolloCache {
20
38
  readonly assumeImmutableResults = true;
21
39
  readonly policies: Policies;
22
40
  readonly makeVar: typeof makeVar;
23
- constructor(config?: InMemoryCacheConfig);
41
+ constructor(...args: {} extends InMemoryCache.ScalarsOption ? [
42
+ config?: InMemoryCacheConfig
43
+ ] : [config: InMemoryCacheConfig]);
24
44
  private init;
25
45
  private resetResultCache;
46
+ getScalar<TKey extends keyof ApolloCache.Scalars>(key: TKey): ApolloCache.GetScalarType<TKey> extends (Scalar<infer TSerialized, infer TParsed>) ? IsLooselyEqual<TSerialized, TParsed> extends true ? ApolloCache.GetScalarType<TKey> | undefined : ApolloCache.GetScalarType<TKey> : never;
47
+ /**
48
+ * Serializes scalar values in the variables object
49
+ */
50
+ serializeVariables<TVariables extends OperationVariables = OperationVariables>(document: DocumentNode | TypedDocumentNode<any, TVariables>, variables: NoInfer<TVariables>): TVariables;
51
+ /**
52
+ * Serializes scalar values in the variables object
53
+ */
54
+ serializeVariables<TVariables extends OperationVariables = OperationVariables>(document: DocumentNode | TypedDocumentNode<any, TVariables>, variables: NoInfer<TVariables> | undefined): TVariables | undefined;
55
+ private serializeVariablesValue;
56
+ private serializeInputArray;
57
+ private serializeInputObject;
26
58
  restore(data: NormalizedCacheObject): this;
27
59
  extract(optimistic?: boolean): NormalizedCacheObject;
28
60
  read<TData = unknown>(options: Cache.ReadOptions<TData, OperationVariables> & {
@@ -2,7 +2,7 @@ import { equal } from "@wry/equality";
2
2
  import { wrap } from "optimism";
3
3
  import { addTypenameToDocument, cacheSizes, canonicalStringify, DocumentTransform, isReference, print, } from "@apollo/client/utilities";
4
4
  import { __DEV__ } from "@apollo/client/utilities/environment";
5
- import { getInMemoryCacheMemoryInternals } from "@apollo/client/utilities/internal";
5
+ import { getInMemoryCacheMemoryInternals, getOperationDefinition, getUnwrappedType, isPlainObject, } from "@apollo/client/utilities/internal";
6
6
  import { invariant } from "@apollo/client/utilities/invariant";
7
7
  import { ApolloCache } from "../core/cache.js";
8
8
  import { EntityStore, supportsResultCaching } from "./entityStore.js";
@@ -28,9 +28,9 @@ export class InMemoryCache extends ApolloCache {
28
28
  // cache.policies.addPossibletypes.
29
29
  policies;
30
30
  makeVar = makeVar;
31
- constructor(config = {}) {
31
+ constructor(...args) {
32
32
  super();
33
- this.config = normalizeConfig(config);
33
+ this.config = normalizeConfig(args[0] ?? {});
34
34
  this.policies = new Policies({
35
35
  cache: this,
36
36
  dataIdFromObject: this.config.dataIdFromObject,
@@ -90,6 +90,68 @@ export class InMemoryCache extends ApolloCache {
90
90
  // all CacheGroup dependency information.
91
91
  new Set([this.data.group, this.optimisticData.group]).forEach((group) => group.resetCaching());
92
92
  }
93
+ getScalar(key) {
94
+ return this.config.scalars?.[key];
95
+ }
96
+ serializeVariables(document, variables) {
97
+ if (!variables ||
98
+ Object.keys(variables).length === 0 ||
99
+ (!this.config.scalars && !this.config.inputObjects)) {
100
+ return variables;
101
+ }
102
+ const variableTypes = getOperationDefinition(document)?.variableDefinitions?.reduce((memo, node) => {
103
+ memo[node.variable.name.value] = getUnwrappedType(node.type);
104
+ return memo;
105
+ }, {});
106
+ if (!variableTypes || Object.keys(variableTypes).length === 0) {
107
+ return variables;
108
+ }
109
+ return this.serializeVariablesValue(variables, variableTypes);
110
+ }
111
+ serializeVariablesValue(value, variableTypes, scalar) {
112
+ if (Array.isArray(value)) {
113
+ return this.serializeInputArray(value, variableTypes, scalar);
114
+ }
115
+ if (scalar && value != null) {
116
+ return scalar.coerceToSerialized(value);
117
+ }
118
+ if (isPlainObject(value)) {
119
+ return this.serializeInputObject(value, variableTypes);
120
+ }
121
+ return value;
122
+ }
123
+ serializeInputArray(value, variableTypes, scalar) {
124
+ let changed = false;
125
+ const newValue = value.map((item) => {
126
+ const newItem = this.serializeVariablesValue(item, variableTypes, scalar);
127
+ changed ||= newItem !== item;
128
+ return newItem;
129
+ });
130
+ return changed ? newValue : value;
131
+ }
132
+ serializeInputObject(value, variableTypes) {
133
+ let changed = false;
134
+ const entries = Object.entries(value).map(([name, value]) => {
135
+ const type = variableTypes[name];
136
+ if (!type) {
137
+ return [name, value];
138
+ }
139
+ const inputObject = this.config.inputObjects?.[type];
140
+ if (inputObject) {
141
+ const newValue = this.serializeVariablesValue(value, inputObject.fields);
142
+ changed ||= newValue !== value;
143
+ return [name, newValue];
144
+ }
145
+ const scalar = this.getScalar(type);
146
+ if (scalar) {
147
+ const newValue = this.serializeVariablesValue(value, variableTypes, scalar);
148
+ changed ||= newValue !== value;
149
+ return [name, newValue];
150
+ }
151
+ return [name, value];
152
+ });
153
+ return changed ? Object.fromEntries(entries) : value;
154
+ }
93
155
  restore(data) {
94
156
  this.init();
95
157
  // Since calling this.init() discards/replaces the entire StoreReader, along
@@ -103,7 +165,7 @@ export class InMemoryCache extends ApolloCache {
103
165
  return (optimistic ? this.optimisticData : this.data).extract();
104
166
  }
105
167
  read(options) {
106
- const {
168
+ const { query, variables,
107
169
  // Since read returns data or null, without any additional metadata
108
170
  // about whether/where there might have been missing fields, the
109
171
  // default behavior cannot be returnPartialData = true (like it is
@@ -114,15 +176,20 @@ export class InMemoryCache extends ApolloCache {
114
176
  returnPartialData = false, } = options;
115
177
  return this.storeReader.diffQueryAgainstStore({
116
178
  ...options,
179
+ variables: this.serializeVariables(query, variables),
117
180
  store: options.optimistic ? this.optimisticData : this.data,
118
181
  config: this.config,
119
182
  returnPartialData,
120
183
  }).result;
121
184
  }
122
185
  write(options) {
186
+ const { query, variables } = options;
123
187
  try {
124
188
  ++this.txCount;
125
- return this.storeWriter.writeToStore(this.data, options);
189
+ return this.storeWriter.writeToStore(this.data, {
190
+ ...options,
191
+ variables: this.serializeVariables(query, variables),
192
+ });
126
193
  }
127
194
  finally {
128
195
  if (!--this.txCount && options.broadcast !== false) {
@@ -158,8 +225,10 @@ export class InMemoryCache extends ApolloCache {
158
225
  }
159
226
  }
160
227
  diff(options) {
228
+ const { variables } = options;
161
229
  return this.storeReader.diffQueryAgainstStore({
162
230
  ...options,
231
+ variables: this.serializeVariables(options.query, variables),
163
232
  store: options.optimistic ? this.optimisticData : this.data,
164
233
  rootId: options.id || "ROOT_QUERY",
165
234
  config: this.config,