@apollo/client 3.7.8 → 3.7.9

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.
@@ -423,26 +423,20 @@ function getDefaultValues(definition) {
423
423
  return defaultValues;
424
424
  }
425
425
 
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;
426
+ var isArray = Array.isArray;
427
+ function isNonEmptyArray(value) {
428
+ return Array.isArray(value) && value.length > 0;
435
429
  }
436
430
 
437
431
  var TYPENAME_FIELD = {
438
- kind: 'Field',
432
+ kind: graphql.Kind.FIELD,
439
433
  name: {
440
- kind: 'Name',
434
+ kind: graphql.Kind.NAME,
441
435
  value: '__typename',
442
436
  },
443
437
  };
444
438
  function isEmpty(op, fragmentMap) {
445
- return !op || op.selectionSet.selections.every(function (selection) { return selection.kind === 'FragmentSpread' &&
439
+ return !op || op.selectionSet.selections.every(function (selection) { return selection.kind === graphql.Kind.FRAGMENT_SPREAD &&
446
440
  isEmpty(fragmentMap[selection.name.value], fragmentMap); });
447
441
  }
448
442
  function nullIfDocIsEmpty(doc) {
@@ -451,84 +445,190 @@ function nullIfDocIsEmpty(doc) {
451
445
  : doc;
452
446
  }
453
447
  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
- });
448
+ var nameSet = new Set();
449
+ var tests = [];
450
+ directives.forEach(function (directive) {
451
+ if (directive.name) {
452
+ nameSet.add(directive.name);
453
+ }
454
+ else if (directive.test) {
455
+ tests.push(directive.test);
456
+ }
457
+ });
458
+ return function (directive) { return (nameSet.has(directive.name.value) ||
459
+ tests.some(function (test) { return test(directive); })); };
460
+ }
461
+ function makeInUseGetterFunction(defaultKey) {
462
+ var map = new Map();
463
+ return function inUseGetterFunction(key) {
464
+ if (key === void 0) { key = defaultKey; }
465
+ var inUse = map.get(key);
466
+ if (!inUse) {
467
+ map.set(key, inUse = {
468
+ variables: new Set,
469
+ fragmentSpreads: new Set,
470
+ });
471
+ }
472
+ return inUse;
459
473
  };
460
474
  }
461
475
  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, {
476
+ var getInUseByOperationName = makeInUseGetterFunction("");
477
+ var getInUseByFragmentName = makeInUseGetterFunction("");
478
+ var getInUse = function (ancestors) {
479
+ for (var p = 0, ancestor = void 0; p < ancestors.length && (ancestor = ancestors[p]); ++p) {
480
+ if (isArray(ancestor))
481
+ continue;
482
+ if (ancestor.kind === graphql.Kind.OPERATION_DEFINITION) {
483
+ return getInUseByOperationName(ancestor.name && ancestor.name.value);
484
+ }
485
+ if (ancestor.kind === graphql.Kind.FRAGMENT_DEFINITION) {
486
+ return getInUseByFragmentName(ancestor.name.value);
487
+ }
488
+ }
489
+ __DEV__ && globals.invariant.error("Could not find operation or fragment");
490
+ return null;
491
+ };
492
+ var operationCount = 0;
493
+ for (var i = doc.definitions.length - 1; i >= 0; --i) {
494
+ if (doc.definitions[i].kind === graphql.Kind.OPERATION_DEFINITION) {
495
+ ++operationCount;
496
+ }
497
+ }
498
+ var directiveMatcher = getDirectiveMatcher(directives);
499
+ var hasRemoveDirective = directives.some(function (directive) { return directive.remove; });
500
+ var shouldRemoveField = function (nodeDirectives) { return (hasRemoveDirective &&
501
+ nodeDirectives &&
502
+ nodeDirectives.some(directiveMatcher)); };
503
+ var originalFragmentDefsByPath = new Map();
504
+ var firstVisitMadeChanges = false;
505
+ var fieldOrInlineFragmentVisitor = {
506
+ enter: function (node) {
507
+ if (shouldRemoveField(node.directives)) {
508
+ firstVisitMadeChanges = true;
509
+ return null;
510
+ }
511
+ },
512
+ };
513
+ var docWithoutDirectiveSubtrees = graphql.visit(doc, {
514
+ Field: fieldOrInlineFragmentVisitor,
515
+ InlineFragment: fieldOrInlineFragmentVisitor,
516
+ VariableDefinition: {
517
+ enter: function () {
518
+ return false;
519
+ },
520
+ },
467
521
  Variable: {
468
- enter: function (node, _key, parent) {
469
- if (parent.kind !== 'VariableDefinition') {
470
- variablesInUse[node.name.value] = true;
522
+ enter: function (node, _key, _parent, _path, ancestors) {
523
+ var inUse = getInUse(ancestors);
524
+ if (inUse) {
525
+ inUse.variables.add(node.name.value);
471
526
  }
472
527
  },
473
528
  },
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
- }
529
+ FragmentSpread: {
530
+ enter: function (node, _key, _parent, _path, ancestors) {
531
+ if (shouldRemoveField(node.directives)) {
532
+ firstVisitMadeChanges = true;
533
+ return null;
534
+ }
535
+ var inUse = getInUse(ancestors);
536
+ if (inUse) {
537
+ inUse.fragmentSpreads.add(node.name.value);
499
538
  }
500
539
  },
501
540
  },
502
- FragmentSpread: {
503
- enter: function (node) {
504
- fragmentSpreadsInUse[node.name.value] = true;
541
+ FragmentDefinition: {
542
+ enter: function (node, _key, _parent, path) {
543
+ originalFragmentDefsByPath.set(JSON.stringify(path), node);
544
+ },
545
+ leave: function (node, _key, _parent, path) {
546
+ var originalNode = originalFragmentDefsByPath.get(JSON.stringify(path));
547
+ if (node === originalNode) {
548
+ return node;
549
+ }
550
+ if (operationCount > 0 &&
551
+ node.selectionSet.selections.every(function (selection) { return (selection.kind === graphql.Kind.FIELD &&
552
+ selection.name.value === '__typename'); })) {
553
+ getInUseByFragmentName(node.name.value).removed = true;
554
+ firstVisitMadeChanges = true;
555
+ return null;
556
+ }
505
557
  },
506
558
  },
507
559
  Directive: {
508
- enter: function (node) {
509
- if (getDirectiveMatcher(directives)(node)) {
560
+ leave: function (node) {
561
+ if (directiveMatcher(node)) {
562
+ firstVisitMadeChanges = true;
510
563
  return null;
511
564
  }
512
565
  },
513
566
  },
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);
567
+ });
568
+ if (!firstVisitMadeChanges) {
569
+ return doc;
523
570
  }
524
- return modifiedDoc;
571
+ var populateTransitiveVars = function (inUse) {
572
+ if (!inUse.transitiveVars) {
573
+ inUse.transitiveVars = new Set(inUse.variables);
574
+ if (!inUse.removed) {
575
+ inUse.fragmentSpreads.forEach(function (childFragmentName) {
576
+ populateTransitiveVars(getInUseByFragmentName(childFragmentName)).transitiveVars.forEach(function (varName) {
577
+ inUse.transitiveVars.add(varName);
578
+ });
579
+ });
580
+ }
581
+ }
582
+ return inUse;
583
+ };
584
+ var allFragmentNamesUsed = new Set();
585
+ docWithoutDirectiveSubtrees.definitions.forEach(function (def) {
586
+ if (def.kind === graphql.Kind.OPERATION_DEFINITION) {
587
+ populateTransitiveVars(getInUseByOperationName(def.name && def.name.value)).fragmentSpreads.forEach(function (childFragmentName) {
588
+ allFragmentNamesUsed.add(childFragmentName);
589
+ });
590
+ }
591
+ else if (def.kind === graphql.Kind.FRAGMENT_DEFINITION &&
592
+ operationCount === 0 &&
593
+ !getInUseByFragmentName(def.name.value).removed) {
594
+ allFragmentNamesUsed.add(def.name.value);
595
+ }
596
+ });
597
+ allFragmentNamesUsed.forEach(function (fragmentName) {
598
+ populateTransitiveVars(getInUseByFragmentName(fragmentName)).fragmentSpreads.forEach(function (childFragmentName) {
599
+ allFragmentNamesUsed.add(childFragmentName);
600
+ });
601
+ });
602
+ var fragmentWillBeRemoved = function (fragmentName) { return !!(!allFragmentNamesUsed.has(fragmentName) ||
603
+ getInUseByFragmentName(fragmentName).removed); };
604
+ var enterVisitor = {
605
+ enter: function (node) {
606
+ if (fragmentWillBeRemoved(node.name.value)) {
607
+ return null;
608
+ }
609
+ },
610
+ };
611
+ return nullIfDocIsEmpty(graphql.visit(docWithoutDirectiveSubtrees, {
612
+ FragmentSpread: enterVisitor,
613
+ FragmentDefinition: enterVisitor,
614
+ OperationDefinition: {
615
+ leave: function (node) {
616
+ if (node.variableDefinitions) {
617
+ var usedVariableNames_1 = populateTransitiveVars(getInUseByOperationName(node.name && node.name.value)).transitiveVars;
618
+ if (usedVariableNames_1.size < node.variableDefinitions.length) {
619
+ return tslib.__assign(tslib.__assign({}, node), { variableDefinitions: node.variableDefinitions.filter(function (varDef) { return usedVariableNames_1.has(varDef.variable.name.value); }) });
620
+ }
621
+ }
622
+ },
623
+ },
624
+ }));
525
625
  }
526
626
  var addTypenameToDocument = Object.assign(function (doc) {
527
627
  return graphql.visit(doc, {
528
628
  SelectionSet: {
529
629
  enter: function (node, _key, parent) {
530
630
  if (parent &&
531
- parent.kind === 'OperationDefinition') {
631
+ parent.kind === graphql.Kind.OPERATION_DEFINITION) {
532
632
  return;
533
633
  }
534
634
  var selections = node.selections;
@@ -578,7 +678,7 @@ function getArgumentMatcher(config) {
578
678
  return function argumentMatcher(argument) {
579
679
  return config.some(function (aConfig) {
580
680
  return argument.value &&
581
- argument.value.kind === 'Variable' &&
681
+ argument.value.kind === graphql.Kind.VARIABLE &&
582
682
  argument.value.name &&
583
683
  (aConfig.name === argument.value.name.value ||
584
684
  (aConfig.test && aConfig.test(argument)));
@@ -633,19 +733,6 @@ function removeFragmentSpreadFromDocument(config, doc) {
633
733
  FragmentDefinition: { enter: enter },
634
734
  }));
635
735
  }
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
736
  function buildQueryFromSelectionSet(document) {
650
737
  var definition = getMainDefinition(document);
651
738
  var definitionOperation = definition.operation;
@@ -669,22 +756,6 @@ function removeClientSetsFromDocument(document) {
669
756
  remove: true,
670
757
  },
671
758
  ], 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
759
  return modifiedDoc;
689
760
  }
690
761
 
@@ -1181,10 +1252,6 @@ var Concast = (function (_super) {
1181
1252
  }(zenObservableTs.Observable));
1182
1253
  fixObservableSubclass(Concast);
1183
1254
 
1184
- function isNonEmptyArray(value) {
1185
- return Array.isArray(value) && value.length > 0;
1186
- }
1187
-
1188
1255
  function isExecutionPatchIncrementalResult(value) {
1189
1256
  return "incremental" in value;
1190
1257
  }
@@ -1287,6 +1354,7 @@ exports.hasAllDirectives = hasAllDirectives;
1287
1354
  exports.hasAnyDirectives = hasAnyDirectives;
1288
1355
  exports.hasClientExports = hasClientExports;
1289
1356
  exports.hasDirectives = hasDirectives;
1357
+ exports.isArray = isArray;
1290
1358
  exports.isDocumentNode = isDocumentNode;
1291
1359
  exports.isField = isField;
1292
1360
  exports.isInlineFragment = isInlineFragment;
package/version.js CHANGED
@@ -1,2 +1,2 @@
1
- export var version = '3.7.8';
1
+ export var version = '3.7.9';
2
2
  //# sourceMappingURL=version.js.map