@apollo/client 3.7.8 → 3.7.10

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 (43) hide show
  1. package/README.md +1 -1
  2. package/apollo-client.cjs +186 -168
  3. package/apollo-client.cjs.map +1 -1
  4. package/apollo-client.min.cjs +1 -1
  5. package/cache/cache.cjs +20 -21
  6. package/cache/cache.cjs.map +1 -1
  7. package/cache/cache.cjs.native.js +20 -21
  8. package/cache/core/types/DataProxy.d.ts +3 -2
  9. package/cache/core/types/DataProxy.d.ts.map +1 -1
  10. package/cache/core/types/DataProxy.js.map +1 -1
  11. package/cache/inmemory/helpers.d.ts +2 -2
  12. package/cache/inmemory/helpers.d.ts.map +1 -1
  13. package/cache/inmemory/helpers.js +2 -2
  14. package/cache/inmemory/helpers.js.map +1 -1
  15. package/core/ApolloClient.d.ts +3 -3
  16. package/core/ApolloClient.d.ts.map +1 -1
  17. package/core/ApolloClient.js +10 -4
  18. package/core/ApolloClient.js.map +1 -1
  19. package/core/core.cjs +11 -5
  20. package/core/core.cjs.map +1 -1
  21. package/core/core.cjs.native.js +11 -5
  22. package/invariantErrorCodes.js +1 -1
  23. package/package.json +8 -8
  24. package/react/hooks/hooks.cjs +2 -1
  25. package/react/hooks/hooks.cjs.map +1 -1
  26. package/react/hooks/hooks.cjs.native.js +2 -1
  27. package/react/hooks/useMutation.d.ts.map +1 -1
  28. package/react/hooks/useMutation.js +2 -1
  29. package/react/hooks/useMutation.js.map +1 -1
  30. package/utilities/common/arrays.d.ts +1 -0
  31. package/utilities/common/arrays.d.ts.map +1 -1
  32. package/utilities/common/arrays.js +1 -0
  33. package/utilities/common/arrays.js.map +1 -1
  34. package/utilities/graphql/getFromAST.d.ts.map +1 -1
  35. package/utilities/graphql/getFromAST.js +7 -3
  36. package/utilities/graphql/getFromAST.js.map +1 -1
  37. package/utilities/graphql/transform.d.ts.map +1 -1
  38. package/utilities/graphql/transform.js +167 -90
  39. package/utilities/graphql/transform.js.map +1 -1
  40. package/utilities/utilities.cjs +175 -103
  41. package/utilities/utilities.cjs.map +1 -1
  42. package/utilities/utilities.cjs.native.js +175 -103
  43. package/version.js +1 -1
@@ -364,17 +364,21 @@ function checkDocument(doc) {
364
364
  }
365
365
  function getOperationDefinition(doc) {
366
366
  checkDocument(doc);
367
- return doc.definitions.filter(function (definition) { return definition.kind === 'OperationDefinition'; })[0];
367
+ return doc.definitions.filter(function (definition) {
368
+ return definition.kind === 'OperationDefinition';
369
+ })[0];
368
370
  }
369
371
  function getOperationName(doc) {
370
372
  return (doc.definitions
371
373
  .filter(function (definition) {
372
- return definition.kind === 'OperationDefinition' && definition.name;
374
+ return definition.kind === 'OperationDefinition' && !!definition.name;
373
375
  })
374
376
  .map(function (x) { return x.name.value; })[0] || null);
375
377
  }
376
378
  function getFragmentDefinitions(doc) {
377
- return doc.definitions.filter(function (definition) { return definition.kind === 'FragmentDefinition'; });
379
+ return doc.definitions.filter(function (definition) {
380
+ return definition.kind === 'FragmentDefinition';
381
+ });
378
382
  }
379
383
  function getQueryDefinition(doc) {
380
384
  var queryDef = getOperationDefinition(doc);
@@ -423,26 +427,20 @@ function getDefaultValues(definition) {
423
427
  return defaultValues;
424
428
  }
425
429
 
426
- function filterInPlace(array, test, context) {
427
- var target = 0;
428
- array.forEach(function (elem, i) {
429
- if (test.call(this, elem, i, array)) {
430
- array[target++] = elem;
431
- }
432
- }, context);
433
- array.length = target;
434
- return array;
430
+ var isArray = Array.isArray;
431
+ function isNonEmptyArray(value) {
432
+ return Array.isArray(value) && value.length > 0;
435
433
  }
436
434
 
437
435
  var TYPENAME_FIELD = {
438
- kind: 'Field',
436
+ kind: graphql.Kind.FIELD,
439
437
  name: {
440
- kind: 'Name',
438
+ kind: graphql.Kind.NAME,
441
439
  value: '__typename',
442
440
  },
443
441
  };
444
442
  function isEmpty(op, fragmentMap) {
445
- return !op || op.selectionSet.selections.every(function (selection) { return selection.kind === 'FragmentSpread' &&
443
+ return !op || op.selectionSet.selections.every(function (selection) { return selection.kind === graphql.Kind.FRAGMENT_SPREAD &&
446
444
  isEmpty(fragmentMap[selection.name.value], fragmentMap); });
447
445
  }
448
446
  function nullIfDocIsEmpty(doc) {
@@ -451,84 +449,190 @@ function nullIfDocIsEmpty(doc) {
451
449
  : doc;
452
450
  }
453
451
  function getDirectiveMatcher(directives) {
454
- return function directiveMatcher(directive) {
455
- return directives.some(function (dir) {
456
- return (dir.name && dir.name === directive.name.value) ||
457
- (dir.test && dir.test(directive));
458
- });
452
+ var nameSet = new Set();
453
+ var tests = [];
454
+ directives.forEach(function (directive) {
455
+ if (directive.name) {
456
+ nameSet.add(directive.name);
457
+ }
458
+ else if (directive.test) {
459
+ tests.push(directive.test);
460
+ }
461
+ });
462
+ return function (directive) { return (nameSet.has(directive.name.value) ||
463
+ tests.some(function (test) { return test(directive); })); };
464
+ }
465
+ function makeInUseGetterFunction(defaultKey) {
466
+ var map = new Map();
467
+ return function inUseGetterFunction(key) {
468
+ if (key === void 0) { key = defaultKey; }
469
+ var inUse = map.get(key);
470
+ if (!inUse) {
471
+ map.set(key, inUse = {
472
+ variables: new Set,
473
+ fragmentSpreads: new Set,
474
+ });
475
+ }
476
+ return inUse;
459
477
  };
460
478
  }
461
479
  function removeDirectivesFromDocument(directives, doc) {
462
- var variablesInUse = Object.create(null);
463
- var variablesToRemove = [];
464
- var fragmentSpreadsInUse = Object.create(null);
465
- var fragmentSpreadsToRemove = [];
466
- var modifiedDoc = nullIfDocIsEmpty(graphql.visit(doc, {
480
+ var getInUseByOperationName = makeInUseGetterFunction("");
481
+ var getInUseByFragmentName = makeInUseGetterFunction("");
482
+ var getInUse = function (ancestors) {
483
+ for (var p = 0, ancestor = void 0; p < ancestors.length && (ancestor = ancestors[p]); ++p) {
484
+ if (isArray(ancestor))
485
+ continue;
486
+ if (ancestor.kind === graphql.Kind.OPERATION_DEFINITION) {
487
+ return getInUseByOperationName(ancestor.name && ancestor.name.value);
488
+ }
489
+ if (ancestor.kind === graphql.Kind.FRAGMENT_DEFINITION) {
490
+ return getInUseByFragmentName(ancestor.name.value);
491
+ }
492
+ }
493
+ __DEV__ && globals.invariant.error("Could not find operation or fragment");
494
+ return null;
495
+ };
496
+ var operationCount = 0;
497
+ for (var i = doc.definitions.length - 1; i >= 0; --i) {
498
+ if (doc.definitions[i].kind === graphql.Kind.OPERATION_DEFINITION) {
499
+ ++operationCount;
500
+ }
501
+ }
502
+ var directiveMatcher = getDirectiveMatcher(directives);
503
+ var hasRemoveDirective = directives.some(function (directive) { return directive.remove; });
504
+ var shouldRemoveField = function (nodeDirectives) { return (hasRemoveDirective &&
505
+ nodeDirectives &&
506
+ nodeDirectives.some(directiveMatcher)); };
507
+ var originalFragmentDefsByPath = new Map();
508
+ var firstVisitMadeChanges = false;
509
+ var fieldOrInlineFragmentVisitor = {
510
+ enter: function (node) {
511
+ if (shouldRemoveField(node.directives)) {
512
+ firstVisitMadeChanges = true;
513
+ return null;
514
+ }
515
+ },
516
+ };
517
+ var docWithoutDirectiveSubtrees = graphql.visit(doc, {
518
+ Field: fieldOrInlineFragmentVisitor,
519
+ InlineFragment: fieldOrInlineFragmentVisitor,
520
+ VariableDefinition: {
521
+ enter: function () {
522
+ return false;
523
+ },
524
+ },
467
525
  Variable: {
468
- enter: function (node, _key, parent) {
469
- if (parent.kind !== 'VariableDefinition') {
470
- variablesInUse[node.name.value] = true;
526
+ enter: function (node, _key, _parent, _path, ancestors) {
527
+ var inUse = getInUse(ancestors);
528
+ if (inUse) {
529
+ inUse.variables.add(node.name.value);
471
530
  }
472
531
  },
473
532
  },
474
- Field: {
475
- enter: function (node) {
476
- if (directives && node.directives) {
477
- var shouldRemoveField = directives.some(function (directive) { return directive.remove; });
478
- if (shouldRemoveField &&
479
- node.directives &&
480
- node.directives.some(getDirectiveMatcher(directives))) {
481
- if (node.arguments) {
482
- node.arguments.forEach(function (arg) {
483
- if (arg.value.kind === 'Variable') {
484
- variablesToRemove.push({
485
- name: arg.value.name.value,
486
- });
487
- }
488
- });
489
- }
490
- if (node.selectionSet) {
491
- getAllFragmentSpreadsFromSelectionSet(node.selectionSet).forEach(function (frag) {
492
- fragmentSpreadsToRemove.push({
493
- name: frag.name.value,
494
- });
495
- });
496
- }
497
- return null;
498
- }
533
+ FragmentSpread: {
534
+ enter: function (node, _key, _parent, _path, ancestors) {
535
+ if (shouldRemoveField(node.directives)) {
536
+ firstVisitMadeChanges = true;
537
+ return null;
538
+ }
539
+ var inUse = getInUse(ancestors);
540
+ if (inUse) {
541
+ inUse.fragmentSpreads.add(node.name.value);
499
542
  }
500
543
  },
501
544
  },
502
- FragmentSpread: {
503
- enter: function (node) {
504
- fragmentSpreadsInUse[node.name.value] = true;
545
+ FragmentDefinition: {
546
+ enter: function (node, _key, _parent, path) {
547
+ originalFragmentDefsByPath.set(JSON.stringify(path), node);
548
+ },
549
+ leave: function (node, _key, _parent, path) {
550
+ var originalNode = originalFragmentDefsByPath.get(JSON.stringify(path));
551
+ if (node === originalNode) {
552
+ return node;
553
+ }
554
+ if (operationCount > 0 &&
555
+ node.selectionSet.selections.every(function (selection) { return (selection.kind === graphql.Kind.FIELD &&
556
+ selection.name.value === '__typename'); })) {
557
+ getInUseByFragmentName(node.name.value).removed = true;
558
+ firstVisitMadeChanges = true;
559
+ return null;
560
+ }
505
561
  },
506
562
  },
507
563
  Directive: {
508
- enter: function (node) {
509
- if (getDirectiveMatcher(directives)(node)) {
564
+ leave: function (node) {
565
+ if (directiveMatcher(node)) {
566
+ firstVisitMadeChanges = true;
510
567
  return null;
511
568
  }
512
569
  },
513
570
  },
514
- }));
515
- if (modifiedDoc &&
516
- filterInPlace(variablesToRemove, function (v) { return !!v.name && !variablesInUse[v.name]; }).length) {
517
- modifiedDoc = removeArgumentsFromDocument(variablesToRemove, modifiedDoc);
518
- }
519
- if (modifiedDoc &&
520
- filterInPlace(fragmentSpreadsToRemove, function (fs) { return !!fs.name && !fragmentSpreadsInUse[fs.name]; })
521
- .length) {
522
- modifiedDoc = removeFragmentSpreadFromDocument(fragmentSpreadsToRemove, modifiedDoc);
571
+ });
572
+ if (!firstVisitMadeChanges) {
573
+ return doc;
523
574
  }
524
- return modifiedDoc;
575
+ var populateTransitiveVars = function (inUse) {
576
+ if (!inUse.transitiveVars) {
577
+ inUse.transitiveVars = new Set(inUse.variables);
578
+ if (!inUse.removed) {
579
+ inUse.fragmentSpreads.forEach(function (childFragmentName) {
580
+ populateTransitiveVars(getInUseByFragmentName(childFragmentName)).transitiveVars.forEach(function (varName) {
581
+ inUse.transitiveVars.add(varName);
582
+ });
583
+ });
584
+ }
585
+ }
586
+ return inUse;
587
+ };
588
+ var allFragmentNamesUsed = new Set();
589
+ docWithoutDirectiveSubtrees.definitions.forEach(function (def) {
590
+ if (def.kind === graphql.Kind.OPERATION_DEFINITION) {
591
+ populateTransitiveVars(getInUseByOperationName(def.name && def.name.value)).fragmentSpreads.forEach(function (childFragmentName) {
592
+ allFragmentNamesUsed.add(childFragmentName);
593
+ });
594
+ }
595
+ else if (def.kind === graphql.Kind.FRAGMENT_DEFINITION &&
596
+ operationCount === 0 &&
597
+ !getInUseByFragmentName(def.name.value).removed) {
598
+ allFragmentNamesUsed.add(def.name.value);
599
+ }
600
+ });
601
+ allFragmentNamesUsed.forEach(function (fragmentName) {
602
+ populateTransitiveVars(getInUseByFragmentName(fragmentName)).fragmentSpreads.forEach(function (childFragmentName) {
603
+ allFragmentNamesUsed.add(childFragmentName);
604
+ });
605
+ });
606
+ var fragmentWillBeRemoved = function (fragmentName) { return !!(!allFragmentNamesUsed.has(fragmentName) ||
607
+ getInUseByFragmentName(fragmentName).removed); };
608
+ var enterVisitor = {
609
+ enter: function (node) {
610
+ if (fragmentWillBeRemoved(node.name.value)) {
611
+ return null;
612
+ }
613
+ },
614
+ };
615
+ return nullIfDocIsEmpty(graphql.visit(docWithoutDirectiveSubtrees, {
616
+ FragmentSpread: enterVisitor,
617
+ FragmentDefinition: enterVisitor,
618
+ OperationDefinition: {
619
+ leave: function (node) {
620
+ if (node.variableDefinitions) {
621
+ var usedVariableNames_1 = populateTransitiveVars(getInUseByOperationName(node.name && node.name.value)).transitiveVars;
622
+ if (usedVariableNames_1.size < node.variableDefinitions.length) {
623
+ return tslib.__assign(tslib.__assign({}, node), { variableDefinitions: node.variableDefinitions.filter(function (varDef) { return usedVariableNames_1.has(varDef.variable.name.value); }) });
624
+ }
625
+ }
626
+ },
627
+ },
628
+ }));
525
629
  }
526
630
  var addTypenameToDocument = Object.assign(function (doc) {
527
631
  return graphql.visit(doc, {
528
632
  SelectionSet: {
529
633
  enter: function (node, _key, parent) {
530
634
  if (parent &&
531
- parent.kind === 'OperationDefinition') {
635
+ parent.kind === graphql.Kind.OPERATION_DEFINITION) {
532
636
  return;
533
637
  }
534
638
  var selections = node.selections;
@@ -578,7 +682,7 @@ function getArgumentMatcher(config) {
578
682
  return function argumentMatcher(argument) {
579
683
  return config.some(function (aConfig) {
580
684
  return argument.value &&
581
- argument.value.kind === 'Variable' &&
685
+ argument.value.kind === graphql.Kind.VARIABLE &&
582
686
  argument.value.name &&
583
687
  (aConfig.name === argument.value.name.value ||
584
688
  (aConfig.test && aConfig.test(argument)));
@@ -633,19 +737,6 @@ function removeFragmentSpreadFromDocument(config, doc) {
633
737
  FragmentDefinition: { enter: enter },
634
738
  }));
635
739
  }
636
- function getAllFragmentSpreadsFromSelectionSet(selectionSet) {
637
- var allFragments = [];
638
- selectionSet.selections.forEach(function (selection) {
639
- if ((isField(selection) || isInlineFragment(selection)) &&
640
- selection.selectionSet) {
641
- getAllFragmentSpreadsFromSelectionSet(selection.selectionSet).forEach(function (frag) { return allFragments.push(frag); });
642
- }
643
- else if (selection.kind === 'FragmentSpread') {
644
- allFragments.push(selection);
645
- }
646
- });
647
- return allFragments;
648
- }
649
740
  function buildQueryFromSelectionSet(document) {
650
741
  var definition = getMainDefinition(document);
651
742
  var definitionOperation = definition.operation;
@@ -669,22 +760,6 @@ function removeClientSetsFromDocument(document) {
669
760
  remove: true,
670
761
  },
671
762
  ], document);
672
- if (modifiedDoc) {
673
- modifiedDoc = graphql.visit(modifiedDoc, {
674
- FragmentDefinition: {
675
- enter: function (node) {
676
- if (node.selectionSet) {
677
- var isTypenameOnly = node.selectionSet.selections.every(function (selection) {
678
- return isField(selection) && selection.name.value === '__typename';
679
- });
680
- if (isTypenameOnly) {
681
- return null;
682
- }
683
- }
684
- },
685
- },
686
- });
687
- }
688
763
  return modifiedDoc;
689
764
  }
690
765
 
@@ -1181,10 +1256,6 @@ var Concast = (function (_super) {
1181
1256
  }(zenObservableTs.Observable));
1182
1257
  fixObservableSubclass(Concast);
1183
1258
 
1184
- function isNonEmptyArray(value) {
1185
- return Array.isArray(value) && value.length > 0;
1186
- }
1187
-
1188
1259
  function isExecutionPatchIncrementalResult(value) {
1189
1260
  return "incremental" in value;
1190
1261
  }
@@ -1287,6 +1358,7 @@ exports.hasAllDirectives = hasAllDirectives;
1287
1358
  exports.hasAnyDirectives = hasAnyDirectives;
1288
1359
  exports.hasClientExports = hasClientExports;
1289
1360
  exports.hasDirectives = hasDirectives;
1361
+ exports.isArray = isArray;
1290
1362
  exports.isDocumentNode = isDocumentNode;
1291
1363
  exports.isField = isField;
1292
1364
  exports.isInlineFragment = isInlineFragment;
package/version.js CHANGED
@@ -1,2 +1,2 @@
1
- export var version = '3.7.8';
1
+ export var version = '3.7.10';
2
2
  //# sourceMappingURL=version.js.map