@apollo/client 3.8.0-alpha.0 → 3.8.0-alpha.1

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 (37) hide show
  1. package/README.md +7 -7
  2. package/apollo-client.cjs +130 -54
  3. package/apollo-client.cjs.map +1 -1
  4. package/apollo-client.min.cjs +1 -1
  5. package/core/ObservableQuery.d.ts +2 -1
  6. package/core/ObservableQuery.d.ts.map +1 -1
  7. package/core/ObservableQuery.js +5 -2
  8. package/core/ObservableQuery.js.map +1 -1
  9. package/core/QueryInfo.d.ts.map +1 -1
  10. package/core/QueryInfo.js +3 -16
  11. package/core/QueryInfo.js.map +1 -1
  12. package/core/QueryManager.d.ts.map +1 -1
  13. package/core/QueryManager.js +52 -27
  14. package/core/QueryManager.js.map +1 -1
  15. package/core/core.cjs +147 -45
  16. package/core/core.cjs.map +1 -1
  17. package/core/core.cjs.native.js +147 -45
  18. package/invariantErrorCodes.js +1 -1
  19. package/package.json +8 -8
  20. package/react/hooks/hooks.cjs +27 -5
  21. package/react/hooks/hooks.cjs.map +1 -1
  22. package/react/hooks/hooks.cjs.native.js +27 -5
  23. package/react/hooks/useSuspenseQuery.d.ts.map +1 -1
  24. package/react/hooks/useSuspenseQuery.js +28 -6
  25. package/react/hooks/useSuspenseQuery.js.map +1 -1
  26. package/utilities/common/errorHandling.d.ts +3 -2
  27. package/utilities/common/errorHandling.d.ts.map +1 -1
  28. package/utilities/common/errorHandling.js +18 -1
  29. package/utilities/common/errorHandling.js.map +1 -1
  30. package/utilities/common/incrementalResult.d.ts +5 -2
  31. package/utilities/common/incrementalResult.d.ts.map +1 -1
  32. package/utilities/common/incrementalResult.js +29 -1
  33. package/utilities/common/incrementalResult.js.map +1 -1
  34. package/utilities/utilities.cjs +21 -1
  35. package/utilities/utilities.cjs.map +1 -1
  36. package/utilities/utilities.cjs.native.js +21 -1
  37. package/version.js +1 -1
package/README.md CHANGED
@@ -21,13 +21,13 @@ Learn how to use Apollo Client with self-paced hands-on training on Odyssey, Apo
21
21
 
22
22
  ## Maintainers
23
23
 
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)
24
+ |Name|Username|
25
+ |---|---|
26
+ |Ben Newman|[@benjamn](https://github.com/benjamn)|
27
+ |Alessia Bellisario|[@alessbell](https://github.com/alessbell)|
28
+ |Jeff Auriemma|[@bignimbus](https://github.com/bignimbus)|
29
+ |Hugh Willson|[@hwillson](https://github.com/hwillson)|
30
+ |Jerel Miller|[@jerelmiller](https://github.com/jerelmiller)|
31
31
 
32
32
  ## Who is Apollo?
33
33
 
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.8.0-alpha.0';
1348
+ var version = '3.8.0-alpha.1';
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);
@@ -4613,7 +4654,7 @@ var ObservableQuery = (function (_super) {
4613
4654
  }
4614
4655
  return this.last;
4615
4656
  };
4616
- ObservableQuery.prototype.reobserve = function (newOptions, newNetworkStatus) {
4657
+ ObservableQuery.prototype.reobserveAsConcast = function (newOptions, newNetworkStatus) {
4617
4658
  var _this = this;
4618
4659
  this.isTornDown = false;
4619
4660
  var useDisposableConcast = newNetworkStatus === exports.NetworkStatus.refetch ||
@@ -4656,7 +4697,10 @@ var ObservableQuery = (function (_super) {
4656
4697
  this.observer = observer;
4657
4698
  }
4658
4699
  concast.addObserver(observer);
4659
- return concast.promise;
4700
+ return concast;
4701
+ };
4702
+ ObservableQuery.prototype.reobserve = function (newOptions, newNetworkStatus) {
4703
+ return this.reobserveAsConcast(newOptions, newNetworkStatus).promise;
4660
4704
  };
4661
4705
  ObservableQuery.prototype.observe = function () {
4662
4706
  this.reportResult(this.getCurrentResult(false), this.variables);
@@ -5182,22 +5226,8 @@ var QueryInfo = (function () {
5182
5226
  : [];
5183
5227
  this.reset();
5184
5228
  if ('incremental' in result && isNonEmptyArray(result.incremental)) {
5185
- var mergedData_1 = this.getDiff().result;
5186
- result.incremental.forEach(function (_a) {
5187
- var data = _a.data, path = _a.path, errors = _a.errors;
5188
- for (var i = path.length - 1; i >= 0; --i) {
5189
- var key = path[i];
5190
- var isNumericKey = !isNaN(+key);
5191
- var parent_1 = isNumericKey ? [] : {};
5192
- parent_1[key] = data;
5193
- data = parent_1;
5194
- }
5195
- if (errors) {
5196
- graphQLErrors.push.apply(graphQLErrors, errors);
5197
- }
5198
- mergedData_1 = merger.merge(mergedData_1, data);
5199
- });
5200
- result.data = mergedData_1;
5229
+ var mergedData = mergeIncrementalData(this.getDiff().result, result);
5230
+ result.data = mergedData;
5201
5231
  }
5202
5232
  else if ('hasNext' in result && result.hasNext) {
5203
5233
  var diff = this.getDiff();
@@ -5358,7 +5388,7 @@ var QueryManager = (function () {
5358
5388
  return asyncMap(self.getObservableFromLink(mutation, tslib.__assign(tslib.__assign({}, context), { optimisticResponse: optimisticResponse }), variables, false), function (result) {
5359
5389
  if (graphQLResultHasError(result) && errorPolicy === 'none') {
5360
5390
  throw new ApolloError({
5361
- graphQLErrors: result.errors,
5391
+ graphQLErrors: getGraphQLErrorsFromResult(result),
5362
5392
  });
5363
5393
  }
5364
5394
  if (mutationStoreValue) {
@@ -5392,7 +5422,9 @@ var QueryManager = (function () {
5392
5422
  }).subscribe({
5393
5423
  next: function (storeResult) {
5394
5424
  self.broadcastQueries();
5395
- resolve(storeResult);
5425
+ if (!('hasNext' in storeResult) || storeResult.hasNext === false) {
5426
+ resolve(storeResult);
5427
+ }
5396
5428
  },
5397
5429
  error: function (err) {
5398
5430
  if (mutationStoreValue) {
@@ -5420,12 +5452,33 @@ var QueryManager = (function () {
5420
5452
  var cacheWrites = [];
5421
5453
  var skipCache = mutation.fetchPolicy === "no-cache";
5422
5454
  if (!skipCache && shouldWriteResult(result, mutation.errorPolicy)) {
5423
- cacheWrites.push({
5424
- result: result.data,
5425
- dataId: 'ROOT_MUTATION',
5426
- query: mutation.document,
5427
- variables: mutation.variables,
5428
- });
5455
+ if (!isExecutionPatchIncrementalResult(result)) {
5456
+ cacheWrites.push({
5457
+ result: result.data,
5458
+ dataId: 'ROOT_MUTATION',
5459
+ query: mutation.document,
5460
+ variables: mutation.variables,
5461
+ });
5462
+ }
5463
+ if (isExecutionPatchIncrementalResult(result) && isNonEmptyArray(result.incremental)) {
5464
+ var diff = cache.diff({
5465
+ id: "ROOT_MUTATION",
5466
+ query: this.transform(mutation.document).asQuery,
5467
+ variables: mutation.variables,
5468
+ optimistic: false,
5469
+ returnPartialData: true,
5470
+ });
5471
+ var mergedData = mergeIncrementalData(diff.result, result);
5472
+ if (typeof mergedData !== 'undefined') {
5473
+ result.data = mergedData;
5474
+ cacheWrites.push({
5475
+ result: mergedData,
5476
+ dataId: 'ROOT_MUTATION',
5477
+ query: mutation.document,
5478
+ variables: mutation.variables,
5479
+ });
5480
+ }
5481
+ }
5429
5482
  var updateQueries_1 = mutation.updateQueries;
5430
5483
  if (updateQueries_1) {
5431
5484
  this.queries.forEach(function (_a, queryId) {
@@ -5472,6 +5525,8 @@ var QueryManager = (function () {
5472
5525
  cacheWrites.forEach(function (write) { return cache.write(write); });
5473
5526
  }
5474
5527
  var update = mutation.update;
5528
+ var isFinalResult = !isExecutionPatchResult(result) ||
5529
+ (isExecutionPatchIncrementalResult(result) && !result.hasNext);
5475
5530
  if (update) {
5476
5531
  if (!skipCache) {
5477
5532
  var diff = cache.diff({
@@ -5481,16 +5536,24 @@ var QueryManager = (function () {
5481
5536
  optimistic: false,
5482
5537
  returnPartialData: true,
5483
5538
  });
5484
- if (diff.complete && !(isExecutionPatchIncrementalResult(result))) {
5539
+ if (diff.complete) {
5485
5540
  result = tslib.__assign(tslib.__assign({}, result), { data: diff.result });
5541
+ if ('incremental' in result) {
5542
+ delete result.incremental;
5543
+ }
5544
+ if ('hasNext' in result) {
5545
+ delete result.hasNext;
5546
+ }
5486
5547
  }
5487
5548
  }
5488
- update(cache, result, {
5489
- context: mutation.context,
5490
- variables: mutation.variables,
5491
- });
5549
+ if (isFinalResult) {
5550
+ update(cache, result, {
5551
+ context: mutation.context,
5552
+ variables: mutation.variables,
5553
+ });
5554
+ }
5492
5555
  }
5493
- if (!skipCache && !mutation.keepRootFields) {
5556
+ if (!skipCache && !mutation.keepRootFields && isFinalResult) {
5494
5557
  cache.modify({
5495
5558
  id: 'ROOT_MUTATION',
5496
5559
  fields: function (value, _a) {
@@ -5858,17 +5921,8 @@ var QueryManager = (function () {
5858
5921
  var requestId = queryInfo.lastRequestId = this.generateRequestId();
5859
5922
  var linkDocument = this.cache.transformForLink(this.transform(queryInfo.document).document);
5860
5923
  return asyncMap(this.getObservableFromLink(linkDocument, options.context, options.variables), function (result) {
5861
- var graphQLErrors = isNonEmptyArray(result.errors)
5862
- ? result.errors.slice(0)
5863
- : [];
5864
- if ('incremental' in result && isNonEmptyArray(result.incremental)) {
5865
- result.incremental.forEach(function (incrementalResult) {
5866
- if (incrementalResult.errors) {
5867
- graphQLErrors.push.apply(graphQLErrors, incrementalResult.errors);
5868
- }
5869
- });
5870
- }
5871
- var hasErrors = isNonEmptyArray(graphQLErrors);
5924
+ var graphQLErrors = getGraphQLErrorsFromResult(result);
5925
+ var hasErrors = graphQLErrors.length > 0;
5872
5926
  if (requestId >= queryInfo.lastRequestId) {
5873
5927
  if (hasErrors && options.errorPolicy === "none") {
5874
5928
  throw queryInfo.markError(new ApolloError({
@@ -7171,24 +7225,28 @@ function useSuspenseQuery_experimental(query, options) {
7171
7225
  var client = useApolloClient(options.client);
7172
7226
  var watchQueryOptions = useWatchQueryOptions({ query: query, options: options, client: client });
7173
7227
  var previousWatchQueryOptionsRef = React.useRef(watchQueryOptions);
7228
+ var deferred = useIsDeferred(query);
7174
7229
  var fetchPolicy = watchQueryOptions.fetchPolicy, errorPolicy = watchQueryOptions.errorPolicy, returnPartialData = watchQueryOptions.returnPartialData, variables = watchQueryOptions.variables;
7175
7230
  var cacheEntry = suspenseCache.lookup(query, variables);
7176
7231
  var observable = React.useState(function () {
7177
7232
  return (cacheEntry === null || cacheEntry === void 0 ? void 0 : cacheEntry.observable) || client.watchQuery(watchQueryOptions);
7178
7233
  })[0];
7179
7234
  var result = useObservableQueryResult(observable);
7180
- if (result.error && errorPolicy === 'none') {
7235
+ var hasFullResult = result.data && !result.partial;
7236
+ var hasPartialResult = result.data && result.partial;
7237
+ var usePartialResult = returnPartialData && hasPartialResult;
7238
+ if (result.error &&
7239
+ errorPolicy === 'none' &&
7240
+ (!deferred || !hasPartialResult)) {
7181
7241
  throw result.error;
7182
7242
  }
7183
7243
  if (result.loading) {
7184
7244
  if (!cacheEntry) {
7185
7245
  cacheEntry = suspenseCache.add(query, variables, {
7186
- promise: observable.reobserve(watchQueryOptions),
7246
+ promise: maybeWrapConcastWithCustomPromise(observable.reobserveAsConcast(watchQueryOptions), { deferred: deferred }),
7187
7247
  observable: observable,
7188
7248
  });
7189
7249
  }
7190
- var hasFullResult = result.data && !result.partial;
7191
- var usePartialResult = returnPartialData && result.partial && result.data;
7192
7250
  var hasUsableResult = usePartialResult ||
7193
7251
  (fetchPolicy === 'cache-and-network' && hasFullResult);
7194
7252
  if (!hasUsableResult && !cacheEntry.fulfilled) {
@@ -7215,7 +7273,7 @@ function useSuspenseQuery_experimental(query, options) {
7215
7273
  return React.useMemo(function () {
7216
7274
  return {
7217
7275
  data: result.data,
7218
- error: errorPolicy === 'all' ? toApolloError(result) : void 0,
7276
+ error: errorPolicy === 'ignore' ? void 0 : toApolloError(result),
7219
7277
  fetchMore: function (options) {
7220
7278
  var promise = observable.fetchMore(options);
7221
7279
  suspenseCache.add(query, watchQueryOptions.variables, {
@@ -7254,6 +7312,21 @@ function toApolloError(result) {
7254
7312
  ? new ApolloError({ graphQLErrors: result.errors })
7255
7313
  : result.error;
7256
7314
  }
7315
+ function maybeWrapConcastWithCustomPromise(concast, _a) {
7316
+ var deferred = _a.deferred;
7317
+ if (deferred) {
7318
+ return new Promise(function (resolve, reject) {
7319
+ var subscription = concast.subscribe({
7320
+ next: function (value) {
7321
+ resolve(value);
7322
+ subscription.unsubscribe();
7323
+ },
7324
+ error: reject,
7325
+ });
7326
+ });
7327
+ }
7328
+ return concast.promise;
7329
+ }
7257
7330
  function useWatchQueryOptions(_a) {
7258
7331
  var query = _a.query, options = _a.options, client = _a.client;
7259
7332
  var defaultOptions = client.defaultOptions.watchQuery;
@@ -7266,6 +7339,9 @@ function useWatchQueryOptions(_a) {
7266
7339
  }
7267
7340
  return watchQueryOptions;
7268
7341
  }
7342
+ function useIsDeferred(query) {
7343
+ return React.useMemo(function () { return hasDirectives(['defer'], query); }, [query]);
7344
+ }
7269
7345
  function useObservableQueryResult(observable) {
7270
7346
  var resultRef = React.useRef();
7271
7347
  var isMountedRef = React.useRef(false);