@apollo/client 3.7.0-alpha.4 → 3.7.0-beta.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 (57) hide show
  1. package/apollo-client.cjs +87 -34
  2. package/apollo-client.cjs.map +1 -1
  3. package/apollo-client.min.cjs +1 -1
  4. package/cache/cache.cjs +21 -12
  5. package/cache/cache.cjs.map +1 -1
  6. package/cache/cache.cjs.native.js +21 -12
  7. package/cache/core/types/Cache.d.ts +2 -2
  8. package/cache/core/types/Cache.d.ts.map +1 -1
  9. package/cache/core/types/Cache.js.map +1 -1
  10. package/cache/core/types/common.d.ts +1 -0
  11. package/cache/core/types/common.d.ts.map +1 -1
  12. package/cache/core/types/common.js +10 -0
  13. package/cache/core/types/common.js.map +1 -1
  14. package/cache/index.d.ts +1 -1
  15. package/cache/index.d.ts.map +1 -1
  16. package/cache/index.js.map +1 -1
  17. package/cache/inmemory/inMemoryCache.js +2 -2
  18. package/cache/inmemory/inMemoryCache.js.map +1 -1
  19. package/cache/inmemory/readFromStore.d.ts.map +1 -1
  20. package/cache/inmemory/readFromStore.js +10 -11
  21. package/cache/inmemory/readFromStore.js.map +1 -1
  22. package/core/ObservableQuery.d.ts.map +1 -1
  23. package/core/ObservableQuery.js +2 -2
  24. package/core/ObservableQuery.js.map +1 -1
  25. package/core/core.cjs +2 -2
  26. package/core/core.cjs.map +1 -1
  27. package/core/core.cjs.native.js +2 -2
  28. package/invariantErrorCodes.js +1 -1
  29. package/package.json +9 -9
  30. package/react/hooks/hooks.cjs +56 -9
  31. package/react/hooks/hooks.cjs.map +1 -1
  32. package/react/hooks/hooks.cjs.native.js +56 -9
  33. package/react/hooks/index.d.ts +1 -0
  34. package/react/hooks/index.d.ts.map +1 -1
  35. package/react/hooks/index.js +1 -0
  36. package/react/hooks/index.js.map +1 -1
  37. package/react/hooks/useFragment.d.ts +14 -0
  38. package/react/hooks/useFragment.d.ts.map +1 -0
  39. package/react/hooks/useFragment.js +37 -0
  40. package/react/hooks/useFragment.js.map +1 -0
  41. package/react/hooks/useQuery.d.ts +1 -1
  42. package/react/hooks/useQuery.d.ts.map +1 -1
  43. package/react/hooks/useQuery.js +7 -2
  44. package/react/hooks/useQuery.js.map +1 -1
  45. package/react/hooks/useSubscription.d.ts.map +1 -1
  46. package/react/hooks/useSubscription.js +17 -7
  47. package/react/hooks/useSubscription.js.map +1 -1
  48. package/utilities/common/mergeDeep.d.ts.map +1 -1
  49. package/utilities/common/mergeDeep.js +8 -11
  50. package/utilities/common/mergeDeep.js.map +1 -1
  51. package/utilities/policies/pagination.d.ts.map +1 -1
  52. package/utilities/policies/pagination.js +9 -7
  53. package/utilities/policies/pagination.js.map +1 -1
  54. package/utilities/utilities.cjs +17 -18
  55. package/utilities/utilities.cjs.map +1 -1
  56. package/utilities/utilities.cjs.native.js +17 -18
  57. package/version.js +1 -1
package/apollo-client.cjs CHANGED
@@ -761,18 +761,15 @@ var DeepMerger = (function () {
761
761
  };
762
762
  DeepMerger.prototype.shallowCopyForMerge = function (value) {
763
763
  if (isNonNullObject(value)) {
764
- if (this.pastCopies.has(value)) {
765
- if (!Object.isFrozen(value))
766
- return value;
767
- this.pastCopies.delete(value);
768
- }
769
- if (Array.isArray(value)) {
770
- value = value.slice(0);
771
- }
772
- else {
773
- value = tslib.__assign({ __proto__: Object.getPrototypeOf(value) }, value);
764
+ if (!this.pastCopies.has(value)) {
765
+ if (Array.isArray(value)) {
766
+ value = value.slice(0);
767
+ }
768
+ else {
769
+ value = tslib.__assign({ __proto__: Object.getPrototypeOf(value) }, value);
770
+ }
771
+ this.pastCopies.add(value);
774
772
  }
775
- this.pastCopies.add(value);
776
773
  }
777
774
  return value;
778
775
  };
@@ -1301,7 +1298,7 @@ var concat = ApolloLink.concat;
1301
1298
 
1302
1299
  var execute = ApolloLink.execute;
1303
1300
 
1304
- var version = '3.7.0-alpha.4';
1301
+ var version = '3.7.0-beta.0';
1305
1302
 
1306
1303
  var hasOwnProperty$3 = Object.prototype.hasOwnProperty;
1307
1304
  function parseAndCheckHttpResponse(operations) {
@@ -1685,10 +1682,20 @@ exports.Cache = void 0;
1685
1682
 
1686
1683
  var MissingFieldError = (function () {
1687
1684
  function MissingFieldError(message, path, query, variables) {
1685
+ var _a;
1688
1686
  this.message = message;
1689
1687
  this.path = path;
1690
1688
  this.query = query;
1691
1689
  this.variables = variables;
1690
+ if (Array.isArray(this.path)) {
1691
+ this.missing = this.message;
1692
+ for (var i = this.path.length - 1; i >= 0; --i) {
1693
+ this.missing = (_a = {}, _a[this.path[i]] = this.missing, _a);
1694
+ }
1695
+ }
1696
+ else {
1697
+ this.missing = this.path;
1698
+ }
1692
1699
  }
1693
1700
  return MissingFieldError;
1694
1701
  }());
@@ -2394,7 +2401,6 @@ var StoreReader = (function () {
2394
2401
  var policies = this.config.cache.policies;
2395
2402
  variables = tslib.__assign(tslib.__assign({}, getDefaultValues(getQueryDefinition(query))), variables);
2396
2403
  var rootRef = makeReference(rootId);
2397
- var merger = new DeepMerger;
2398
2404
  var execResult = this.executeSelectionSet({
2399
2405
  selectionSet: getMainDefinition(query).selectionSet,
2400
2406
  objectOrReference: rootRef,
@@ -2407,9 +2413,6 @@ var StoreReader = (function () {
2407
2413
  varString: canonicalStringify(variables),
2408
2414
  canonizeResults: canonizeResults,
2409
2415
  fragmentMap: createFragmentMap(getFragmentDefinitions(query)),
2410
- merge: function (a, b) {
2411
- return merger.merge(a, b);
2412
- },
2413
2416
  },
2414
2417
  });
2415
2418
  var missing;
@@ -2448,17 +2451,18 @@ var StoreReader = (function () {
2448
2451
  }
2449
2452
  var variables = context.variables, policies = context.policies, store = context.store;
2450
2453
  var typename = store.getFieldValue(objectOrReference, "__typename");
2451
- var result = {};
2454
+ var objectsToMerge = [];
2452
2455
  var missing;
2456
+ var missingMerger = new DeepMerger();
2453
2457
  if (this.config.addTypename &&
2454
2458
  typeof typename === "string" &&
2455
2459
  !policies.rootIdsByTypename[typename]) {
2456
- result = { __typename: typename };
2460
+ objectsToMerge.push({ __typename: typename });
2457
2461
  }
2458
2462
  function handleMissing(result, resultName) {
2459
2463
  var _a;
2460
2464
  if (result.missing) {
2461
- missing = context.merge(missing, (_a = {}, _a[resultName] = result.missing, _a));
2465
+ missing = missingMerger.merge(missing, (_a = {}, _a[resultName] = result.missing, _a));
2462
2466
  }
2463
2467
  return result.result;
2464
2468
  }
@@ -2477,7 +2481,7 @@ var StoreReader = (function () {
2477
2481
  var resultName = resultKeyNameFromField(selection);
2478
2482
  if (fieldValue === void 0) {
2479
2483
  if (!addTypenameToDocument.added(selection)) {
2480
- missing = context.merge(missing, (_a = {},
2484
+ missing = missingMerger.merge(missing, (_a = {},
2481
2485
  _a[resultName] = "Can't find field '".concat(selection.name.value, "' on ").concat(isReference(objectOrReference)
2482
2486
  ? objectOrReference.__ref + " object"
2483
2487
  : "object " + JSON.stringify(objectOrReference, null, 2)),
@@ -2506,7 +2510,7 @@ var StoreReader = (function () {
2506
2510
  }), resultName);
2507
2511
  }
2508
2512
  if (fieldValue !== void 0) {
2509
- result = context.merge(result, (_b = {}, _b[resultName] = fieldValue, _b));
2513
+ objectsToMerge.push((_b = {}, _b[resultName] = fieldValue, _b));
2510
2514
  }
2511
2515
  }
2512
2516
  else {
@@ -2516,6 +2520,7 @@ var StoreReader = (function () {
2516
2520
  }
2517
2521
  }
2518
2522
  });
2523
+ var result = mergeDeepArray(objectsToMerge);
2519
2524
  var finalResult = { result: result, missing: missing };
2520
2525
  var frozen = context.canonizeResults
2521
2526
  ? this.canon.admit(finalResult)
@@ -2529,10 +2534,11 @@ var StoreReader = (function () {
2529
2534
  var _this = this;
2530
2535
  var field = _a.field, array = _a.array, enclosingRef = _a.enclosingRef, context = _a.context;
2531
2536
  var missing;
2537
+ var missingMerger = new DeepMerger();
2532
2538
  function handleMissing(childResult, i) {
2533
2539
  var _a;
2534
2540
  if (childResult.missing) {
2535
- missing = context.merge(missing, (_a = {}, _a[i] = childResult.missing, _a));
2541
+ missing = missingMerger.merge(missing, (_a = {}, _a[i] = childResult.missing, _a));
2536
2542
  }
2537
2543
  return childResult.result;
2538
2544
  }
@@ -3619,8 +3625,8 @@ var InMemoryCache = (function (_super) {
3619
3625
  makeCacheKey: function (c) {
3620
3626
  var store = c.optimistic ? _this.optimisticData : _this.data;
3621
3627
  if (supportsResultCaching(store)) {
3622
- var optimistic = c.optimistic, rootId = c.rootId, variables = c.variables;
3623
- return store.makeCacheKey(c.query, c.callback, canonicalStringify({ optimistic: optimistic, rootId: rootId, variables: variables }));
3628
+ var optimistic = c.optimistic, id = c.id, variables = c.variables;
3629
+ return store.makeCacheKey(c.query, c.callback, canonicalStringify({ optimistic: optimistic, id: id, variables: variables }));
3624
3630
  }
3625
3631
  }
3626
3632
  });
@@ -4305,7 +4311,7 @@ var ObservableQuery = (function (_super) {
4305
4311
  newNetworkStatus === exports.NetworkStatus.poll;
4306
4312
  var oldVariables = this.options.variables;
4307
4313
  var oldFetchPolicy = this.options.fetchPolicy;
4308
- var mergedOptions = mergeOptions(this.options, newOptions || {});
4314
+ var mergedOptions = compact(this.options, newOptions || {});
4309
4315
  var options = useDisposableConcast
4310
4316
  ? mergedOptions
4311
4317
  : assign(this.options, mergedOptions);
@@ -6188,7 +6194,7 @@ function useInternalState(client, query) {
6188
6194
  if (!stateRef.current ||
6189
6195
  client !== stateRef.current.client ||
6190
6196
  query !== stateRef.current.query) {
6191
- stateRef.current = new InternalState(client, query);
6197
+ stateRef.current = new InternalState(client, query, stateRef.current);
6192
6198
  }
6193
6199
  var state = stateRef.current;
6194
6200
  var _a = React.useState(0); _a[0]; var setTick = _a[1];
@@ -6198,7 +6204,7 @@ function useInternalState(client, query) {
6198
6204
  return state;
6199
6205
  }
6200
6206
  var InternalState = (function () {
6201
- function InternalState(client, query) {
6207
+ function InternalState(client, query, previous) {
6202
6208
  this.client = client;
6203
6209
  this.query = query;
6204
6210
  this.asyncResolveFns = new Set();
@@ -6217,6 +6223,11 @@ var InternalState = (function () {
6217
6223
  });
6218
6224
  this.toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)();
6219
6225
  verifyDocumentType(query, exports.DocumentType.Query);
6226
+ var previousResult = previous && previous.result;
6227
+ var previousData = previousResult && previousResult.data;
6228
+ if (previousData) {
6229
+ this.previousData = previousData;
6230
+ }
6220
6231
  }
6221
6232
  InternalState.prototype.forceUpdate = function () {
6222
6233
  __DEV__ && tsInvariant.invariant.warn("Calling default no-op implementation of InternalState#forceUpdate");
@@ -6591,6 +6602,12 @@ function useSubscription(subscription, options) {
6591
6602
  context: options === null || options === void 0 ? void 0 : options.context,
6592
6603
  });
6593
6604
  }), observable = _b[0], setObservable = _b[1];
6605
+ var canResetObservableRef = React.useRef(false);
6606
+ React.useEffect(function () {
6607
+ return function () {
6608
+ canResetObservableRef.current = true;
6609
+ };
6610
+ }, []);
6594
6611
  var ref = React.useRef({ client: client, subscription: subscription, options: options });
6595
6612
  React.useEffect(function () {
6596
6613
  var _a, _b, _c, _d;
@@ -6599,7 +6616,7 @@ function useSubscription(subscription, options) {
6599
6616
  shouldResubscribe = !!shouldResubscribe(options);
6600
6617
  }
6601
6618
  if (options === null || options === void 0 ? void 0 : options.skip) {
6602
- if (!(options === null || options === void 0 ? void 0 : options.skip) !== !((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip)) {
6619
+ if (!(options === null || options === void 0 ? void 0 : options.skip) !== !((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.skip) || canResetObservableRef.current) {
6603
6620
  setResult({
6604
6621
  loading: false,
6605
6622
  data: void 0,
@@ -6607,13 +6624,16 @@ function useSubscription(subscription, options) {
6607
6624
  variables: options === null || options === void 0 ? void 0 : options.variables,
6608
6625
  });
6609
6626
  setObservable(null);
6627
+ canResetObservableRef.current = false;
6610
6628
  }
6611
6629
  }
6612
- else if (shouldResubscribe !== false && (client !== ref.current.client ||
6613
- subscription !== ref.current.subscription ||
6614
- (options === null || options === void 0 ? void 0 : options.fetchPolicy) !== ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
6615
- !(options === null || options === void 0 ? void 0 : options.skip) !== !((_c = ref.current.options) === null || _c === void 0 ? void 0 : _c.skip) ||
6616
- !equality.equal(options === null || options === void 0 ? void 0 : options.variables, (_d = ref.current.options) === null || _d === void 0 ? void 0 : _d.variables))) {
6630
+ else if ((shouldResubscribe !== false &&
6631
+ (client !== ref.current.client ||
6632
+ subscription !== ref.current.subscription ||
6633
+ (options === null || options === void 0 ? void 0 : options.fetchPolicy) !== ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.fetchPolicy) ||
6634
+ !(options === null || options === void 0 ? void 0 : options.skip) !== !((_c = ref.current.options) === null || _c === void 0 ? void 0 : _c.skip) ||
6635
+ !equality.equal(options === null || options === void 0 ? void 0 : options.variables, (_d = ref.current.options) === null || _d === void 0 ? void 0 : _d.variables))) ||
6636
+ canResetObservableRef.current) {
6617
6637
  setResult({
6618
6638
  loading: true,
6619
6639
  data: void 0,
@@ -6626,9 +6646,10 @@ function useSubscription(subscription, options) {
6626
6646
  fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
6627
6647
  context: options === null || options === void 0 ? void 0 : options.context,
6628
6648
  }));
6649
+ canResetObservableRef.current = false;
6629
6650
  }
6630
6651
  Object.assign(ref.current, { client: client, subscription: subscription, options: options });
6631
- }, [client, subscription, options]);
6652
+ }, [client, subscription, options, canResetObservableRef.current]);
6632
6653
  React.useEffect(function () {
6633
6654
  if (!observable) {
6634
6655
  return;
@@ -6683,6 +6704,37 @@ function useReactiveVar(rv) {
6683
6704
  return value;
6684
6705
  }
6685
6706
 
6707
+ function useFragment(options) {
6708
+ var cache = useApolloClient().cache;
6709
+ var fragment = options.fragment, fragmentName = options.fragmentName, from = options.from, _a = options.optimistic, optimistic = _a === void 0 ? true : _a, rest = tslib.__rest(options, ["fragment", "fragmentName", "from", "optimistic"]);
6710
+ var diffOptions = tslib.__assign(tslib.__assign({}, rest), { id: typeof from === "string" ? from : cache.identify(from), query: cache["getFragmentDoc"](fragment, fragmentName), optimistic: optimistic });
6711
+ var resultRef = React.useRef();
6712
+ var latestDiff = cache.diff(diffOptions);
6713
+ return useSyncExternalStore(function (forceUpdate) {
6714
+ var immediate = true;
6715
+ return cache.watch(tslib.__assign(tslib.__assign({}, diffOptions), { immediate: immediate, callback: function (diff) {
6716
+ if (!immediate && !equality.equal(diff, latestDiff)) {
6717
+ resultRef.current = diffToResult(latestDiff = diff);
6718
+ forceUpdate();
6719
+ }
6720
+ immediate = false;
6721
+ } }));
6722
+ }, function () {
6723
+ return resultRef.current ||
6724
+ (resultRef.current = diffToResult(latestDiff));
6725
+ });
6726
+ }
6727
+ function diffToResult(diff) {
6728
+ var result = {
6729
+ data: diff.result,
6730
+ complete: !!diff.complete,
6731
+ };
6732
+ if (diff.missing) {
6733
+ result.missing = mergeDeepArray(diff.missing.map(function (error) { return error.missing; }));
6734
+ }
6735
+ return result;
6736
+ }
6737
+
6686
6738
  exports.setLogVerbosity = tsInvariant.setVerbosity;
6687
6739
  exports.Observable = zenObservableTs.Observable;
6688
6740
  exports.disableExperimentalFragmentVariables = graphqlTag.disableExperimentalFragmentVariables;
@@ -6731,6 +6783,7 @@ exports.split = split;
6731
6783
  exports.throwServerError = throwServerError;
6732
6784
  exports.toPromise = toPromise;
6733
6785
  exports.useApolloClient = useApolloClient;
6786
+ exports.useFragment = useFragment;
6734
6787
  exports.useLazyQuery = useLazyQuery;
6735
6788
  exports.useMutation = useMutation;
6736
6789
  exports.useQuery = useQuery;