@apollo/client 4.3.7 → 4.4.0-alpha.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 (91) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/__cjs/cache/inmemory/policies.cjs +2 -2
  3. package/__cjs/cache/inmemory/policies.cjs.map +1 -1
  4. package/__cjs/cache/inmemory/policies.d.cts +1 -1
  5. package/__cjs/cache/inmemory/readFromStore.cjs +43 -5
  6. package/__cjs/cache/inmemory/readFromStore.cjs.map +1 -1
  7. package/__cjs/cache/inmemory/types.d.cts +2 -0
  8. package/__cjs/cache/inmemory/writeToStore.cjs +40 -13
  9. package/__cjs/cache/inmemory/writeToStore.cjs.map +1 -1
  10. package/__cjs/cache/inmemory/writeToStore.d.cts +4 -1
  11. package/__cjs/local-state/LocalState.cjs +12 -1
  12. package/__cjs/local-state/LocalState.cjs.map +1 -1
  13. package/__cjs/utilities/internal/argumentsObjectFromField.cjs.map +1 -1
  14. package/__cjs/utilities/internal/argumentsObjectFromField.d.cts +2 -2
  15. package/__cjs/utilities/internal/createFragmentArgumentNode.cjs +13 -0
  16. package/__cjs/utilities/internal/createFragmentArgumentNode.cjs.map +1 -0
  17. package/__cjs/utilities/internal/createFragmentArgumentNode.d.cts +3 -0
  18. package/__cjs/utilities/internal/createFragmentSpreadNode.cjs +17 -0
  19. package/__cjs/utilities/internal/createFragmentSpreadNode.cjs.map +1 -0
  20. package/__cjs/utilities/internal/createFragmentSpreadNode.d.cts +5 -0
  21. package/__cjs/utilities/internal/createNameNode.cjs +8 -0
  22. package/__cjs/utilities/internal/createNameNode.cjs.map +1 -0
  23. package/__cjs/utilities/internal/createNameNode.d.cts +3 -0
  24. package/__cjs/utilities/internal/createVariableNode.cjs +9 -0
  25. package/__cjs/utilities/internal/createVariableNode.cjs.map +1 -0
  26. package/__cjs/utilities/internal/createVariableNode.d.cts +3 -0
  27. package/__cjs/utilities/internal/equalByQuery.cjs +14 -1
  28. package/__cjs/utilities/internal/equalByQuery.cjs.map +1 -1
  29. package/__cjs/utilities/internal/getDefaultValues.cjs.map +1 -1
  30. package/__cjs/utilities/internal/getDefaultValues.d.cts +2 -2
  31. package/__cjs/utilities/internal/getFragmentQueryDocument.cjs +10 -7
  32. package/__cjs/utilities/internal/getFragmentQueryDocument.cjs.map +1 -1
  33. package/__cjs/utilities/internal/getFragmentVariables.cjs +34 -0
  34. package/__cjs/utilities/internal/getFragmentVariables.cjs.map +1 -0
  35. package/__cjs/utilities/internal/getFragmentVariables.d.cts +8 -0
  36. package/__cjs/utilities/internal/index.cjs +4 -2
  37. package/__cjs/utilities/internal/index.cjs.map +1 -1
  38. package/__cjs/utilities/internal/index.d.cts +1 -0
  39. package/__cjs/utilities/internal/removeDirectivesFromDocument.cjs +120 -61
  40. package/__cjs/utilities/internal/removeDirectivesFromDocument.cjs.map +1 -1
  41. package/__cjs/utilities/internal/types/Mutable.cjs +3 -0
  42. package/__cjs/utilities/internal/types/Mutable.cjs.map +1 -0
  43. package/__cjs/utilities/internal/types/Mutable.d.cts +4 -0
  44. package/__cjs/version.cjs +1 -1
  45. package/__cjs/version.cjs.map +1 -1
  46. package/cache/inmemory/policies.d.ts +1 -1
  47. package/cache/inmemory/policies.js +2 -2
  48. package/cache/inmemory/policies.js.map +1 -1
  49. package/cache/inmemory/readFromStore.js +44 -6
  50. package/cache/inmemory/readFromStore.js.map +1 -1
  51. package/cache/inmemory/types.d.ts +2 -0
  52. package/cache/inmemory/types.js.map +1 -1
  53. package/cache/inmemory/writeToStore.d.ts +4 -1
  54. package/cache/inmemory/writeToStore.js +41 -14
  55. package/cache/inmemory/writeToStore.js.map +1 -1
  56. package/local-state/LocalState.js +13 -2
  57. package/local-state/LocalState.js.map +1 -1
  58. package/package.json +2 -2
  59. package/utilities/internal/argumentsObjectFromField.d.ts +2 -2
  60. package/utilities/internal/argumentsObjectFromField.js.map +1 -1
  61. package/utilities/internal/createFragmentArgumentNode.d.ts +3 -0
  62. package/utilities/internal/createFragmentArgumentNode.js +10 -0
  63. package/utilities/internal/createFragmentArgumentNode.js.map +1 -0
  64. package/utilities/internal/createFragmentSpreadNode.d.ts +5 -0
  65. package/utilities/internal/createFragmentSpreadNode.js +14 -0
  66. package/utilities/internal/createFragmentSpreadNode.js.map +1 -0
  67. package/utilities/internal/createNameNode.d.ts +3 -0
  68. package/utilities/internal/createNameNode.js +5 -0
  69. package/utilities/internal/createNameNode.js.map +1 -0
  70. package/utilities/internal/createVariableNode.d.ts +3 -0
  71. package/utilities/internal/createVariableNode.js +6 -0
  72. package/utilities/internal/createVariableNode.js.map +1 -0
  73. package/utilities/internal/equalByQuery.js +14 -1
  74. package/utilities/internal/equalByQuery.js.map +1 -1
  75. package/utilities/internal/getDefaultValues.d.ts +2 -2
  76. package/utilities/internal/getDefaultValues.js.map +1 -1
  77. package/utilities/internal/getFragmentQueryDocument.js +10 -7
  78. package/utilities/internal/getFragmentQueryDocument.js.map +1 -1
  79. package/utilities/internal/getFragmentVariables.d.ts +8 -0
  80. package/utilities/internal/getFragmentVariables.js +31 -0
  81. package/utilities/internal/getFragmentVariables.js.map +1 -0
  82. package/utilities/internal/index.d.ts +1 -0
  83. package/utilities/internal/index.js +1 -0
  84. package/utilities/internal/index.js.map +1 -1
  85. package/utilities/internal/removeDirectivesFromDocument.js +120 -61
  86. package/utilities/internal/removeDirectivesFromDocument.js.map +1 -1
  87. package/utilities/internal/types/Mutable.d.ts +4 -0
  88. package/utilities/internal/types/Mutable.js +2 -0
  89. package/utilities/internal/types/Mutable.js.map +1 -0
  90. package/version.js +1 -1
  91. package/version.js.map +1 -1
@@ -14,6 +14,7 @@ import { isNonEmptyArray } from "./isNonEmptyArray.js";
14
14
  */
15
15
  export function removeDirectivesFromDocument(directives, doc) {
16
16
  checkDocument(doc);
17
+ const canUseFragmentArgs = Kind.FRAGMENT_ARGUMENT !== undefined;
17
18
  // Passing empty strings to makeInUseGetterFunction means we handle anonymous
18
19
  // operations as if their names were "". Anonymous fragment definitions are
19
20
  // not supposed to be possible, but the same default naming strategy seems
@@ -35,17 +36,10 @@ export function removeDirectivesFromDocument(directives, doc) {
35
36
  invariant.error(16);
36
37
  return null;
37
38
  };
38
- let operationCount = 0;
39
- for (let i = doc.definitions.length - 1; i >= 0; --i) {
40
- if (doc.definitions[i].kind === Kind.OPERATION_DEFINITION) {
41
- ++operationCount;
42
- }
43
- }
39
+ const hasOperation = doc.definitions.some((def) => def.kind === Kind.OPERATION_DEFINITION);
44
40
  const directiveMatcher = getDirectiveMatcher(directives);
45
41
  const shouldRemoveField = (nodeDirectives) => isNonEmptyArray(nodeDirectives) &&
46
- nodeDirectives
47
- .map(directiveMatcher)
48
- .some((config) => config && config.remove);
42
+ nodeDirectives.map(directiveMatcher).some((config) => config?.remove);
49
43
  const originalFragmentDefsByPath = new Map();
50
44
  // Any time the first traversal of the document below makes a change like
51
45
  // removing a fragment (by returning null), this variable should be set to
@@ -75,7 +69,16 @@ export function removeDirectivesFromDocument(directives, doc) {
75
69
  },
76
70
  },
77
71
  Variable: {
78
- enter(node, _key, _parent, _path, ancestors) {
72
+ enter(node, _key, parent, _path, ancestors) {
73
+ // Variables in ...spread args are tracked by FragmentSpread below since
74
+ // it's possible the variable might be removed if the spread arg is also
75
+ // removed.
76
+ if (canUseFragmentArgs &&
77
+ [parent, ...ancestors].some((ancestor) => ancestor &&
78
+ !isArray(ancestor) &&
79
+ ancestor.kind === Kind.FRAGMENT_ARGUMENT)) {
80
+ return;
81
+ }
79
82
  const inUse = getInUse(ancestors);
80
83
  if (inUse) {
81
84
  inUse.variables.add(node.name.value);
@@ -90,7 +93,17 @@ export function removeDirectivesFromDocument(directives, doc) {
90
93
  }
91
94
  const inUse = getInUse(ancestors);
92
95
  if (inUse) {
93
- inUse.fragmentSpreads.add(node.name.value);
96
+ const spreadArgs = inUse.fragmentSpreads.get(node.name.value) ?? [];
97
+ node.arguments?.forEach((arg) => {
98
+ const variables = new Set();
99
+ visit(arg, {
100
+ Variable(node) {
101
+ variables.add(node.name.value);
102
+ },
103
+ });
104
+ spreadArgs.push({ name: arg.name.value, variables });
105
+ });
106
+ inUse.fragmentSpreads.set(node.name.value, spreadArgs);
94
107
  }
95
108
  // We might like to remove this FragmentSpread by returning null here if
96
109
  // the corresponding FragmentDefinition node is also going to be removed
@@ -102,6 +115,11 @@ export function removeDirectivesFromDocument(directives, doc) {
102
115
  FragmentDefinition: {
103
116
  enter(node, _key, _parent, path) {
104
117
  originalFragmentDefsByPath.set(JSON.stringify(path), node);
118
+ if (canUseFragmentArgs) {
119
+ node.variableDefinitions?.forEach((def) => {
120
+ getInUseByFragmentName(node.name.value).declaredVariables.add(def.variable.name.value);
121
+ });
122
+ }
105
123
  },
106
124
  leave(node, _key, _parent, path) {
107
125
  const originalNode = originalFragmentDefsByPath.get(JSON.stringify(path));
@@ -118,7 +136,7 @@ export function removeDirectivesFromDocument(directives, doc) {
118
136
  // This logic applies only if the document contains one or more
119
137
  // operations, since removing all fragments from a document containing
120
138
  // only fragments makes the document useless.
121
- operationCount > 0 &&
139
+ hasOperation &&
122
140
  node.selectionSet.selections.every((selection) => selection.kind === Kind.FIELD &&
123
141
  selection.name.value === "__typename")) {
124
142
  // This is a somewhat opinionated choice: if a FragmentDefinition ends
@@ -150,20 +168,27 @@ export function removeDirectivesFromDocument(directives, doc) {
150
168
  // Utility for making sure inUse.transitiveVars is recursively populated.
151
169
  // Because this logic assumes inUse.fragmentSpreads has been completely
152
170
  // populated and inUse.removed has been set if appropriate,
153
- // populateTransitiveVars must be called after that information has been
171
+ // getUsedVariables must be called after that information has been
154
172
  // collected by the first traversal of the document.
155
- const populateTransitiveVars = (inUse) => {
173
+ const getUsedVariables = (inUse) => {
156
174
  if (!inUse.transitiveVars) {
157
- inUse.transitiveVars = new Set(inUse.variables);
158
- if (!inUse.removed) {
159
- inUse.fragmentSpreads.forEach((childFragmentName) => {
160
- populateTransitiveVars(getInUseByFragmentName(childFragmentName)).transitiveVars.forEach((varName) => {
161
- inUse.transitiveVars.add(varName);
162
- });
175
+ inUse.transitiveVars = new Set(Array.from(inUse.variables).filter(
176
+ // Variables declared by the fragment are local to that fragment so
177
+ // we don't treat them as transitive variables
178
+ (varName) => !inUse.declaredVariables.has(varName)));
179
+ inUse.fragmentSpreads.forEach((args, childFragmentName) => {
180
+ const childInUse = getInUseByFragmentName(childFragmentName);
181
+ if (childInUse.removed)
182
+ return;
183
+ getUsedVariables(childInUse).forEach((varName) => inUse.transitiveVars.add(varName));
184
+ args.forEach((arg) => {
185
+ if (isSpreadArgUsed(childFragmentName, arg.name)) {
186
+ arg.variables.forEach((varName) => inUse.transitiveVars.add(varName));
187
+ }
163
188
  });
164
- }
189
+ });
165
190
  }
166
- return inUse;
191
+ return inUse.transitiveVars;
167
192
  };
168
193
  // Since we've been keeping track of fragment spreads used by particular
169
194
  // operations and fragment definitions, we now need to compute the set of all
@@ -171,7 +196,7 @@ export function removeDirectivesFromDocument(directives, doc) {
171
196
  const allFragmentNamesUsed = new Set();
172
197
  docWithoutDirectiveSubtrees.definitions.forEach((def) => {
173
198
  if (def.kind === Kind.OPERATION_DEFINITION) {
174
- populateTransitiveVars(getInUseByOperationName(def.name && def.name.value)).fragmentSpreads.forEach((childFragmentName) => {
199
+ getInUseByOperationName(def.name && def.name.value).fragmentSpreads.forEach((_, childFragmentName) => {
175
200
  allFragmentNamesUsed.add(childFragmentName);
176
201
  });
177
202
  }
@@ -180,7 +205,7 @@ export function removeDirectivesFromDocument(directives, doc) {
180
205
  // definitions count as usages of their own fragment names. This heuristic
181
206
  // prevents accidentally removing all fragment definitions from the
182
207
  // document just because it contains no operations that use the fragments.
183
- operationCount === 0 &&
208
+ !hasOperation &&
184
209
  !getInUseByFragmentName(def.name.value).removed) {
185
210
  allFragmentNamesUsed.add(def.name.value);
186
211
  }
@@ -191,37 +216,76 @@ export function removeDirectivesFromDocument(directives, doc) {
191
216
  allFragmentNamesUsed.forEach((fragmentName) => {
192
217
  // Once all the childFragmentName strings added here have been seen already,
193
218
  // the top-level allFragmentNamesUsed.forEach loop will terminate.
194
- populateTransitiveVars(getInUseByFragmentName(fragmentName)).fragmentSpreads.forEach((childFragmentName) => {
219
+ getInUseByFragmentName(fragmentName).fragmentSpreads.forEach((_, childFragmentName) => {
195
220
  allFragmentNamesUsed.add(childFragmentName);
196
221
  });
197
222
  });
198
- const fragmentWillBeRemoved = (fragmentName) => !!(
223
+ const fragmentWillBeRemoved = (fragmentName) =>
199
224
  // A fragment definition will be removed if there are no spreads that refer
200
225
  // to it, or the fragment was explicitly removed because it had no fields
201
226
  // other than __typename.
202
- (!allFragmentNamesUsed.has(fragmentName) ||
203
- getInUseByFragmentName(fragmentName).removed));
204
- const enterVisitor = {
205
- enter(node) {
206
- if (fragmentWillBeRemoved(node.name.value)) {
207
- return null;
208
- }
209
- },
210
- };
227
+ !allFragmentNamesUsed.has(fragmentName) ||
228
+ getInUseByFragmentName(fragmentName).removed;
229
+ function isFragmentVariableUsed(fragmentName, varName, seen = new Set()) {
230
+ const inUse = getInUseByFragmentName(fragmentName);
231
+ return (inUse.variables.has(varName) ||
232
+ Array.from(inUse.fragmentSpreads.entries()).some(([fragmentName, args]) => {
233
+ return args.some((arg) => arg.variables.has(varName) &&
234
+ isSpreadArgUsed(fragmentName, arg.name, seen));
235
+ }));
236
+ }
237
+ function isSpreadArgUsed(fragmentName, argName, seen = new Set()) {
238
+ const key = `${fragmentName}:${argName}`;
239
+ if (seen.has(key))
240
+ return false;
241
+ seen.add(key);
242
+ const inUse = getInUseByFragmentName(fragmentName);
243
+ return (!inUse.removed &&
244
+ inUse.declaredVariables.has(argName) &&
245
+ isFragmentVariableUsed(fragmentName, argName, seen));
246
+ }
211
247
  return nullIfDocIsEmpty(visit(docWithoutDirectiveSubtrees, {
212
248
  // If the fragment is going to be removed, then leaving any dangling
213
249
  // FragmentSpread nodes with the same name would be a mistake.
214
- FragmentSpread: enterVisitor,
250
+ FragmentSpread: {
251
+ enter(node) {
252
+ if (fragmentWillBeRemoved(node.name.value)) {
253
+ return null;
254
+ }
255
+ if (node.arguments) {
256
+ return {
257
+ ...node,
258
+ arguments: node.arguments.filter((arg) => {
259
+ return isSpreadArgUsed(node.name.value, arg.name.value);
260
+ }),
261
+ };
262
+ }
263
+ },
264
+ },
215
265
  // This is where the fragment definition is actually removed.
216
- FragmentDefinition: enterVisitor,
266
+ FragmentDefinition: {
267
+ enter(node) {
268
+ if (fragmentWillBeRemoved(node.name.value)) {
269
+ return null;
270
+ }
271
+ if (node.variableDefinitions) {
272
+ return {
273
+ ...node,
274
+ variableDefinitions: node.variableDefinitions.filter((def) => {
275
+ return isFragmentVariableUsed(node.name.value, def.variable.name.value);
276
+ }),
277
+ };
278
+ }
279
+ },
280
+ },
217
281
  OperationDefinition: {
218
282
  leave(node) {
219
283
  // Upon leaving each operation in the depth-first AST traversal, prune
220
284
  // any variables that are declared by the operation but unused within.
221
285
  if (node.variableDefinitions) {
222
- const usedVariableNames = populateTransitiveVars(
286
+ const usedVariableNames = getUsedVariables(
223
287
  // If an operation is anonymous, we use the empty string as its key.
224
- getInUseByOperationName(node.name && node.name.value)).transitiveVars;
288
+ getInUseByOperationName(node.name && node.name.value));
225
289
  // According to the GraphQL spec, all variables declared by an
226
290
  // operation must either be used by that operation or used by some
227
291
  // fragment included transitively into that operation:
@@ -230,15 +294,11 @@ export function removeDirectivesFromDocument(directives, doc) {
230
294
  // To stay on the right side of this validation rule, if/when we
231
295
  // remove the last $var references from an operation or its fragments,
232
296
  // we must also remove the corresponding $var declaration from the
233
- // enclosing operation. This pruning applies only to operations and
234
- // not fragment definitions, at the moment. Fragments may be able to
235
- // declare variables eventually, but today they can only consume them.
236
- if (usedVariableNames.size < node.variableDefinitions.length) {
237
- return {
238
- ...node,
239
- variableDefinitions: node.variableDefinitions.filter((varDef) => usedVariableNames.has(varDef.variable.name.value)),
240
- };
241
- }
297
+ // enclosing operation.
298
+ return {
299
+ ...node,
300
+ variableDefinitions: node.variableDefinitions.filter((varDef) => usedVariableNames.has(varDef.variable.name.value)),
301
+ };
242
302
  }
243
303
  },
244
304
  },
@@ -250,12 +310,13 @@ function makeInUseGetterFunction(defaultKey) {
250
310
  let inUse = map.get(key);
251
311
  if (!inUse) {
252
312
  map.set(key, (inUse = {
253
- // Variable and fragment spread names used directly within this
254
- // operation or fragment definition, as identified by key. These sets
255
- // will be populated during the first traversal of the document in
256
- // removeDirectivesFromDocument below.
313
+ // Variable names, fragment spreads, and declared variable names used
314
+ // directly within this operation or fragment definition, as
315
+ // identified by key. These will be populated during the first
316
+ // traversal of the document in removeDirectivesFromDocument below.
257
317
  variables: new Set(),
258
- fragmentSpreads: new Set(),
318
+ fragmentSpreads: new Map(),
319
+ declaredVariables: new Set(),
259
320
  }));
260
321
  }
261
322
  return inUse;
@@ -264,14 +325,12 @@ function makeInUseGetterFunction(defaultKey) {
264
325
  function getDirectiveMatcher(configs) {
265
326
  const names = new Map();
266
327
  const tests = new Map();
267
- configs.forEach((directive) => {
268
- if (directive) {
269
- if (directive.name) {
270
- names.set(directive.name, directive);
271
- }
272
- else if (directive.test) {
273
- tests.set(directive.test, directive);
274
- }
328
+ configs.forEach((config) => {
329
+ if (config.name) {
330
+ names.set(config.name, config);
331
+ }
332
+ else if (config.test) {
333
+ tests.set(config.test, config);
275
334
  }
276
335
  });
277
336
  return (directive) => {