@apollo/client 3.7.2 → 3.7.4

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 (51) hide show
  1. package/README.md +8 -7
  2. package/apollo-client.cjs +147 -63
  3. package/apollo-client.cjs.map +1 -1
  4. package/apollo-client.min.cjs +1 -1
  5. package/core/QueryInfo.d.ts.map +1 -1
  6. package/core/QueryInfo.js +8 -17
  7. package/core/QueryInfo.js.map +1 -1
  8. package/core/QueryManager.d.ts.map +1 -1
  9. package/core/QueryManager.js +52 -27
  10. package/core/QueryManager.js.map +1 -1
  11. package/core/core.cjs +147 -44
  12. package/core/core.cjs.map +1 -1
  13. package/core/core.cjs.native.js +147 -44
  14. package/invariantErrorCodes.js +1 -1
  15. package/package.json +16 -10
  16. package/react/hooks/hooks.cjs +44 -15
  17. package/react/hooks/hooks.cjs.map +1 -1
  18. package/react/hooks/hooks.cjs.native.js +44 -15
  19. package/react/hooks/useLazyQuery.d.ts.map +1 -1
  20. package/react/hooks/useLazyQuery.js +19 -4
  21. package/react/hooks/useLazyQuery.js.map +1 -1
  22. package/react/hooks/useMutation.d.ts.map +1 -1
  23. package/react/hooks/useMutation.js +7 -7
  24. package/react/hooks/useMutation.js.map +1 -1
  25. package/react/hooks/useQuery.d.ts +2 -1
  26. package/react/hooks/useQuery.d.ts.map +1 -1
  27. package/react/hooks/useQuery.js +19 -5
  28. package/react/hooks/useQuery.js.map +1 -1
  29. package/testing/core/core.cjs +17 -0
  30. package/testing/core/core.cjs.map +1 -1
  31. package/testing/core/core.cjs.native.js +17 -0
  32. package/testing/core/index.d.ts +1 -0
  33. package/testing/core/index.d.ts.map +1 -1
  34. package/testing/core/index.js +1 -0
  35. package/testing/core/index.js.map +1 -1
  36. package/testing/core/wait.d.ts +3 -0
  37. package/testing/core/wait.d.ts.map +1 -0
  38. package/testing/core/wait.js +16 -0
  39. package/testing/core/wait.js.map +1 -0
  40. package/utilities/common/errorHandling.d.ts +3 -2
  41. package/utilities/common/errorHandling.d.ts.map +1 -1
  42. package/utilities/common/errorHandling.js +18 -1
  43. package/utilities/common/errorHandling.js.map +1 -1
  44. package/utilities/common/incrementalResult.d.ts +5 -2
  45. package/utilities/common/incrementalResult.d.ts.map +1 -1
  46. package/utilities/common/incrementalResult.js +29 -1
  47. package/utilities/common/incrementalResult.js.map +1 -1
  48. package/utilities/utilities.cjs +21 -1
  49. package/utilities/utilities.cjs.map +1 -1
  50. package/utilities/utilities.cjs.native.js +21 -1
  51. package/version.js +1 -1
package/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
  [![npm version](https://badge.fury.io/js/%40apollo%2Fclient.svg)](https://badge.fury.io/js/%40apollo%2Fclient)
6
6
  [![Build Status](https://circleci.com/gh/apollographql/apollo-client.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-client)
7
7
  [![Join the community](https://img.shields.io/discourse/status?label=Join%20the%20community&server=https%3A%2F%2Fcommunity.apollographql.com)](https://community.apollographql.com)
8
+ [![Join our Discord server](https://img.shields.io/discord/1022972389463687228.svg?color=7389D8&labelColor=6A7EC2&logo=discord&logoColor=ffffff&style=flat-square)](https://discord.gg/graphos)
8
9
 
9
10
  Apollo Client is a fully-featured caching GraphQL client with integrations for React, Angular, and more. It allows you to easily build UI components that fetch data via GraphQL.
10
11
 
@@ -21,13 +22,13 @@ Learn how to use Apollo Client with self-paced hands-on training on Odyssey, Apo
21
22
 
22
23
  ## Maintainers
23
24
 
24
- - [@benjamn](https://github.com/benjamn)
25
- - [@alessbell](https://github.com/alessbell)
26
- - [@bignimbus](https://github.com/bignimbus)
27
- - [@hwillson](https://github.com/hwillson)
28
- - [@jpvajda](https://github.com/jpvajda)
29
- - [@mrdoombringer](https://github.com/mrdoombringer)
30
- - [@jerelmiller](https://github.com/jerelmiller)
25
+ |Name|Username|
26
+ |---|---|
27
+ |Ben Newman|[@benjamn](https://github.com/benjamn)|
28
+ |Alessia Bellisario|[@alessbell](https://github.com/alessbell)|
29
+ |Jeff Auriemma|[@bignimbus](https://github.com/bignimbus)|
30
+ |Hugh Willson|[@hwillson](https://github.com/hwillson)|
31
+ |Jerel Miller|[@jerelmiller](https://github.com/jerelmiller)|
31
32
 
32
33
  ## Who is Apollo?
33
34
 
package/apollo-client.cjs CHANGED
@@ -1053,8 +1053,53 @@ function isNonEmptyArray(value) {
1053
1053
  return Array.isArray(value) && value.length > 0;
1054
1054
  }
1055
1055
 
1056
+ function isExecutionPatchIncrementalResult(value) {
1057
+ return "incremental" in value;
1058
+ }
1059
+ function isExecutionPatchInitialResult(value) {
1060
+ return "hasNext" in value && "data" in value;
1061
+ }
1062
+ function isExecutionPatchResult(value) {
1063
+ return (isExecutionPatchIncrementalResult(value) ||
1064
+ isExecutionPatchInitialResult(value));
1065
+ }
1066
+ function mergeIncrementalData(prevResult, result) {
1067
+ var mergedData = prevResult;
1068
+ var merger = new DeepMerger();
1069
+ if (isExecutionPatchIncrementalResult(result) &&
1070
+ isNonEmptyArray(result.incremental)) {
1071
+ result.incremental.forEach(function (_a) {
1072
+ var data = _a.data, path = _a.path;
1073
+ for (var i = path.length - 1; i >= 0; --i) {
1074
+ var key = path[i];
1075
+ var isNumericKey = !isNaN(+key);
1076
+ var parent_1 = isNumericKey ? [] : {};
1077
+ parent_1[key] = data;
1078
+ data = parent_1;
1079
+ }
1080
+ mergedData = merger.merge(mergedData, data);
1081
+ });
1082
+ }
1083
+ return mergedData;
1084
+ }
1085
+
1056
1086
  function graphQLResultHasError(result) {
1057
- return (result.errors && result.errors.length > 0) || false;
1087
+ var errors = getGraphQLErrorsFromResult(result);
1088
+ return isNonEmptyArray(errors);
1089
+ }
1090
+ function getGraphQLErrorsFromResult(result) {
1091
+ var graphQLErrors = isNonEmptyArray(result.errors)
1092
+ ? result.errors.slice(0)
1093
+ : [];
1094
+ if (isExecutionPatchIncrementalResult(result) &&
1095
+ isNonEmptyArray(result.incremental)) {
1096
+ result.incremental.forEach(function (incrementalResult) {
1097
+ if (incrementalResult.errors) {
1098
+ graphQLErrors.push.apply(graphQLErrors, incrementalResult.errors);
1099
+ }
1100
+ });
1101
+ }
1102
+ return graphQLErrors;
1058
1103
  }
1059
1104
 
1060
1105
  function compact() {
@@ -1300,7 +1345,7 @@ var concat = ApolloLink.concat;
1300
1345
 
1301
1346
  var execute = ApolloLink.execute;
1302
1347
 
1303
- var version = '3.7.2';
1348
+ var version = '3.7.4';
1304
1349
 
1305
1350
  function isNodeResponse(value) {
1306
1351
  return !!value.body;
@@ -1881,10 +1926,6 @@ var HttpLink = (function (_super) {
1881
1926
  return HttpLink;
1882
1927
  }(ApolloLink));
1883
1928
 
1884
- function isExecutionPatchIncrementalResult(value) {
1885
- return !!value.incremental;
1886
- }
1887
-
1888
1929
  var ApolloCache = (function () {
1889
1930
  function ApolloCache() {
1890
1931
  this.getFragmentDoc = optimism.wrap(getFragmentQueryDocument);
@@ -5175,28 +5216,18 @@ var QueryInfo = (function () {
5175
5216
  };
5176
5217
  QueryInfo.prototype.markResult = function (result, document, options, cacheWriteBehavior) {
5177
5218
  var _this = this;
5219
+ var merger = new DeepMerger();
5178
5220
  var graphQLErrors = isNonEmptyArray(result.errors)
5179
5221
  ? result.errors.slice(0)
5180
5222
  : [];
5181
5223
  this.reset();
5182
5224
  if ('incremental' in result && isNonEmptyArray(result.incremental)) {
5183
- var mergedData_1 = this.getDiff().result;
5184
- var merger_1 = new DeepMerger();
5185
- result.incremental.forEach(function (_a) {
5186
- var data = _a.data, path = _a.path, errors = _a.errors;
5187
- for (var i = path.length - 1; i >= 0; --i) {
5188
- var key = path[i];
5189
- var isNumericKey = !isNaN(+key);
5190
- var parent_1 = isNumericKey ? [] : {};
5191
- parent_1[key] = data;
5192
- data = parent_1;
5193
- }
5194
- if (errors) {
5195
- graphQLErrors.push.apply(graphQLErrors, errors);
5196
- }
5197
- mergedData_1 = merger_1.merge(mergedData_1, data);
5198
- });
5199
- result.data = mergedData_1;
5225
+ var mergedData = mergeIncrementalData(this.getDiff().result, result);
5226
+ result.data = mergedData;
5227
+ }
5228
+ else if ('hasNext' in result && result.hasNext) {
5229
+ var diff = this.getDiff();
5230
+ result.data = merger.merge(diff.result, result.data);
5200
5231
  }
5201
5232
  this.graphQLErrors = graphQLErrors;
5202
5233
  if (options.fetchPolicy === 'no-cache') {
@@ -5353,7 +5384,7 @@ var QueryManager = (function () {
5353
5384
  return asyncMap(self.getObservableFromLink(mutation, tslib.__assign(tslib.__assign({}, context), { optimisticResponse: optimisticResponse }), variables, false), function (result) {
5354
5385
  if (graphQLResultHasError(result) && errorPolicy === 'none') {
5355
5386
  throw new ApolloError({
5356
- graphQLErrors: result.errors,
5387
+ graphQLErrors: getGraphQLErrorsFromResult(result),
5357
5388
  });
5358
5389
  }
5359
5390
  if (mutationStoreValue) {
@@ -5387,7 +5418,9 @@ var QueryManager = (function () {
5387
5418
  }).subscribe({
5388
5419
  next: function (storeResult) {
5389
5420
  self.broadcastQueries();
5390
- resolve(storeResult);
5421
+ if (!('hasNext' in storeResult) || storeResult.hasNext === false) {
5422
+ resolve(storeResult);
5423
+ }
5391
5424
  },
5392
5425
  error: function (err) {
5393
5426
  if (mutationStoreValue) {
@@ -5415,12 +5448,33 @@ var QueryManager = (function () {
5415
5448
  var cacheWrites = [];
5416
5449
  var skipCache = mutation.fetchPolicy === "no-cache";
5417
5450
  if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
5418
- cacheWrites.push({
5419
- result: result.data,
5420
- dataId: 'ROOT_MUTATION',
5421
- query: mutation.document,
5422
- variables: mutation.variables,
5423
- });
5451
+ if (!isExecutionPatchIncrementalResult(result)) {
5452
+ cacheWrites.push({
5453
+ result: result.data,
5454
+ dataId: 'ROOT_MUTATION',
5455
+ query: mutation.document,
5456
+ variables: mutation.variables,
5457
+ });
5458
+ }
5459
+ if (isExecutionPatchIncrementalResult(result) && isNonEmptyArray(result.incremental)) {
5460
+ var diff = cache.diff({
5461
+ id: "ROOT_MUTATION",
5462
+ query: this.transform(mutation.document).asQuery,
5463
+ variables: mutation.variables,
5464
+ optimistic: false,
5465
+ returnPartialData: true,
5466
+ });
5467
+ var mergedData = mergeIncrementalData(diff.result, result);
5468
+ if (typeof mergedData !== 'undefined') {
5469
+ result.data = mergedData;
5470
+ cacheWrites.push({
5471
+ result: mergedData,
5472
+ dataId: 'ROOT_MUTATION',
5473
+ query: mutation.document,
5474
+ variables: mutation.variables,
5475
+ });
5476
+ }
5477
+ }
5424
5478
  var updateQueries_1 = mutation.updateQueries;
5425
5479
  if (updateQueries_1) {
5426
5480
  this.queries.forEach(function (_a, queryId) {
@@ -5467,6 +5521,8 @@ var QueryManager = (function () {
5467
5521
  cacheWrites.forEach(function (write) { return cache.write(write); });
5468
5522
  }
5469
5523
  var update = mutation.update;
5524
+ var isFinalResult = !isExecutionPatchResult(result) ||
5525
+ (isExecutionPatchIncrementalResult(result) && !result.hasNext);
5470
5526
  if (update) {
5471
5527
  if (!skipCache) {
5472
5528
  var diff = cache.diff({
@@ -5476,16 +5532,24 @@ var QueryManager = (function () {
5476
5532
  optimistic: false,
5477
5533
  returnPartialData: true,
5478
5534
  });
5479
- if (diff.complete && !(isExecutionPatchIncrementalResult(result))) {
5535
+ if (diff.complete) {
5480
5536
  result = tslib.__assign(tslib.__assign({}, result), { data: diff.result });
5537
+ if ('incremental' in result) {
5538
+ delete result.incremental;
5539
+ }
5540
+ if ('hasNext' in result) {
5541
+ delete result.hasNext;
5542
+ }
5481
5543
  }
5482
5544
  }
5483
- update(cache, result, {
5484
- context: mutation.context,
5485
- variables: mutation.variables,
5486
- });
5545
+ if (isFinalResult) {
5546
+ update(cache, result, {
5547
+ context: mutation.context,
5548
+ variables: mutation.variables,
5549
+ });
5550
+ }
5487
5551
  }
5488
- if (!skipCache && !mutation.keepRootFields) {
5552
+ if (!skipCache && !mutation.keepRootFields && isFinalResult) {
5489
5553
  cache.modify({
5490
5554
  id: 'ROOT_MUTATION',
5491
5555
  fields: function (value, _a) {
@@ -5853,17 +5917,8 @@ var QueryManager = (function () {
5853
5917
  var requestId = queryInfo.lastRequestId = this.generateRequestId();
5854
5918
  var linkDocument = this.cache.transformForLink(this.transform(queryInfo.document).document);
5855
5919
  return asyncMap(this.getObservableFromLink(linkDocument, options.context, options.variables), function (result) {
5856
- var graphQLErrors = isNonEmptyArray(result.errors)
5857
- ? result.errors.slice(0)
5858
- : [];
5859
- if ('incremental' in result && isNonEmptyArray(result.incremental)) {
5860
- result.incremental.forEach(function (incrementalResult) {
5861
- if (incrementalResult.errors) {
5862
- graphQLErrors.push.apply(graphQLErrors, incrementalResult.errors);
5863
- }
5864
- });
5865
- }
5866
- var hasErrors = isNonEmptyArray(graphQLErrors);
5920
+ var graphQLErrors = getGraphQLErrorsFromResult(result);
5921
+ var hasErrors = graphQLErrors.length > 0;
5867
5922
  if (requestId >= queryInfo.lastRequestId) {
5868
5923
  if (hasErrors && options.errorPolicy === "none") {
5869
5924
  throw queryInfo.markError(new ApolloError({
@@ -6577,11 +6632,19 @@ var InternalState = (function () {
6577
6632
  InternalState.prototype.forceUpdate = function () {
6578
6633
  __DEV__ && tsInvariant.invariant.warn("Calling default no-op implementation of InternalState#forceUpdate");
6579
6634
  };
6580
- InternalState.prototype.asyncUpdate = function () {
6635
+ InternalState.prototype.asyncUpdate = function (signal) {
6581
6636
  var _this = this;
6582
- return new Promise(function (resolve) {
6637
+ return new Promise(function (resolve, reject) {
6638
+ var watchQueryOptions = _this.watchQueryOptions;
6639
+ var handleAborted = function () {
6640
+ _this.asyncResolveFns.delete(resolve);
6641
+ _this.optionsToIgnoreOnce.delete(watchQueryOptions);
6642
+ signal.removeEventListener('abort', handleAborted);
6643
+ reject(signal.reason);
6644
+ };
6583
6645
  _this.asyncResolveFns.add(resolve);
6584
- _this.optionsToIgnoreOnce.add(_this.watchQueryOptions);
6646
+ _this.optionsToIgnoreOnce.add(watchQueryOptions);
6647
+ signal.addEventListener('abort', handleAborted);
6585
6648
  _this.forceUpdate();
6586
6649
  });
6587
6650
  };
@@ -6758,9 +6821,10 @@ var InternalState = (function () {
6758
6821
  InternalState.prototype.handleErrorOrCompleted = function (result) {
6759
6822
  var _this = this;
6760
6823
  if (!result.loading) {
6824
+ var error_1 = this.toApolloError(result);
6761
6825
  Promise.resolve().then(function () {
6762
- if (result.error) {
6763
- _this.onError(result.error);
6826
+ if (error_1) {
6827
+ _this.onError(error_1);
6764
6828
  }
6765
6829
  else if (result.data) {
6766
6830
  _this.onCompleted(result.data);
@@ -6770,6 +6834,11 @@ var InternalState = (function () {
6770
6834
  });
6771
6835
  }
6772
6836
  };
6837
+ InternalState.prototype.toApolloError = function (result) {
6838
+ return isNonEmptyArray(result.errors)
6839
+ ? new ApolloError({ graphQLErrors: result.errors })
6840
+ : result.error;
6841
+ };
6773
6842
  InternalState.prototype.getCurrentResult = function () {
6774
6843
  if (!this.result) {
6775
6844
  this.handleErrorOrCompleted(this.result = this.observable.getCurrentResult());
@@ -6812,6 +6881,7 @@ var EAGER_METHODS = [
6812
6881
  'subscribeToMore',
6813
6882
  ];
6814
6883
  function useLazyQuery(query, options) {
6884
+ var abortControllersRef = React.useRef(new Set());
6815
6885
  var internalState = useInternalState(useApolloClient(options && options.client), query);
6816
6886
  var execOptionsRef = React.useRef();
6817
6887
  var merged = execOptionsRef.current
@@ -6842,14 +6912,28 @@ function useLazyQuery(query, options) {
6842
6912
  return eagerMethods;
6843
6913
  }, []);
6844
6914
  Object.assign(result, eagerMethods);
6915
+ React.useEffect(function () {
6916
+ return function () {
6917
+ abortControllersRef.current.forEach(function (controller) {
6918
+ controller.abort();
6919
+ });
6920
+ };
6921
+ }, []);
6845
6922
  var execute = React.useCallback(function (executeOptions) {
6923
+ var controller = new AbortController();
6924
+ abortControllersRef.current.add(controller);
6846
6925
  execOptionsRef.current = executeOptions ? tslib.__assign(tslib.__assign({}, executeOptions), { fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy }) : {
6847
6926
  fetchPolicy: initialFetchPolicy,
6848
6927
  };
6849
6928
  var promise = internalState
6850
- .asyncUpdate()
6851
- .then(function (queryResult) { return Object.assign(queryResult, eagerMethods); });
6852
- promise.catch(function () { });
6929
+ .asyncUpdate(controller.signal)
6930
+ .then(function (queryResult) {
6931
+ abortControllersRef.current.delete(controller);
6932
+ return Object.assign(queryResult, eagerMethods);
6933
+ });
6934
+ promise.catch(function () {
6935
+ abortControllersRef.current.delete(controller);
6936
+ });
6853
6937
  return promise;
6854
6938
  }, []);
6855
6939
  return [execute, result];
@@ -6890,7 +6974,7 @@ function useMutation(mutation, options) {
6890
6974
  var mutationId = ++ref.current.mutationId;
6891
6975
  var clientOptions = mergeOptions(baseOptions, executeOptions);
6892
6976
  return client.mutate(clientOptions).then(function (response) {
6893
- var _a, _b, _c;
6977
+ var _a;
6894
6978
  var data = response.data, errors = response.errors;
6895
6979
  var error = errors && errors.length > 0
6896
6980
  ? new ApolloError({ graphQLErrors: errors })
@@ -6908,11 +6992,11 @@ function useMutation(mutation, options) {
6908
6992
  setResult(ref.current.result = result_1);
6909
6993
  }
6910
6994
  }
6911
- (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onCompleted) === null || _b === void 0 ? void 0 : _b.call(_a, response.data, clientOptions);
6912
- (_c = executeOptions.onCompleted) === null || _c === void 0 ? void 0 : _c.call(executeOptions, response.data, clientOptions);
6995
+ var onCompleted = executeOptions.onCompleted || ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onCompleted);
6996
+ onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(response.data, clientOptions);
6913
6997
  return response;
6914
6998
  }).catch(function (error) {
6915
- var _a, _b, _c, _d;
6999
+ var _a;
6916
7000
  if (mutationId === ref.current.mutationId &&
6917
7001
  ref.current.isMounted) {
6918
7002
  var result_2 = {
@@ -6926,9 +7010,9 @@ function useMutation(mutation, options) {
6926
7010
  setResult(ref.current.result = result_2);
6927
7011
  }
6928
7012
  }
6929
- if (((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError) || clientOptions.onError) {
6930
- (_c = (_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.onError) === null || _c === void 0 ? void 0 : _c.call(_b, error, clientOptions);
6931
- (_d = executeOptions.onError) === null || _d === void 0 ? void 0 : _d.call(executeOptions, error, clientOptions);
7013
+ var onError = executeOptions.onError || ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError);
7014
+ if (onError) {
7015
+ onError(error, clientOptions);
6932
7016
  return { data: void 0, errors: error };
6933
7017
  }
6934
7018
  throw error;