@apollo/client 3.6.0-beta.4 → 3.6.0-beta.7

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/apollo-client.cjs +276 -243
  2. package/apollo-client.cjs.map +1 -1
  3. package/apollo-client.min.cjs +1 -1
  4. package/cache/cache.cjs +21 -20
  5. package/cache/cache.cjs.map +1 -1
  6. package/cache/inmemory/helpers.d.ts +1 -0
  7. package/cache/inmemory/helpers.d.ts.map +1 -1
  8. package/cache/inmemory/helpers.js +3 -2
  9. package/cache/inmemory/helpers.js.map +1 -1
  10. package/cache/inmemory/key-extractor.js +5 -5
  11. package/cache/inmemory/key-extractor.js.map +1 -1
  12. package/cache/inmemory/object-canon.d.ts.map +1 -1
  13. package/cache/inmemory/object-canon.js +2 -1
  14. package/cache/inmemory/object-canon.js.map +1 -1
  15. package/cache/inmemory/policies.d.ts +1 -1
  16. package/cache/inmemory/policies.d.ts.map +1 -1
  17. package/cache/inmemory/policies.js +6 -6
  18. package/cache/inmemory/policies.js.map +1 -1
  19. package/cache/inmemory/readFromStore.js +3 -3
  20. package/cache/inmemory/readFromStore.js.map +1 -1
  21. package/cache/inmemory/writeToStore.js +7 -7
  22. package/cache/inmemory/writeToStore.js.map +1 -1
  23. package/core/ApolloClient.d.ts.map +1 -1
  24. package/core/ApolloClient.js +2 -2
  25. package/core/ApolloClient.js.map +1 -1
  26. package/core/QueryManager.d.ts +5 -2
  27. package/core/QueryManager.d.ts.map +1 -1
  28. package/core/QueryManager.js +10 -7
  29. package/core/QueryManager.js.map +1 -1
  30. package/core/core.cjs +13 -10
  31. package/core/core.cjs.map +1 -1
  32. package/invariantErrorCodes.js +1 -1
  33. package/package.json +14 -14
  34. package/react/hooks/hooks.cjs +233 -203
  35. package/react/hooks/hooks.cjs.map +1 -1
  36. package/react/hooks/useQuery.d.ts.map +1 -1
  37. package/react/hooks/useQuery.js +234 -204
  38. package/react/hooks/useQuery.js.map +1 -1
  39. package/react/ssr/RenderPromises.d.ts +1 -1
  40. package/react/ssr/RenderPromises.d.ts.map +1 -1
  41. package/react/ssr/RenderPromises.js +2 -2
  42. package/react/ssr/RenderPromises.js.map +1 -1
  43. package/react/ssr/ssr.cjs +2 -2
  44. package/react/ssr/ssr.cjs.map +1 -1
  45. package/react/types/types.d.ts +1 -0
  46. package/react/types/types.d.ts.map +1 -1
  47. package/react/types/types.js.map +1 -1
  48. package/utilities/globals/global.d.ts +2 -8
  49. package/utilities/globals/global.d.ts.map +1 -1
  50. package/utilities/globals/global.js.map +1 -1
  51. package/version.js +1 -1
package/apollo-client.cjs CHANGED
@@ -703,7 +703,7 @@ function removeClientSetsFromDocument(document) {
703
703
  return modifiedDoc;
704
704
  }
705
705
 
706
- var hasOwnProperty$3 = Object.prototype.hasOwnProperty;
706
+ var hasOwnProperty$4 = Object.prototype.hasOwnProperty;
707
707
  function mergeDeep() {
708
708
  var sources = [];
709
709
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -740,7 +740,7 @@ var DeepMerger = (function () {
740
740
  }
741
741
  if (isNonNullObject(source) && isNonNullObject(target)) {
742
742
  Object.keys(source).forEach(function (sourceKey) {
743
- if (hasOwnProperty$3.call(target, sourceKey)) {
743
+ if (hasOwnProperty$4.call(target, sourceKey)) {
744
744
  var targetValue = target[sourceKey];
745
745
  if (source[sourceKey] !== targetValue) {
746
746
  var result = _this.reconciler.apply(_this, tslib.__spreadArray([target, source, sourceKey], context, false));
@@ -1291,9 +1291,9 @@ var concat = ApolloLink.concat;
1291
1291
 
1292
1292
  var execute = ApolloLink.execute;
1293
1293
 
1294
- var version = '3.6.0-beta.4';
1294
+ var version = '3.6.0-beta.7';
1295
1295
 
1296
- var hasOwnProperty$2 = Object.prototype.hasOwnProperty;
1296
+ var hasOwnProperty$3 = Object.prototype.hasOwnProperty;
1297
1297
  function parseAndCheckHttpResponse(operations) {
1298
1298
  return function (response) { return response
1299
1299
  .text()
@@ -1315,8 +1315,8 @@ function parseAndCheckHttpResponse(operations) {
1315
1315
  throwServerError(response, result, "Response not successful: Received status code ".concat(response.status));
1316
1316
  }
1317
1317
  if (!Array.isArray(result) &&
1318
- !hasOwnProperty$2.call(result, 'data') &&
1319
- !hasOwnProperty$2.call(result, 'errors')) {
1318
+ !hasOwnProperty$3.call(result, 'data') &&
1319
+ !hasOwnProperty$3.call(result, 'errors')) {
1320
1320
  throwServerError(response, result, "Server response was missing for query '".concat(Array.isArray(operations)
1321
1321
  ? operations.map(function (op) { return op.operationName; })
1322
1322
  : operations.operationName, "'."));
@@ -1726,7 +1726,7 @@ function fieldNameFromStoreName(storeFieldName) {
1726
1726
  }
1727
1727
  function selectionSetMatchesResult(selectionSet, result, variables) {
1728
1728
  if (isNonNullObject(result)) {
1729
- return Array.isArray(result)
1729
+ return isArray(result)
1730
1730
  ? result.every(function (item) { return selectionSetMatchesResult(selectionSet, item, variables); })
1731
1731
  : selectionSet.selections.every(function (field) {
1732
1732
  if (isField(field) && shouldInclude(field, variables)) {
@@ -1743,11 +1743,12 @@ function selectionSetMatchesResult(selectionSet, result, variables) {
1743
1743
  function storeValueIsStoreObject(value) {
1744
1744
  return isNonNullObject(value) &&
1745
1745
  !isReference(value) &&
1746
- !Array.isArray(value);
1746
+ !isArray(value);
1747
1747
  }
1748
1748
  function makeProcessedFieldsMerger() {
1749
1749
  return new DeepMerger;
1750
1750
  }
1751
+ var isArray = function (a) { return Array.isArray(a); };
1751
1752
 
1752
1753
  var DELETE = Object.create(null);
1753
1754
  var delModifier = function () { return DELETE; };
@@ -2210,7 +2211,7 @@ function supportsResultCaching(store) {
2210
2211
 
2211
2212
  function shallowCopy(value) {
2212
2213
  if (isNonNullObject(value)) {
2213
- return Array.isArray(value)
2214
+ return isArray(value)
2214
2215
  ? value.slice(0)
2215
2216
  : tslib.__assign({ __proto__: Object.getPrototypeOf(value) }, value);
2216
2217
  }
@@ -2473,7 +2474,7 @@ var StoreReader = (function () {
2473
2474
  _a));
2474
2475
  }
2475
2476
  }
2476
- else if (Array.isArray(fieldValue)) {
2477
+ else if (isArray(fieldValue)) {
2477
2478
  fieldValue = handleMissing(_this.executeSubSelectedArray({
2478
2479
  field: selection,
2479
2480
  array: fieldValue,
@@ -2532,7 +2533,7 @@ var StoreReader = (function () {
2532
2533
  if (item === null) {
2533
2534
  return null;
2534
2535
  }
2535
- if (Array.isArray(item)) {
2536
+ if (isArray(item)) {
2536
2537
  return handleMissing(_this.executeSubSelectedArray({
2537
2538
  field: field,
2538
2539
  array: item,
@@ -2726,13 +2727,13 @@ function getSpecifierPaths(spec) {
2726
2727
  var paths_1 = info.paths = [];
2727
2728
  var currentPath_1 = [];
2728
2729
  spec.forEach(function (s, i) {
2729
- if (Array.isArray(s)) {
2730
+ if (isArray(s)) {
2730
2731
  getSpecifierPaths(s).forEach(function (p) { return paths_1.push(currentPath_1.concat(p)); });
2731
2732
  currentPath_1.length = 0;
2732
2733
  }
2733
2734
  else {
2734
2735
  currentPath_1.push(s);
2735
- if (!Array.isArray(spec[i + 1])) {
2736
+ if (!isArray(spec[i + 1])) {
2736
2737
  paths_1.push(currentPath_1.slice(0));
2737
2738
  currentPath_1.length = 0;
2738
2739
  }
@@ -2747,14 +2748,14 @@ function extractKey(object, key) {
2747
2748
  function extractKeyPath(object, path, extract) {
2748
2749
  extract = extract || extractKey;
2749
2750
  return normalize(path.reduce(function reducer(obj, key) {
2750
- return Array.isArray(obj)
2751
+ return isArray(obj)
2751
2752
  ? obj.map(function (child) { return reducer(child, key); })
2752
2753
  : obj && extract(obj, key);
2753
2754
  }, object));
2754
2755
  }
2755
2756
  function normalize(value) {
2756
2757
  if (isNonNullObject(value)) {
2757
- if (Array.isArray(value)) {
2758
+ if (isArray(value)) {
2758
2759
  return value.map(normalize);
2759
2760
  }
2760
2761
  return collectSpecifierPaths(Object.keys(value).sort(), function (path) { return extractKeyPath(value, path); });
@@ -2817,7 +2818,7 @@ var Policies = (function () {
2817
2818
  var keyFn = policy && policy.keyFn || this.config.dataIdFromObject;
2818
2819
  while (keyFn) {
2819
2820
  var specifierOrId = keyFn(object, context);
2820
- if (Array.isArray(specifierOrId)) {
2821
+ if (isArray(specifierOrId)) {
2821
2822
  keyFn = keyFieldsFnFromSpecifier(specifierOrId);
2822
2823
  }
2823
2824
  else {
@@ -2860,7 +2861,7 @@ var Policies = (function () {
2860
2861
  setMerge(existing, incoming.merge);
2861
2862
  existing.keyFn =
2862
2863
  keyFields === false ? nullKeyFieldsFn :
2863
- Array.isArray(keyFields) ? keyFieldsFnFromSpecifier(keyFields) :
2864
+ isArray(keyFields) ? keyFieldsFnFromSpecifier(keyFields) :
2864
2865
  typeof keyFields === "function" ? keyFields :
2865
2866
  existing.keyFn;
2866
2867
  if (fields) {
@@ -2874,7 +2875,7 @@ var Policies = (function () {
2874
2875
  var keyArgs = incoming.keyArgs, read = incoming.read, merge = incoming.merge;
2875
2876
  existing.keyFn =
2876
2877
  keyArgs === false ? simpleKeyArgsFn :
2877
- Array.isArray(keyArgs) ? keyArgsFnFromSpecifier(keyArgs) :
2878
+ isArray(keyArgs) ? keyArgsFnFromSpecifier(keyArgs) :
2878
2879
  typeof keyArgs === "function" ? keyArgs :
2879
2880
  existing.keyFn;
2880
2881
  if (typeof read === "function") {
@@ -3019,7 +3020,7 @@ var Policies = (function () {
3019
3020
  var args = argsFromFieldSpecifier(fieldSpec);
3020
3021
  while (keyFn) {
3021
3022
  var specifierOrString = keyFn(args, context);
3022
- if (Array.isArray(specifierOrString)) {
3023
+ if (isArray(specifierOrString)) {
3023
3024
  keyFn = keyArgsFnFromSpecifier(specifierOrString);
3024
3025
  }
3025
3026
  else {
@@ -3140,7 +3141,7 @@ function normalizeReadFieldOptions(readFieldArgs, objectOrReference, variables)
3140
3141
  }
3141
3142
  function makeMergeObjectsFunction(store) {
3142
3143
  return function mergeObjects(existing, incoming) {
3143
- if (Array.isArray(existing) || Array.isArray(incoming)) {
3144
+ if (isArray(existing) || isArray(incoming)) {
3144
3145
  throw __DEV__ ? new tsInvariant.InvariantError("Cannot automatically merge arrays") : new tsInvariant.InvariantError(4);
3145
3146
  }
3146
3147
  if (isNonNullObject(existing) &&
@@ -3369,7 +3370,7 @@ var StoreWriter = (function () {
3369
3370
  if (!field.selectionSet || value === null) {
3370
3371
  return __DEV__ ? cloneDeep(value) : value;
3371
3372
  }
3372
- if (Array.isArray(value)) {
3373
+ if (isArray(value)) {
3373
3374
  return value.map(function (item, i) {
3374
3375
  var value = _this.processFieldValue(item, field, context, getChildMergeTree(mergeTree, i));
3375
3376
  maybeRecycleChildMergeTree(mergeTree, i);
@@ -3434,7 +3435,7 @@ var StoreWriter = (function () {
3434
3435
  var _a;
3435
3436
  var _this = this;
3436
3437
  if (mergeTree.map.size && !isReference(incoming)) {
3437
- var e_1 = (!Array.isArray(incoming) &&
3438
+ var e_1 = (!isArray(incoming) &&
3438
3439
  (isReference(existing) || storeValueIsStoreObject(existing))) ? existing : void 0;
3439
3440
  var i_1 = incoming;
3440
3441
  if (e_1 && !getStorageArgs) {
@@ -3442,7 +3443,7 @@ var StoreWriter = (function () {
3442
3443
  }
3443
3444
  var changedFields_1;
3444
3445
  var getValue_1 = function (from, name) {
3445
- return Array.isArray(from)
3446
+ return isArray(from)
3446
3447
  ? (typeof name === "number" ? from[name] : void 0)
3447
3448
  : context.store.getFieldValue(from, String(name));
3448
3449
  };
@@ -3464,7 +3465,7 @@ var StoreWriter = (function () {
3464
3465
  }
3465
3466
  });
3466
3467
  if (changedFields_1) {
3467
- incoming = (Array.isArray(i_1) ? i_1.slice(0) : tslib.__assign({}, i_1));
3468
+ incoming = (isArray(i_1) ? i_1.slice(0) : tslib.__assign({}, i_1));
3468
3469
  changedFields_1.forEach(function (value, name) {
3469
3470
  incoming[name] = value;
3470
3471
  });
@@ -3545,8 +3546,8 @@ function warnAboutDataLoss(existingRef, incomingObj, storeFieldName, store) {
3545
3546
  return;
3546
3547
  warnings.add(typeDotName);
3547
3548
  var childTypenames = [];
3548
- if (!Array.isArray(existing) &&
3549
- !Array.isArray(incoming)) {
3549
+ if (!isArray(existing) &&
3550
+ !isArray(incoming)) {
3550
3551
  [existing, incoming].forEach(function (child) {
3551
3552
  var typename = store.getFieldValue(child, "__typename");
3552
3553
  if (typeof typename === "string" &&
@@ -3904,7 +3905,7 @@ function isNetworkRequestInFlight(networkStatus) {
3904
3905
  return networkStatus ? networkStatus < 7 : false;
3905
3906
  }
3906
3907
 
3907
- var assign = Object.assign, hasOwnProperty$1 = Object.hasOwnProperty;
3908
+ var assign = Object.assign, hasOwnProperty$2 = Object.hasOwnProperty;
3908
3909
  var warnedAboutUpdateQuery = false;
3909
3910
  var ObservableQuery = (function (_super) {
3910
3911
  tslib.__extends(ObservableQuery, _super);
@@ -4060,7 +4061,7 @@ var ObservableQuery = (function (_super) {
4060
4061
  else {
4061
4062
  reobserveOptions.fetchPolicy = 'network-only';
4062
4063
  }
4063
- if (__DEV__ && variables && hasOwnProperty$1.call(variables, "variables")) {
4064
+ if (__DEV__ && variables && hasOwnProperty$2.call(variables, "variables")) {
4064
4065
  var queryDef = getQueryDefinition(this.options.query);
4065
4066
  var vars = queryDef.variableDefinitions;
4066
4067
  if (!vars || !vars.some(function (v) { return v.variable.name.value === "variables"; })) {
@@ -4869,10 +4870,10 @@ function shouldWriteResult(result, errorPolicy) {
4869
4870
  return writeWithErrors;
4870
4871
  }
4871
4872
 
4872
- var hasOwnProperty = Object.prototype.hasOwnProperty;
4873
+ var hasOwnProperty$1 = Object.prototype.hasOwnProperty;
4873
4874
  var QueryManager = (function () {
4874
4875
  function QueryManager(_a) {
4875
- var cache = _a.cache, link = _a.link, _b = _a.queryDeduplication, queryDeduplication = _b === void 0 ? false : _b, onBroadcast = _a.onBroadcast, _c = _a.ssrMode, ssrMode = _c === void 0 ? false : _c, _d = _a.clientAwareness, clientAwareness = _d === void 0 ? {} : _d, localState = _a.localState, assumeImmutableResults = _a.assumeImmutableResults;
4876
+ var cache = _a.cache, link = _a.link, defaultOptions = _a.defaultOptions, _b = _a.queryDeduplication, queryDeduplication = _b === void 0 ? false : _b, onBroadcast = _a.onBroadcast, _c = _a.ssrMode, ssrMode = _c === void 0 ? false : _c, _d = _a.clientAwareness, clientAwareness = _d === void 0 ? {} : _d, localState = _a.localState, assumeImmutableResults = _a.assumeImmutableResults;
4876
4877
  this.clientAwareness = {};
4877
4878
  this.queries = new Map();
4878
4879
  this.fetchCancelFns = new Map();
@@ -4883,6 +4884,7 @@ var QueryManager = (function () {
4883
4884
  this.inFlightLinkObservables = new Map();
4884
4885
  this.cache = cache;
4885
4886
  this.link = link;
4887
+ this.defaultOptions = defaultOptions || Object.create(null);
4886
4888
  this.queryDeduplication = queryDeduplication;
4887
4889
  this.clientAwareness = clientAwareness;
4888
4890
  this.localState = localState || new LocalState({ cache: cache });
@@ -4904,11 +4906,12 @@ var QueryManager = (function () {
4904
4906
  this.fetchCancelFns.clear();
4905
4907
  };
4906
4908
  QueryManager.prototype.mutate = function (_a) {
4907
- var mutation = _a.mutation, variables = _a.variables, optimisticResponse = _a.optimisticResponse, updateQueries = _a.updateQueries, _b = _a.refetchQueries, refetchQueries = _b === void 0 ? [] : _b, _c = _a.awaitRefetchQueries, awaitRefetchQueries = _c === void 0 ? false : _c, updateWithProxyFn = _a.update, onQueryUpdated = _a.onQueryUpdated, _d = _a.errorPolicy, errorPolicy = _d === void 0 ? 'none' : _d, _e = _a.fetchPolicy, fetchPolicy = _e === void 0 ? 'network-only' : _e, keepRootFields = _a.keepRootFields, context = _a.context;
4909
+ var _b, _c;
4910
+ var mutation = _a.mutation, variables = _a.variables, optimisticResponse = _a.optimisticResponse, updateQueries = _a.updateQueries, _d = _a.refetchQueries, refetchQueries = _d === void 0 ? [] : _d, _e = _a.awaitRefetchQueries, awaitRefetchQueries = _e === void 0 ? false : _e, updateWithProxyFn = _a.update, onQueryUpdated = _a.onQueryUpdated, _f = _a.fetchPolicy, fetchPolicy = _f === void 0 ? ((_b = this.defaultOptions.mutate) === null || _b === void 0 ? void 0 : _b.fetchPolicy) || "network-only" : _f, _g = _a.errorPolicy, errorPolicy = _g === void 0 ? ((_c = this.defaultOptions.mutate) === null || _c === void 0 ? void 0 : _c.errorPolicy) || "none" : _g, keepRootFields = _a.keepRootFields, context = _a.context;
4908
4911
  return tslib.__awaiter(this, void 0, void 0, function () {
4909
4912
  var mutationId, mutationStoreValue, self;
4910
- return tslib.__generator(this, function (_f) {
4911
- switch (_f.label) {
4913
+ return tslib.__generator(this, function (_h) {
4914
+ switch (_h.label) {
4912
4915
  case 0:
4913
4916
  __DEV__ ? tsInvariant.invariant(mutation, 'mutation option is required. You must specify your GraphQL document in the mutation option.') : tsInvariant.invariant(mutation, 12);
4914
4917
  __DEV__ ? tsInvariant.invariant(fetchPolicy === 'network-only' ||
@@ -4920,8 +4923,8 @@ var QueryManager = (function () {
4920
4923
  if (!this.transform(mutation).hasClientExports) return [3, 2];
4921
4924
  return [4, this.localState.addExportedVariables(mutation, variables, context)];
4922
4925
  case 1:
4923
- variables = (_f.sent());
4924
- _f.label = 2;
4926
+ variables = (_h.sent());
4927
+ _h.label = 2;
4925
4928
  case 2:
4926
4929
  mutationStoreValue = this.mutationStore &&
4927
4930
  (this.mutationStore[mutationId] = {
@@ -5022,7 +5025,7 @@ var QueryManager = (function () {
5022
5025
  this.queries.forEach(function (_a, queryId) {
5023
5026
  var observableQuery = _a.observableQuery;
5024
5027
  var queryName = observableQuery && observableQuery.queryName;
5025
- if (!queryName || !hasOwnProperty.call(updateQueries_1, queryName)) {
5028
+ if (!queryName || !hasOwnProperty$1.call(updateQueries_1, queryName)) {
5026
5029
  return;
5027
5030
  }
5028
5031
  var updater = updateQueries_1[queryName];
@@ -5481,7 +5484,8 @@ var QueryManager = (function () {
5481
5484
  var query = this.transform(options.query).document;
5482
5485
  var variables = this.getVariables(query, options.variables);
5483
5486
  var queryInfo = this.getQuery(queryId);
5484
- var _a = options.fetchPolicy, fetchPolicy = _a === void 0 ? "cache-first" : _a, _b = options.errorPolicy, errorPolicy = _b === void 0 ? "none" : _b, _c = options.returnPartialData, returnPartialData = _c === void 0 ? false : _c, _d = options.notifyOnNetworkStatusChange, notifyOnNetworkStatusChange = _d === void 0 ? false : _d, _e = options.context, context = _e === void 0 ? {} : _e;
5487
+ var defaults = this.defaultOptions.watchQuery;
5488
+ var _a = options.fetchPolicy, fetchPolicy = _a === void 0 ? defaults && defaults.fetchPolicy || "cache-first" : _a, _b = options.errorPolicy, errorPolicy = _b === void 0 ? defaults && defaults.errorPolicy || "none" : _b, _c = options.returnPartialData, returnPartialData = _c === void 0 ? false : _c, _d = options.notifyOnNetworkStatusChange, notifyOnNetworkStatusChange = _d === void 0 ? false : _d, _e = options.context, context = _e === void 0 ? {} : _e;
5485
5489
  var normalized = Object.assign({}, options, {
5486
5490
  query: query,
5487
5491
  variables: variables,
@@ -5699,7 +5703,6 @@ function mergeOptions(defaults, options) {
5699
5703
  var ApolloClient = (function () {
5700
5704
  function ApolloClient(options) {
5701
5705
  var _this = this;
5702
- this.defaultOptions = {};
5703
5706
  this.resetStoreCallbacks = [];
5704
5707
  this.clearStoreCallbacks = [];
5705
5708
  var uri = options.uri, credentials = options.credentials, headers = options.headers, cache = options.cache, _a = options.ssrMode, ssrMode = _a === void 0 ? false : _a, _b = options.ssrForceFetchDelay, ssrForceFetchDelay = _b === void 0 ? 0 : _b, _c = options.connectToDevTools, connectToDevTools = _c === void 0 ? typeof window === 'object' &&
@@ -5720,7 +5723,7 @@ var ApolloClient = (function () {
5720
5723
  this.cache = cache;
5721
5724
  this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0;
5722
5725
  this.queryDeduplication = queryDeduplication;
5723
- this.defaultOptions = defaultOptions || {};
5726
+ this.defaultOptions = defaultOptions || Object.create(null);
5724
5727
  this.typeDefs = typeDefs;
5725
5728
  if (ssrForceFetchDelay) {
5726
5729
  setTimeout(function () { return (_this.disableNetworkFetches = false); }, ssrForceFetchDelay);
@@ -5767,6 +5770,7 @@ var ApolloClient = (function () {
5767
5770
  this.queryManager = new QueryManager({
5768
5771
  cache: this.cache,
5769
5772
  link: this.link,
5773
+ defaultOptions: this.defaultOptions,
5770
5774
  queryDeduplication: queryDeduplication,
5771
5775
  ssrMode: ssrMode,
5772
5776
  clientAwareness: {
@@ -6053,227 +6057,256 @@ function verifyDocumentType(document, type) {
6053
6057
  "".concat(requiredOperationName, ", but a ").concat(usedOperationName, " was used instead.")) : tsInvariant.invariant(operation.type === type, 34);
6054
6058
  }
6055
6059
 
6060
+ var hasOwnProperty = Object.prototype.hasOwnProperty;
6056
6061
  function useQuery(query, options) {
6057
- var _a;
6058
- var context = React.useContext(getApolloContext());
6059
- var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);
6060
- var defaultWatchQueryOptions = client.defaultOptions.watchQuery;
6061
- verifyDocumentType(query, exports.DocumentType.Query);
6062
- var _b = React.useState(function () {
6063
- var watchQueryOptions = createWatchQueryOptions(query, options, defaultWatchQueryOptions);
6064
- var obsQuery = null;
6065
- if (context.renderPromises) {
6066
- obsQuery = context.renderPromises.getSSRObservable(watchQueryOptions);
6067
- }
6068
- if (!obsQuery) {
6069
- obsQuery = client.watchQuery(watchQueryOptions);
6070
- if (context.renderPromises) {
6071
- context.renderPromises.registerSSRObservable(obsQuery, watchQueryOptions);
6072
- }
6073
- }
6074
- if (context.renderPromises &&
6075
- (options === null || options === void 0 ? void 0 : options.ssr) !== false &&
6076
- !(options === null || options === void 0 ? void 0 : options.skip) &&
6077
- obsQuery.getCurrentResult().loading) {
6078
- context.renderPromises.addQueryPromise({
6079
- getOptions: function () { return createWatchQueryOptions(query, options, defaultWatchQueryOptions); },
6080
- fetchData: function () { return new Promise(function (resolve) {
6081
- var sub = obsQuery.subscribe({
6082
- next: function (result) {
6083
- if (!result.loading) {
6062
+ return useInternalState(useApolloClient(options && options.client), query).useQuery(options);
6063
+ }
6064
+ function useInternalState(client, query) {
6065
+ var stateRef = React.useRef();
6066
+ if (!stateRef.current ||
6067
+ client !== stateRef.current.client ||
6068
+ query !== stateRef.current.query) {
6069
+ stateRef.current = new InternalState(client, query);
6070
+ }
6071
+ var state = stateRef.current;
6072
+ var _a = React.useState(0); _a[0]; var setTick = _a[1];
6073
+ state.forceUpdate = function () {
6074
+ setTick(function (tick) { return tick + 1; });
6075
+ };
6076
+ return state;
6077
+ }
6078
+ var InternalState = (function () {
6079
+ function InternalState(client, query) {
6080
+ this.client = client;
6081
+ this.query = query;
6082
+ this.toQueryResultCache = new (canUseWeakMap ? WeakMap : Map)();
6083
+ verifyDocumentType(query, exports.DocumentType.Query);
6084
+ }
6085
+ InternalState.prototype.forceUpdate = function () {
6086
+ };
6087
+ InternalState.prototype.useQuery = function (options) {
6088
+ this.useOptions(options);
6089
+ var obsQuery = this.useObservableQuery();
6090
+ this.useSubscriptionEffect(obsQuery);
6091
+ var result = this.getCurrentResult();
6092
+ this.unsafeHandlePartialRefetch(result);
6093
+ return this.toQueryResult(result);
6094
+ };
6095
+ InternalState.prototype.useOptions = function (options) {
6096
+ this.renderPromises = React.useContext(getApolloContext()).renderPromises;
6097
+ var watchQueryOptions = this.createWatchQueryOptions(this.queryHookOptions = options || {});
6098
+ if (!equality.equal(watchQueryOptions, this.watchQueryOptions)) {
6099
+ this.watchQueryOptions = watchQueryOptions;
6100
+ }
6101
+ this.ssrDisabled = !!(options && (options.ssr === false ||
6102
+ options.skip));
6103
+ this.onCompleted = options
6104
+ && options.onCompleted
6105
+ || InternalState.prototype.onCompleted;
6106
+ this.onError = options
6107
+ && options.onError
6108
+ || InternalState.prototype.onError;
6109
+ };
6110
+ InternalState.prototype.createWatchQueryOptions = function (_a) {
6111
+ var _b;
6112
+ if (_a === void 0) { _a = {}; }
6113
+ var skip = _a.skip; _a.ssr; _a.onCompleted; _a.onError; _a.displayName; var otherOptions = tslib.__rest(_a, ["skip", "ssr", "onCompleted", "onError", "displayName"]);
6114
+ var watchQueryOptions = Object.assign(otherOptions, { query: this.query });
6115
+ if (skip) {
6116
+ watchQueryOptions.fetchPolicy = 'standby';
6117
+ }
6118
+ else if (((_b = watchQueryOptions.context) === null || _b === void 0 ? void 0 : _b.renderPromises) &&
6119
+ (watchQueryOptions.fetchPolicy === 'network-only' ||
6120
+ watchQueryOptions.fetchPolicy === 'cache-and-network')) {
6121
+ watchQueryOptions.fetchPolicy = 'cache-first';
6122
+ }
6123
+ else if (!watchQueryOptions.fetchPolicy) {
6124
+ var defaultOptions = this.client.defaultOptions.watchQuery;
6125
+ watchQueryOptions.fetchPolicy =
6126
+ defaultOptions && defaultOptions.fetchPolicy || 'cache-first';
6127
+ }
6128
+ if (!watchQueryOptions.variables) {
6129
+ watchQueryOptions.variables = {};
6130
+ }
6131
+ return watchQueryOptions;
6132
+ };
6133
+ InternalState.prototype.onCompleted = function (data) { };
6134
+ InternalState.prototype.onError = function (error) { };
6135
+ InternalState.prototype.useObservableQuery = function () {
6136
+ var _this = this;
6137
+ var obsQuery = this.observable =
6138
+ this.renderPromises
6139
+ && this.renderPromises.getSSRObservable(this.watchQueryOptions)
6140
+ || this.observable
6141
+ || this.client.watchQuery(this.watchQueryOptions);
6142
+ this.obsQueryFields = React.useMemo(function () { return ({
6143
+ refetch: obsQuery.refetch.bind(obsQuery),
6144
+ fetchMore: obsQuery.fetchMore.bind(obsQuery),
6145
+ updateQuery: obsQuery.updateQuery.bind(obsQuery),
6146
+ startPolling: obsQuery.startPolling.bind(obsQuery),
6147
+ stopPolling: obsQuery.stopPolling.bind(obsQuery),
6148
+ subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
6149
+ }); }, [obsQuery]);
6150
+ if (this.renderPromises) {
6151
+ this.renderPromises.registerSSRObservable(obsQuery);
6152
+ if (!this.ssrDisabled && obsQuery.getCurrentResult().loading) {
6153
+ this.renderPromises.addQueryPromise({
6154
+ getOptions: function () { return obsQuery.options; },
6155
+ fetchData: function () { return new Promise(function (resolve) {
6156
+ var sub = obsQuery.subscribe({
6157
+ next: function (result) {
6158
+ if (!result.loading) {
6159
+ resolve();
6160
+ sub.unsubscribe();
6161
+ }
6162
+ },
6163
+ error: function () {
6084
6164
  resolve();
6085
6165
  sub.unsubscribe();
6086
- }
6087
- },
6088
- error: function () {
6089
- resolve();
6090
- sub.unsubscribe();
6091
- },
6092
- complete: function () {
6093
- resolve();
6094
- },
6095
- });
6096
- }); },
6097
- }, function () { return null; });
6166
+ },
6167
+ complete: function () {
6168
+ resolve();
6169
+ },
6170
+ });
6171
+ }); },
6172
+ }, function () { return null; });
6173
+ obsQuery.setOptions(this.watchQueryOptions).catch(function () { });
6174
+ }
6098
6175
  }
6176
+ var prevOptionsRef = React.useRef({
6177
+ watchQueryOptions: this.watchQueryOptions,
6178
+ });
6179
+ React.useEffect(function () {
6180
+ if (_this.renderPromises) ;
6181
+ else if (_this.watchQueryOptions !== prevOptionsRef.current.watchQueryOptions) {
6182
+ obsQuery.setOptions(_this.watchQueryOptions).catch(function () { });
6183
+ prevOptionsRef.current.watchQueryOptions = _this.watchQueryOptions;
6184
+ _this.setResult(obsQuery.getCurrentResult());
6185
+ }
6186
+ }, [obsQuery, this.watchQueryOptions]);
6099
6187
  return obsQuery;
6100
- }), obsQuery = _b[0], setObsQuery = _b[1];
6101
- var _c = React.useState(function () {
6102
- var _a, _b;
6103
- var result = obsQuery.getCurrentResult();
6104
- if (!result.loading && options) {
6188
+ };
6189
+ InternalState.prototype.useSubscriptionEffect = function (obsQuery) {
6190
+ var _this = this;
6191
+ React.useEffect(function () {
6192
+ if (_this.renderPromises) {
6193
+ return;
6194
+ }
6195
+ var onNext = function () {
6196
+ var previousResult = _this.result;
6197
+ var result = obsQuery.getCurrentResult();
6198
+ if (previousResult &&
6199
+ previousResult.loading === result.loading &&
6200
+ previousResult.networkStatus === result.networkStatus &&
6201
+ equality.equal(previousResult.data, result.data)) {
6202
+ return;
6203
+ }
6204
+ _this.setResult(result);
6205
+ };
6206
+ var onError = function (error) {
6207
+ var last = obsQuery["last"];
6208
+ subscription.unsubscribe();
6209
+ try {
6210
+ obsQuery.resetLastResults();
6211
+ subscription = obsQuery.subscribe(onNext, onError);
6212
+ }
6213
+ finally {
6214
+ obsQuery["last"] = last;
6215
+ }
6216
+ if (!hasOwnProperty.call(error, 'graphQLErrors')) {
6217
+ throw error;
6218
+ }
6219
+ var previousResult = _this.result;
6220
+ if (!previousResult ||
6221
+ (previousResult && previousResult.loading) ||
6222
+ !equality.equal(error, previousResult.error)) {
6223
+ _this.setResult({
6224
+ data: (previousResult && previousResult.data),
6225
+ error: error,
6226
+ loading: false,
6227
+ networkStatus: exports.NetworkStatus.error,
6228
+ });
6229
+ }
6230
+ };
6231
+ var subscription = obsQuery.subscribe(onNext, onError);
6232
+ return function () { return subscription.unsubscribe(); };
6233
+ }, [
6234
+ obsQuery,
6235
+ this.renderPromises,
6236
+ this.client.disableNetworkFetches,
6237
+ ]);
6238
+ };
6239
+ InternalState.prototype.setResult = function (nextResult) {
6240
+ var previousResult = this.result;
6241
+ if (previousResult && previousResult.data) {
6242
+ this.previousData = previousResult.data;
6243
+ }
6244
+ this.result = nextResult;
6245
+ this.forceUpdate();
6246
+ this.handleErrorOrCompleted(nextResult);
6247
+ };
6248
+ InternalState.prototype.handleErrorOrCompleted = function (result) {
6249
+ if (!result.loading) {
6105
6250
  if (result.error) {
6106
- (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, result.error);
6251
+ this.onError(result.error);
6107
6252
  }
6108
6253
  else if (result.data) {
6109
- (_b = options.onCompleted) === null || _b === void 0 ? void 0 : _b.call(options, result.data);
6254
+ this.onCompleted(result.data);
6110
6255
  }
6111
6256
  }
6112
- return result;
6113
- }), result = _c[0], setResult = _c[1];
6114
- var ref = React.useRef({
6115
- client: client,
6116
- query: query,
6117
- options: options,
6118
- result: result,
6119
- previousData: void 0,
6120
- watchQueryOptions: createWatchQueryOptions(query, options, defaultWatchQueryOptions),
6121
- });
6122
- React.useEffect(function () {
6123
- var _a, _b;
6124
- var watchQueryOptions = createWatchQueryOptions(query, options, defaultWatchQueryOptions);
6125
- var nextResult;
6126
- if (ref.current.client !== client || !equality.equal(ref.current.query, query)) {
6127
- var obsQuery_1 = client.watchQuery(watchQueryOptions);
6128
- setObsQuery(obsQuery_1);
6129
- nextResult = obsQuery_1.getCurrentResult();
6130
- }
6131
- else if (!equality.equal(ref.current.watchQueryOptions, watchQueryOptions)) {
6132
- obsQuery.setOptions(watchQueryOptions).catch(function () { });
6133
- nextResult = obsQuery.getCurrentResult();
6134
- ref.current.watchQueryOptions = watchQueryOptions;
6135
- }
6136
- if (nextResult) {
6137
- var previousResult = ref.current.result;
6138
- if (previousResult.data) {
6139
- ref.current.previousData = previousResult.data;
6140
- }
6141
- setResult(ref.current.result = nextResult);
6142
- if (!nextResult.loading && options) {
6143
- if (nextResult.error) {
6144
- (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, nextResult.error);
6145
- }
6146
- else if (nextResult.data) {
6147
- (_b = options.onCompleted) === null || _b === void 0 ? void 0 : _b.call(options, nextResult.data);
6148
- }
6149
- }
6257
+ };
6258
+ InternalState.prototype.getCurrentResult = function () {
6259
+ var result = this.result;
6260
+ if (!result) {
6261
+ result = this.result = this.observable.getCurrentResult();
6262
+ this.handleErrorOrCompleted(result);
6150
6263
  }
6151
- Object.assign(ref.current, { client: client, query: query });
6152
- }, [obsQuery, client, query, options]);
6153
- React.useEffect(function () {
6154
- if (context.renderPromises) {
6155
- return;
6264
+ if ((this.renderPromises || this.client.disableNetworkFetches) &&
6265
+ this.queryHookOptions.ssr === false) {
6266
+ result = {
6267
+ loading: true,
6268
+ data: void 0,
6269
+ error: void 0,
6270
+ networkStatus: exports.NetworkStatus.loading,
6271
+ };
6156
6272
  }
6157
- var subscription = obsQuery.subscribe(onNext, onError);
6158
- function onNext() {
6159
- var _a, _b;
6160
- var previousResult = ref.current.result;
6161
- var result = obsQuery.getCurrentResult();
6162
- if (previousResult &&
6163
- previousResult.loading === result.loading &&
6164
- previousResult.networkStatus === result.networkStatus &&
6165
- equality.equal(previousResult.data, result.data)) {
6166
- return;
6167
- }
6168
- if (previousResult.data) {
6169
- ref.current.previousData = previousResult.data;
6170
- }
6171
- setResult(ref.current.result = result);
6172
- if (!result.loading) {
6173
- (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onCompleted) === null || _b === void 0 ? void 0 : _b.call(_a, result.data);
6174
- }
6273
+ else if (this.queryHookOptions.skip ||
6274
+ this.queryHookOptions.fetchPolicy === 'standby') {
6275
+ result = {
6276
+ loading: false,
6277
+ data: void 0,
6278
+ error: void 0,
6279
+ networkStatus: exports.NetworkStatus.ready,
6280
+ };
6175
6281
  }
6176
- function onError(error) {
6177
- var _a, _b;
6178
- var last = obsQuery["last"];
6179
- subscription.unsubscribe();
6180
- try {
6181
- obsQuery.resetLastResults();
6182
- subscription = obsQuery.subscribe(onNext, onError);
6183
- }
6184
- finally {
6185
- obsQuery["last"] = last;
6186
- }
6187
- if (!error.hasOwnProperty('graphQLErrors')) {
6188
- throw error;
6189
- }
6190
- var previousResult = ref.current.result;
6191
- if ((previousResult && previousResult.loading) ||
6192
- !equality.equal(error, previousResult.error)) {
6193
- setResult(ref.current.result = {
6194
- data: previousResult.data,
6195
- error: error,
6196
- loading: false,
6197
- networkStatus: exports.NetworkStatus.error,
6198
- });
6199
- (_b = (_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
6200
- }
6282
+ return result;
6283
+ };
6284
+ InternalState.prototype.toQueryResult = function (result) {
6285
+ var queryResult = this.toQueryResultCache.get(result);
6286
+ if (queryResult)
6287
+ return queryResult;
6288
+ var data = result.data; result.partial; var resultWithoutPartial = tslib.__rest(result, ["data", "partial"]);
6289
+ this.toQueryResultCache.set(result, queryResult = tslib.__assign(tslib.__assign(tslib.__assign({ data: data }, resultWithoutPartial), this.obsQueryFields), { client: this.client, observable: this.observable, variables: this.observable.variables, called: true, previousData: this.previousData }));
6290
+ if (!queryResult.error && isNonEmptyArray(result.errors)) {
6291
+ queryResult.error = new ApolloError({ graphQLErrors: result.errors });
6201
6292
  }
6202
- return function () { return subscription.unsubscribe(); };
6203
- }, [obsQuery, context.renderPromises, client.disableNetworkFetches]);
6204
- var partial;
6205
- (_a = result, partial = _a.partial, result = tslib.__rest(_a, ["partial"]));
6206
- {
6207
- if (partial &&
6208
- (options === null || options === void 0 ? void 0 : options.partialRefetch) &&
6293
+ return queryResult;
6294
+ };
6295
+ InternalState.prototype.unsafeHandlePartialRefetch = function (result) {
6296
+ if (result.partial &&
6297
+ this.queryHookOptions.partialRefetch &&
6209
6298
  !result.loading &&
6210
6299
  (!result.data || Object.keys(result.data).length === 0) &&
6211
- obsQuery.options.fetchPolicy !== 'cache-only') {
6212
- result = tslib.__assign(tslib.__assign({}, result), { loading: true, networkStatus: exports.NetworkStatus.refetch });
6213
- obsQuery.refetch();
6214
- }
6215
- if (context.renderPromises &&
6216
- (options === null || options === void 0 ? void 0 : options.ssr) !== false &&
6217
- !(options === null || options === void 0 ? void 0 : options.skip) &&
6218
- result.loading) {
6219
- obsQuery.setOptions(createWatchQueryOptions(query, options, defaultWatchQueryOptions)).catch(function () { });
6300
+ this.observable.options.fetchPolicy !== 'cache-only') {
6301
+ Object.assign(result, {
6302
+ loading: true,
6303
+ networkStatus: exports.NetworkStatus.refetch,
6304
+ });
6305
+ this.observable.refetch();
6220
6306
  }
6221
- Object.assign(ref.current, { options: options });
6222
- }
6223
- if ((context.renderPromises || client.disableNetworkFetches) &&
6224
- (options === null || options === void 0 ? void 0 : options.ssr) === false) {
6225
- result = ref.current.result = {
6226
- loading: true,
6227
- data: void 0,
6228
- error: void 0,
6229
- networkStatus: exports.NetworkStatus.loading,
6230
- };
6231
- }
6232
- else if ((options === null || options === void 0 ? void 0 : options.skip) || (options === null || options === void 0 ? void 0 : options.fetchPolicy) === 'standby') {
6233
- result = {
6234
- loading: false,
6235
- data: void 0,
6236
- error: void 0,
6237
- networkStatus: exports.NetworkStatus.ready,
6238
- };
6239
- }
6240
- if (result.errors && result.errors.length) {
6241
- result = tslib.__assign(tslib.__assign({}, result), { error: result.error || new ApolloError({ graphQLErrors: result.errors }) });
6242
- }
6243
- var obsQueryFields = React.useMemo(function () { return ({
6244
- refetch: obsQuery.refetch.bind(obsQuery),
6245
- fetchMore: obsQuery.fetchMore.bind(obsQuery),
6246
- updateQuery: obsQuery.updateQuery.bind(obsQuery),
6247
- startPolling: obsQuery.startPolling.bind(obsQuery),
6248
- stopPolling: obsQuery.stopPolling.bind(obsQuery),
6249
- subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
6250
- }); }, [obsQuery]);
6251
- return tslib.__assign(tslib.__assign(tslib.__assign({}, obsQueryFields), { variables: createWatchQueryOptions(query, options, defaultWatchQueryOptions).variables, client: client, called: true, previousData: ref.current.previousData }), result);
6252
- }
6253
- function createWatchQueryOptions(query, options, defaultOptions) {
6254
- var _a;
6255
- if (options === void 0) { options = {}; }
6256
- var skip = options.skip; options.ssr; options.onCompleted; options.onError; options.displayName; var otherOptions = tslib.__rest(options, ["skip", "ssr", "onCompleted", "onError", "displayName"]);
6257
- var watchQueryOptions = tslib.__assign({ query: query }, otherOptions);
6258
- if (defaultOptions) {
6259
- watchQueryOptions = mergeOptions(defaultOptions, watchQueryOptions);
6260
- }
6261
- if (skip) {
6262
- watchQueryOptions.fetchPolicy = 'standby';
6263
- }
6264
- else if (((_a = watchQueryOptions.context) === null || _a === void 0 ? void 0 : _a.renderPromises) &&
6265
- (watchQueryOptions.fetchPolicy === 'network-only' ||
6266
- watchQueryOptions.fetchPolicy === 'cache-and-network')) {
6267
- watchQueryOptions.fetchPolicy = 'cache-first';
6268
- }
6269
- else if (!watchQueryOptions.fetchPolicy) {
6270
- watchQueryOptions.fetchPolicy = 'cache-first';
6271
- }
6272
- if (!watchQueryOptions.variables) {
6273
- watchQueryOptions.variables = {};
6274
- }
6275
- return watchQueryOptions;
6276
- }
6307
+ };
6308
+ return InternalState;
6309
+ }());
6277
6310
 
6278
6311
  var EAGER_METHODS = [
6279
6312
  'refetch',