@apollo/client 3.5.10 → 3.6.5

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 (152) hide show
  1. package/LICENSE +1 -1
  2. package/apollo-client.cjs +541 -369
  3. package/apollo-client.cjs.map +1 -1
  4. package/apollo-client.min.cjs +1 -1
  5. package/cache/cache.cjs +9 -10
  6. package/cache/cache.cjs.map +1 -1
  7. package/cache/cache.cjs.native.js +2288 -0
  8. package/cache/inmemory/readFromStore.d.ts.map +1 -1
  9. package/cache/inmemory/readFromStore.js +10 -11
  10. package/cache/inmemory/readFromStore.js.map +1 -1
  11. package/core/ApolloClient.d.ts +2 -3
  12. package/core/ApolloClient.d.ts.map +1 -1
  13. package/core/ApolloClient.js +4 -8
  14. package/core/ApolloClient.js.map +1 -1
  15. package/core/ObservableQuery.d.ts +10 -4
  16. package/core/ObservableQuery.d.ts.map +1 -1
  17. package/core/ObservableQuery.js +97 -45
  18. package/core/ObservableQuery.js.map +1 -1
  19. package/core/QueryInfo.d.ts.map +1 -1
  20. package/core/QueryInfo.js +4 -2
  21. package/core/QueryInfo.js.map +1 -1
  22. package/core/QueryManager.d.ts +5 -2
  23. package/core/QueryManager.d.ts.map +1 -1
  24. package/core/QueryManager.js +29 -23
  25. package/core/QueryManager.js.map +1 -1
  26. package/core/core.cjs +138 -85
  27. package/core/core.cjs.map +1 -1
  28. package/core/core.cjs.native.js +2141 -0
  29. package/core/index.d.ts +1 -1
  30. package/core/index.d.ts.map +1 -1
  31. package/core/index.js +1 -1
  32. package/core/index.js.map +1 -1
  33. package/core/watchQueryOptions.d.ts +9 -1
  34. package/core/watchQueryOptions.d.ts.map +1 -1
  35. package/core/watchQueryOptions.js.map +1 -1
  36. package/errors/errors.cjs.native.js +48 -0
  37. package/invariantErrorCodes.js +1 -1
  38. package/link/batch/batch.cjs +47 -37
  39. package/link/batch/batch.cjs.map +1 -1
  40. package/link/batch/batch.cjs.native.js +161 -0
  41. package/link/batch/batching.d.ts +2 -6
  42. package/link/batch/batching.d.ts.map +1 -1
  43. package/link/batch/batching.js +47 -37
  44. package/link/batch/batching.js.map +1 -1
  45. package/link/batch-http/batch-http.cjs.native.js +127 -0
  46. package/link/context/context.cjs.native.js +38 -0
  47. package/link/core/core.cjs.native.js +121 -0
  48. package/link/error/error.cjs.native.js +90 -0
  49. package/link/http/http.cjs.native.js +320 -0
  50. package/link/http/selectHttpOptionsAndBody.d.ts.map +1 -1
  51. package/link/http/selectHttpOptionsAndBody.js.map +1 -1
  52. package/link/persisted-queries/index.d.ts +2 -1
  53. package/link/persisted-queries/index.d.ts.map +1 -1
  54. package/link/persisted-queries/index.js +26 -13
  55. package/link/persisted-queries/index.js.map +1 -1
  56. package/link/persisted-queries/persisted-queries.cjs +25 -12
  57. package/link/persisted-queries/persisted-queries.cjs.map +1 -1
  58. package/link/persisted-queries/persisted-queries.cjs.native.js +174 -0
  59. package/link/retry/retry.cjs.native.js +170 -0
  60. package/link/schema/schema.cjs.native.js +56 -0
  61. package/link/subscriptions/subscriptions.cjs.native.js +45 -0
  62. package/link/utils/utils.cjs.native.js +115 -0
  63. package/link/ws/ws.cjs.native.js +28 -0
  64. package/main.cjs.native.js +16 -0
  65. package/package.json +28 -26
  66. package/react/components/components.cjs.native.js +79 -0
  67. package/react/context/ApolloConsumer.js +2 -2
  68. package/react/context/ApolloProvider.js +2 -2
  69. package/react/context/context.cjs +4 -4
  70. package/react/context/context.cjs.map +1 -1
  71. package/react/context/context.cjs.native.js +67 -0
  72. package/react/hoc/hoc.cjs.native.js +325 -0
  73. package/react/hooks/hooks.cjs +365 -253
  74. package/react/hooks/hooks.cjs.map +1 -1
  75. package/react/hooks/hooks.cjs.native.js +612 -0
  76. package/react/hooks/index.d.ts +1 -1
  77. package/react/hooks/index.d.ts.map +1 -1
  78. package/react/hooks/index.js +1 -1
  79. package/react/hooks/index.js.map +1 -1
  80. package/react/hooks/useLazyQuery.d.ts +2 -2
  81. package/react/hooks/useLazyQuery.d.ts.map +1 -1
  82. package/react/hooks/useLazyQuery.js +26 -21
  83. package/react/hooks/useLazyQuery.js.map +1 -1
  84. package/react/hooks/useMutation.d.ts.map +1 -1
  85. package/react/hooks/useMutation.js +13 -10
  86. package/react/hooks/useMutation.js.map +1 -1
  87. package/react/hooks/useQuery.d.ts +36 -2
  88. package/react/hooks/useQuery.d.ts.map +1 -1
  89. package/react/hooks/useQuery.js +245 -206
  90. package/react/hooks/useQuery.js.map +1 -1
  91. package/react/hooks/useSubscription.d.ts.map +1 -1
  92. package/react/hooks/useSubscription.js +17 -7
  93. package/react/hooks/useSubscription.js.map +1 -1
  94. package/react/hooks/useSyncExternalStore.d.ts +4 -0
  95. package/react/hooks/useSyncExternalStore.d.ts.map +1 -0
  96. package/react/hooks/useSyncExternalStore.js +48 -0
  97. package/react/hooks/useSyncExternalStore.js.map +1 -0
  98. package/react/parser/index.d.ts.map +1 -1
  99. package/react/parser/index.js +24 -10
  100. package/react/parser/index.js.map +1 -1
  101. package/react/parser/parser.cjs +24 -10
  102. package/react/parser/parser.cjs.map +1 -1
  103. package/react/parser/parser.cjs.native.js +103 -0
  104. package/react/react.cjs.native.js +22 -0
  105. package/react/ssr/RenderPromises.d.ts +3 -2
  106. package/react/ssr/RenderPromises.d.ts.map +1 -1
  107. package/react/ssr/RenderPromises.js +25 -3
  108. package/react/ssr/RenderPromises.js.map +1 -1
  109. package/react/ssr/ssr.cjs +25 -3
  110. package/react/ssr/ssr.cjs.map +1 -1
  111. package/react/ssr/ssr.cjs.native.js +150 -0
  112. package/react/types/types.d.ts +10 -11
  113. package/react/types/types.d.ts.map +1 -1
  114. package/react/types/types.js.map +1 -1
  115. package/testing/core/core.cjs.native.js +288 -0
  116. package/testing/core/mocking/mockFetch.js +1 -1
  117. package/testing/core/mocking/mockFetch.js.map +1 -1
  118. package/testing/core/mocking/mockQueryManager.js +1 -1
  119. package/testing/core/mocking/mockWatchQuery.js +1 -1
  120. package/testing/core/wrap.js +1 -1
  121. package/testing/testing.cjs.native.js +58 -0
  122. package/utilities/common/canUse.d.ts +2 -0
  123. package/utilities/common/canUse.d.ts.map +1 -1
  124. package/utilities/common/canUse.js +6 -2
  125. package/utilities/common/canUse.js.map +1 -1
  126. package/utilities/common/mergeDeep.d.ts.map +1 -1
  127. package/utilities/common/mergeDeep.js +8 -11
  128. package/utilities/common/mergeDeep.js.map +1 -1
  129. package/utilities/common/mergeOptions.d.ts +5 -0
  130. package/utilities/common/mergeOptions.d.ts.map +1 -0
  131. package/utilities/common/mergeOptions.js +8 -0
  132. package/utilities/common/mergeOptions.js.map +1 -0
  133. package/utilities/globals/global.js +1 -2
  134. package/utilities/globals/globals.cjs.native.js +56 -0
  135. package/utilities/graphql/transform.d.ts +2 -2
  136. package/utilities/graphql/transform.d.ts.map +1 -1
  137. package/utilities/graphql/transform.js +1 -1
  138. package/utilities/graphql/transform.js.map +1 -1
  139. package/utilities/index.d.ts +1 -0
  140. package/utilities/index.d.ts.map +1 -1
  141. package/utilities/index.js +1 -0
  142. package/utilities/index.js.map +1 -1
  143. package/utilities/observables/Concast.d.ts.map +1 -1
  144. package/utilities/observables/Concast.js +5 -2
  145. package/utilities/observables/Concast.js.map +1 -1
  146. package/utilities/policies/pagination.d.ts.map +1 -1
  147. package/utilities/policies/pagination.js +9 -7
  148. package/utilities/policies/pagination.js.map +1 -1
  149. package/utilities/utilities.cjs +37 -23
  150. package/utilities/utilities.cjs.map +1 -1
  151. package/utilities/utilities.cjs.native.js +1281 -0
  152. package/version.js +1 -1
@@ -3,7 +3,7 @@ import { Observable } from "../../utilities/index.js";
3
3
  var OperationBatcher = (function () {
4
4
  function OperationBatcher(_a) {
5
5
  var batchDebounce = _a.batchDebounce, batchInterval = _a.batchInterval, batchMax = _a.batchMax, batchHandler = _a.batchHandler, batchKey = _a.batchKey;
6
- this.queuedRequests = new Map();
6
+ this.batchesByKey = new Map();
7
7
  this.batchDebounce = batchDebounce;
8
8
  this.batchInterval = batchInterval;
9
9
  this.batchMax = batchMax || 0;
@@ -12,61 +12,75 @@ var OperationBatcher = (function () {
12
12
  }
13
13
  OperationBatcher.prototype.enqueueRequest = function (request) {
14
14
  var _this = this;
15
- var requestCopy = __assign({}, request);
16
- var queued = false;
15
+ var requestCopy = __assign(__assign({}, request), { next: [], error: [], complete: [], subscribers: new Set() });
17
16
  var key = this.batchKey(request.operation);
18
17
  if (!requestCopy.observable) {
19
18
  requestCopy.observable = new Observable(function (observer) {
20
- if (!_this.queuedRequests.has(key)) {
21
- _this.queuedRequests.set(key, []);
19
+ var batch = _this.batchesByKey.get(key);
20
+ if (!batch)
21
+ _this.batchesByKey.set(key, batch = new Set());
22
+ var isFirstEnqueuedRequest = batch.size === 0;
23
+ var isFirstSubscriber = requestCopy.subscribers.size === 0;
24
+ requestCopy.subscribers.add(observer);
25
+ if (isFirstSubscriber) {
26
+ batch.add(requestCopy);
22
27
  }
23
- if (!queued) {
24
- _this.queuedRequests.get(key).push(requestCopy);
25
- queued = true;
26
- }
27
- requestCopy.next = requestCopy.next || [];
28
- if (observer.next)
28
+ if (observer.next) {
29
29
  requestCopy.next.push(observer.next.bind(observer));
30
- requestCopy.error = requestCopy.error || [];
31
- if (observer.error)
30
+ }
31
+ if (observer.error) {
32
32
  requestCopy.error.push(observer.error.bind(observer));
33
- requestCopy.complete = requestCopy.complete || [];
34
- if (observer.complete)
33
+ }
34
+ if (observer.complete) {
35
35
  requestCopy.complete.push(observer.complete.bind(observer));
36
- if (_this.queuedRequests.get(key).length === 1) {
36
+ }
37
+ if (isFirstEnqueuedRequest) {
37
38
  _this.scheduleQueueConsumption(key);
38
39
  }
39
40
  else if (_this.batchDebounce) {
40
41
  clearTimeout(_this.scheduledBatchTimer);
41
42
  _this.scheduleQueueConsumption(key);
42
43
  }
43
- if (_this.queuedRequests.get(key).length === _this.batchMax) {
44
+ if (batch.size === _this.batchMax) {
44
45
  _this.consumeQueue(key);
45
46
  }
47
+ return function () {
48
+ var _a;
49
+ if (requestCopy.subscribers.delete(observer) &&
50
+ requestCopy.subscribers.size < 1) {
51
+ if (batch.delete(requestCopy) && batch.size < 1) {
52
+ clearTimeout(_this.scheduledBatchTimer);
53
+ _this.batchesByKey.delete(key);
54
+ (_a = batch.subscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
55
+ }
56
+ }
57
+ };
46
58
  });
47
59
  }
48
60
  return requestCopy.observable;
49
61
  };
50
62
  OperationBatcher.prototype.consumeQueue = function (key) {
51
- var requestKey = key || '';
52
- var queuedRequests = this.queuedRequests.get(requestKey);
53
- if (!queuedRequests) {
63
+ if (key === void 0) { key = ''; }
64
+ var batch = this.batchesByKey.get(key);
65
+ this.batchesByKey.delete(key);
66
+ if (!batch || !batch.size) {
54
67
  return;
55
68
  }
56
- this.queuedRequests.delete(requestKey);
57
- var requests = queuedRequests.map(function (queuedRequest) { return queuedRequest.operation; });
58
- var forwards = queuedRequests.map(function (queuedRequest) { return queuedRequest.forward; });
69
+ var operations = [];
70
+ var forwards = [];
59
71
  var observables = [];
60
72
  var nexts = [];
61
73
  var errors = [];
62
74
  var completes = [];
63
- queuedRequests.forEach(function (batchableRequest, index) {
64
- observables.push(batchableRequest.observable);
65
- nexts.push(batchableRequest.next);
66
- errors.push(batchableRequest.error);
67
- completes.push(batchableRequest.complete);
75
+ batch.forEach(function (request) {
76
+ operations.push(request.operation);
77
+ forwards.push(request.forward);
78
+ observables.push(request.observable);
79
+ nexts.push(request.next);
80
+ errors.push(request.error);
81
+ completes.push(request.complete);
68
82
  });
69
- var batchedObservable = this.batchHandler(requests, forwards) || Observable.of();
83
+ var batchedObservable = this.batchHandler(operations, forwards) || Observable.of();
70
84
  var onError = function (error) {
71
85
  errors.forEach(function (rejecters) {
72
86
  if (rejecters) {
@@ -74,7 +88,7 @@ var OperationBatcher = (function () {
74
88
  }
75
89
  });
76
90
  };
77
- batchedObservable.subscribe({
91
+ batch.subscription = batchedObservable.subscribe({
78
92
  next: function (results) {
79
93
  if (!Array.isArray(results)) {
80
94
  results = [results];
@@ -103,13 +117,9 @@ var OperationBatcher = (function () {
103
117
  };
104
118
  OperationBatcher.prototype.scheduleQueueConsumption = function (key) {
105
119
  var _this = this;
106
- var requestKey = key || '';
107
- this.scheduledBatchTimer = (setTimeout(function () {
108
- if (_this.queuedRequests.get(requestKey) &&
109
- _this.queuedRequests.get(requestKey).length) {
110
- _this.consumeQueue(requestKey);
111
- }
112
- }, this.batchInterval));
120
+ this.scheduledBatchTimer = setTimeout(function () {
121
+ _this.consumeQueue(key);
122
+ }, this.batchInterval);
113
123
  };
114
124
  return OperationBatcher;
115
125
  }());
@@ -1 +1 @@
1
- {"version":3,"file":"batching.js","sourceRoot":"","sources":["../../../src/link/batch/batching.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAuB7C;IAcE,0BAAY,EAYX;YAXC,aAAa,mBAAA,EACb,aAAa,mBAAA,EACb,QAAQ,cAAA,EACR,YAAY,kBAAA,EACZ,QAAQ,cAAA;QAQR,IAAI,CAAC,cAAc,GAAG,IAAI,GAAG,EAAE,CAAC;QAChC,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,CAAC,CAAC;QAC9B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,QAAQ,GAAG,QAAQ,IAAI,CAAC,cAAM,OAAA,EAAE,EAAF,CAAE,CAAC,CAAC;IACzC,CAAC;IAEM,yCAAc,GAArB,UAAsB,OAAyB;QAA/C,iBA+CC;QA9CC,IAAM,WAAW,gBACZ,OAAO,CACX,CAAC;QACF,IAAI,MAAM,GAAG,KAAK,CAAC;QAEnB,IAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAE7C,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE;YAC3B,WAAW,CAAC,UAAU,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EZ,QAAQ,CAAC,OAAO,CAAC,UAAC,CAAM,IAAK,OAAA,CAAC,EAAE,EAAH,CAAG,CAAC,CAAC;qBACnC;gBACH,CAAC,CAAC,CAAC;YACL,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,WAAW,CAAC;IACrB,CAAC;IAEO,mDAAwB,GAAhC,UAAiC,GAAY;QAA7C,iBAUC;QATC,IAAM,UAAU,GAAG,GAAG,IAAI,EAAE,CAAC;QAC7B,IAAI,CAAC,mBAAmB,GAAG,CAAC,UAAU,CAAC;YACrC,IACE,KAAI,CAAC,cAAc,CAAC,GAAG,CAAC,UAAU,CAAC;gBACnC,KAAI,CAAC,cAAc,CAAC,GAAG,CAAC,UAAU,CAAE,CAAC,MAAM,EAC3C;gBACA,KAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;aAC/B;QACH,CAAC,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC;IAC1B,CAAC;IACH,uBAAC;AAAD,CAAC,AAlLD,IAkLC","sourcesContent":["import { Operation, FetchResult, NextLink } from '../core';\nimport { Observable } from '../../utilities';\n\nexport type BatchHandler = (\n operations: Operation[],\n forward?: (NextLink | undefined)[],\n) => Observable<FetchResult[]> | null;\n\nexport interface BatchableRequest {\n operation: Operation;\n forward?: NextLink;\n\n // promise is created when the query fetch request is\n // added to the queue and is resolved once the result is back\n // from the server.\n observable?: Observable<FetchResult>;\n next?: Array<(result: FetchResult) => void>;\n error?: Array<(error: Error) => void>;\n complete?: Array<() => void>;\n}\n\n// QueryBatcher doesn't fire requests immediately. Requests that were enqueued within\n// a certain amount of time (configurable through `batchInterval`) will be batched together\n// into one query.\nexport class OperationBatcher {\n // Queue on which the QueryBatcher will operate on a per-tick basis.\n // Public only for testing\n public queuedRequests: Map<string, BatchableRequest[]>;\n\n private scheduledBatchTimer: ReturnType<typeof setTimeout>;\n private batchDebounce?: boolean;\n private batchInterval?: number;\n private batchMax: number;\n\n //This function is called to the queries in the queue to the server.\n private batchHandler: BatchHandler;\n private batchKey: (operation: Operation) => string;\n\n constructor({\n batchDebounce,\n batchInterval,\n batchMax,\n batchHandler,\n batchKey,\n }: {\n batchDebounce?: boolean;\n batchInterval?: number;\n batchMax?: number;\n batchHandler: BatchHandler;\n batchKey?: (operation: Operation) => string;\n }) {\n this.queuedRequests = new Map();\n this.batchDebounce = batchDebounce;\n this.batchInterval = batchInterval;\n this.batchMax = batchMax || 0;\n this.batchHandler = batchHandler;\n this.batchKey = batchKey || (() => '');\n }\n\n public enqueueRequest(request: BatchableRequest): Observable<FetchResult> {\n const requestCopy = {\n ...request,\n };\n let queued = false;\n\n const key = this.batchKey(request.operation);\n\n if (!requestCopy.observable) {\n requestCopy.observable = new Observable<FetchResult>(observer => {\n if (!this.queuedRequests.has(key)) {\n this.queuedRequests.set(key, []);\n }\n\n if (!queued) {\n this.queuedRequests.get(key)!.push(requestCopy);\n queued = true;\n }\n\n //called for each subscriber, so need to save all listeners(next, error, complete)\n requestCopy.next = requestCopy.next || [];\n if (observer.next) requestCopy.next.push(observer.next.bind(observer));\n\n requestCopy.error = requestCopy.error || [];\n if (observer.error)\n requestCopy.error.push(observer.error.bind(observer));\n\n requestCopy.complete = requestCopy.complete || [];\n if (observer.complete)\n requestCopy.complete.push(observer.complete.bind(observer));\n\n // The first enqueued request triggers the queue consumption after `batchInterval` milliseconds.\n if (this.queuedRequests.get(key)!.length === 1) {\n this.scheduleQueueConsumption(key);\n } else if (this.batchDebounce) {\n clearTimeout(this.scheduledBatchTimer);\n this.scheduleQueueConsumption(key);\n }\n\n // When amount of requests reaches `batchMax`, trigger the queue consumption without waiting on the `batchInterval`.\n if (this.queuedRequests.get(key)!.length === this.batchMax) {\n this.consumeQueue(key);\n }\n });\n }\n\n return requestCopy.observable;\n }\n\n // Consumes the queue.\n // Returns a list of promises (one for each query).\n public consumeQueue(\n key?: string,\n ): (Observable<FetchResult> | undefined)[] | undefined {\n const requestKey = key || '';\n const queuedRequests = this.queuedRequests.get(requestKey);\n\n if (!queuedRequests) {\n return;\n }\n\n this.queuedRequests.delete(requestKey);\n\n const requests: Operation[] = queuedRequests.map(\n queuedRequest => queuedRequest.operation,\n );\n\n const forwards: (NextLink | undefined)[] = queuedRequests.map(\n queuedRequest => queuedRequest.forward,\n );\n\n const observables: (Observable<FetchResult> | undefined)[] = [];\n const nexts: any[] = [];\n const errors: any[] = [];\n const completes: any[] = [];\n queuedRequests.forEach((batchableRequest, index) => {\n observables.push(batchableRequest.observable);\n nexts.push(batchableRequest.next);\n errors.push(batchableRequest.error);\n completes.push(batchableRequest.complete);\n });\n\n const batchedObservable =\n this.batchHandler(requests, forwards) || Observable.of();\n\n const onError = (error: any) => {\n //each callback list in batch\n errors.forEach(rejecters => {\n if (rejecters) {\n //each subscriber to request\n rejecters.forEach((e: any) => e(error));\n }\n });\n };\n\n batchedObservable.subscribe({\n next: results => {\n if (!Array.isArray(results)) {\n results = [results];\n }\n\n if (nexts.length !== results.length) {\n const error = new Error(\n `server returned results with length ${\n results.length\n }, expected length of ${nexts.length}`,\n );\n (error as any).result = results;\n\n return onError(error);\n }\n\n results.forEach((result, index) => {\n if (nexts[index]) {\n nexts[index].forEach((next: any) => next(result));\n }\n });\n },\n error: onError,\n complete: () => {\n completes.forEach(complete => {\n if (complete) {\n //each subscriber to request\n complete.forEach((c: any) => c());\n }\n });\n },\n });\n\n return observables;\n }\n\n private scheduleQueueConsumption(key?: string): void {\n const requestKey = key || '';\n this.scheduledBatchTimer = (setTimeout(() => {\n if (\n this.queuedRequests.get(requestKey) &&\n this.queuedRequests.get(requestKey)!.length\n ) {\n this.consumeQueue(requestKey);\n }\n }, this.batchInterval));\n }\n}\n"]}
1
+ {"version":3,"file":"batching.js","sourceRoot":"","sources":["../../../src/link/batch/batching.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,UAAU,EAA0B,MAAM,iBAAiB,CAAC;AA6BrE;IAaE,0BAAY,EAYX;YAXC,aAAa,mBAAA,EACb,aAAa,mBAAA,EACb,QAAQ,cA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C,UAAiC,GAAW;QAA5C,iBAIC;QAHC,IAAI,CAAC,mBAAmB,GAAG,UAAU,CAAC;YACpC,KAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;IACzB,CAAC;IACH,uBAAC;AAAD,CAAC,AA/LD,IA+LC","sourcesContent":["import { FetchResult, NextLink, Operation } from '../core';\nimport { Observable, ObservableSubscription } from '../../utilities';\n\nexport type BatchHandler = (\n operations: Operation[],\n forward?: (NextLink | undefined)[],\n) => Observable<FetchResult[]> | null;\n\nexport interface BatchableRequest {\n operation: Operation;\n forward?: NextLink;\n}\n\ntype QueuedRequest = BatchableRequest & {\n observable?: Observable<FetchResult>;\n next: Array<(result: FetchResult) => void>;\n error: Array<(error: Error) => void>;\n complete: Array<() => void>;\n subscribers: Set<object>;\n}\n\n// Batches are primarily a Set<QueuedRequest>, but may have other optional\n// properties, such as batch.subscription.\ntype RequestBatch = Set<QueuedRequest> & {\n subscription?: ObservableSubscription;\n}\n\n// QueryBatcher doesn't fire requests immediately. Requests that were enqueued within\n// a certain amount of time (configurable through `batchInterval`) will be batched together\n// into one query.\nexport class OperationBatcher {\n // Queue on which the QueryBatcher will operate on a per-tick basis.\n private batchesByKey = new Map<string, RequestBatch>();\n\n private scheduledBatchTimer: ReturnType<typeof setTimeout>;\n private batchDebounce?: boolean;\n private batchInterval?: number;\n private batchMax: number;\n\n //This function is called to the queries in the queue to the server.\n private batchHandler: BatchHandler;\n private batchKey: (operation: Operation) => string;\n\n constructor({\n batchDebounce,\n batchInterval,\n batchMax,\n batchHandler,\n batchKey,\n }: {\n batchDebounce?: boolean;\n batchInterval?: number;\n batchMax?: number;\n batchHandler: BatchHandler;\n batchKey?: (operation: Operation) => string;\n }) {\n this.batchDebounce = batchDebounce;\n this.batchInterval = batchInterval;\n this.batchMax = batchMax || 0;\n this.batchHandler = batchHandler;\n this.batchKey = batchKey || (() => '');\n }\n\n public enqueueRequest(request: BatchableRequest): Observable<FetchResult> {\n const requestCopy: QueuedRequest = {\n ...request,\n next: [],\n error: [],\n complete: [],\n subscribers: new Set(),\n };\n\n const key = this.batchKey(request.operation);\n\n if (!requestCopy.observable) {\n requestCopy.observable = new Observable<FetchResult>(observer => {\n let batch = this.batchesByKey.get(key)!;\n if (!batch) this.batchesByKey.set(key, batch = new Set());\n\n // These booleans seem to me (@benjamn) like they might always be the\n // same (and thus we could do with only one of them), but I'm not 100%\n // sure about that.\n const isFirstEnqueuedRequest = batch.size === 0;\n const isFirstSubscriber = requestCopy.subscribers.size === 0;\n requestCopy.subscribers.add(observer);\n if (isFirstSubscriber) {\n batch.add(requestCopy);\n }\n\n // called for each subscriber, so need to save all listeners (next, error, complete)\n if (observer.next) {\n requestCopy.next.push(observer.next.bind(observer));\n }\n\n if (observer.error) {\n requestCopy.error.push(observer.error.bind(observer));\n }\n\n if (observer.complete) {\n requestCopy.complete.push(observer.complete.bind(observer));\n }\n\n // The first enqueued request triggers the queue consumption after `batchInterval` milliseconds.\n if (isFirstEnqueuedRequest) {\n this.scheduleQueueConsumption(key);\n } else if (this.batchDebounce) {\n clearTimeout(this.scheduledBatchTimer);\n this.scheduleQueueConsumption(key);\n }\n\n // When amount of requests reaches `batchMax`, trigger the queue consumption without waiting on the `batchInterval`.\n if (batch.size === this.batchMax) {\n this.consumeQueue(key);\n }\n\n return () => {\n // If this is last subscriber for this request, remove request from queue\n if (requestCopy.subscribers.delete(observer) &&\n requestCopy.subscribers.size < 1) {\n // If this is last request from queue, remove queue entirely\n if (batch.delete(requestCopy) && batch.size < 1) {\n clearTimeout(this.scheduledBatchTimer);\n this.batchesByKey.delete(key);\n // If queue was in flight, cancel it\n batch.subscription?.unsubscribe();\n }\n }\n }\n });\n }\n\n return requestCopy.observable;\n }\n\n // Consumes the queue.\n // Returns a list of promises (one for each query).\n public consumeQueue(\n key: string = '',\n ): (Observable<FetchResult> | undefined)[] | undefined {\n const batch = this.batchesByKey.get(key);\n // Delete this batch and process it below.\n this.batchesByKey.delete(key);\n if (!batch || !batch.size) {\n // No requests to be processed.\n return;\n }\n\n const operations: QueuedRequest['operation'][] = [];\n const forwards: QueuedRequest['forward'][] = [];\n const observables: QueuedRequest['observable'][] = [];\n const nexts: QueuedRequest['next'][] = [];\n const errors: QueuedRequest['error'][] = [];\n const completes: QueuedRequest['complete'][] = [];\n\n // Even though batch is a Set, it preserves the order of first insertion\n // when iterating (per ECMAScript specification), so these requests will be\n // handled in the order they were enqueued (minus any deleted ones).\n batch.forEach(request => {\n operations.push(request.operation);\n forwards.push(request.forward);\n observables.push(request.observable);\n nexts.push(request.next);\n errors.push(request.error);\n completes.push(request.complete);\n });\n\n const batchedObservable =\n this.batchHandler(operations, forwards) || Observable.of();\n\n const onError = (error: Error) => {\n //each callback list in batch\n errors.forEach(rejecters => {\n if (rejecters) {\n //each subscriber to request\n rejecters.forEach((e) => e(error));\n }\n });\n };\n\n batch.subscription = batchedObservable.subscribe({\n next: results => {\n if (!Array.isArray(results)) {\n results = [results];\n }\n\n if (nexts.length !== results.length) {\n const error = new Error(\n `server returned results with length ${\n results.length\n }, expected length of ${nexts.length}`,\n );\n (error as any).result = results;\n\n return onError(error);\n }\n\n results.forEach((result, index) => {\n if (nexts[index]) {\n nexts[index].forEach((next) => next(result));\n }\n });\n },\n error: onError,\n complete: () => {\n completes.forEach(complete => {\n if (complete) {\n //each subscriber to request\n complete.forEach((c) => c());\n }\n });\n },\n });\n\n return observables;\n }\n\n private scheduleQueueConsumption(key: string): void {\n this.scheduledBatchTimer = setTimeout(() => {\n this.consumeQueue(key);\n }, this.batchInterval);\n }\n}\n"]}
@@ -0,0 +1,127 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var core = require('../core');
7
+ var utilities = require('../../utilities');
8
+ var utils = require('../utils');
9
+ var http = require('../http');
10
+ var batch = require('../batch');
11
+
12
+ var BatchHttpLink = (function (_super) {
13
+ tslib.__extends(BatchHttpLink, _super);
14
+ function BatchHttpLink(fetchParams) {
15
+ var _this = _super.call(this) || this;
16
+ var _a = fetchParams || {}, _b = _a.uri, uri = _b === void 0 ? '/graphql' : _b, fetcher = _a.fetch, _c = _a.print, print = _c === void 0 ? http.defaultPrinter : _c, includeExtensions = _a.includeExtensions, batchInterval = _a.batchInterval, batchDebounce = _a.batchDebounce, batchMax = _a.batchMax, batchKey = _a.batchKey, requestOptions = tslib.__rest(_a, ["uri", "fetch", "print", "includeExtensions", "batchInterval", "batchDebounce", "batchMax", "batchKey"]);
17
+ http.checkFetcher(fetcher);
18
+ if (!fetcher) {
19
+ fetcher = fetch;
20
+ }
21
+ var linkConfig = {
22
+ http: { includeExtensions: includeExtensions },
23
+ options: requestOptions.fetchOptions,
24
+ credentials: requestOptions.credentials,
25
+ headers: requestOptions.headers,
26
+ };
27
+ _this.batchDebounce = batchDebounce;
28
+ _this.batchInterval = batchInterval || 10;
29
+ _this.batchMax = batchMax || 10;
30
+ var batchHandler = function (operations) {
31
+ var chosenURI = http.selectURI(operations[0], uri);
32
+ var context = operations[0].getContext();
33
+ var clientAwarenessHeaders = {};
34
+ if (context.clientAwareness) {
35
+ var _a = context.clientAwareness, name_1 = _a.name, version = _a.version;
36
+ if (name_1) {
37
+ clientAwarenessHeaders['apollographql-client-name'] = name_1;
38
+ }
39
+ if (version) {
40
+ clientAwarenessHeaders['apollographql-client-version'] = version;
41
+ }
42
+ }
43
+ var contextConfig = {
44
+ http: context.http,
45
+ options: context.fetchOptions,
46
+ credentials: context.credentials,
47
+ headers: tslib.__assign(tslib.__assign({}, clientAwarenessHeaders), context.headers),
48
+ };
49
+ var optsAndBody = operations.map(function (operation) {
50
+ return http.selectHttpOptionsAndBodyInternal(operation, print, http.fallbackHttpConfig, linkConfig, contextConfig);
51
+ });
52
+ var loadedBody = optsAndBody.map(function (_a) {
53
+ var body = _a.body;
54
+ return body;
55
+ });
56
+ var options = optsAndBody[0].options;
57
+ if (options.method === 'GET') {
58
+ return utils.fromError(new Error('apollo-link-batch-http does not support GET requests'));
59
+ }
60
+ try {
61
+ options.body = http.serializeFetchParameter(loadedBody, 'Payload');
62
+ }
63
+ catch (parseError) {
64
+ return utils.fromError(parseError);
65
+ }
66
+ var controller;
67
+ if (!options.signal) {
68
+ var _b = http.createSignalIfSupported(), _controller = _b.controller, signal = _b.signal;
69
+ controller = _controller;
70
+ if (controller)
71
+ options.signal = signal;
72
+ }
73
+ return new utilities.Observable(function (observer) {
74
+ fetcher(chosenURI, options)
75
+ .then(function (response) {
76
+ operations.forEach(function (operation) { return operation.setContext({ response: response }); });
77
+ return response;
78
+ })
79
+ .then(http.parseAndCheckHttpResponse(operations))
80
+ .then(function (result) {
81
+ observer.next(result);
82
+ observer.complete();
83
+ return result;
84
+ })
85
+ .catch(function (err) {
86
+ if (err.name === 'AbortError')
87
+ return;
88
+ if (err.result && err.result.errors && err.result.data) {
89
+ observer.next(err.result);
90
+ }
91
+ observer.error(err);
92
+ });
93
+ return function () {
94
+ if (controller)
95
+ controller.abort();
96
+ };
97
+ });
98
+ };
99
+ batchKey =
100
+ batchKey ||
101
+ (function (operation) {
102
+ var context = operation.getContext();
103
+ var contextConfig = {
104
+ http: context.http,
105
+ options: context.fetchOptions,
106
+ credentials: context.credentials,
107
+ headers: context.headers,
108
+ };
109
+ return http.selectURI(operation, uri) + JSON.stringify(contextConfig);
110
+ });
111
+ _this.batcher = new batch.BatchLink({
112
+ batchDebounce: _this.batchDebounce,
113
+ batchInterval: _this.batchInterval,
114
+ batchMax: _this.batchMax,
115
+ batchKey: batchKey,
116
+ batchHandler: batchHandler,
117
+ });
118
+ return _this;
119
+ }
120
+ BatchHttpLink.prototype.request = function (operation) {
121
+ return this.batcher.request(operation);
122
+ };
123
+ return BatchHttpLink;
124
+ }(core.ApolloLink));
125
+
126
+ exports.BatchHttpLink = BatchHttpLink;
127
+ //# sourceMappingURL=batch-http.cjs.map
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var core = require('../core');
7
+ var utilities = require('../../utilities');
8
+
9
+ function setContext(setter) {
10
+ return new core.ApolloLink(function (operation, forward) {
11
+ var request = tslib.__rest(operation, []);
12
+ return new utilities.Observable(function (observer) {
13
+ var handle;
14
+ var closed = false;
15
+ Promise.resolve(request)
16
+ .then(function (req) { return setter(req, operation.getContext()); })
17
+ .then(operation.setContext)
18
+ .then(function () {
19
+ if (closed)
20
+ return;
21
+ handle = forward(operation).subscribe({
22
+ next: observer.next.bind(observer),
23
+ error: observer.error.bind(observer),
24
+ complete: observer.complete.bind(observer),
25
+ });
26
+ })
27
+ .catch(observer.error.bind(observer));
28
+ return function () {
29
+ closed = true;
30
+ if (handle)
31
+ handle.unsubscribe();
32
+ };
33
+ });
34
+ });
35
+ }
36
+
37
+ exports.setContext = setContext;
38
+ //# sourceMappingURL=context.cjs.map
@@ -0,0 +1,121 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var globals = require('../../utilities/globals');
6
+ var tslib = require('tslib');
7
+ var utilities = require('../../utilities');
8
+ var utils = require('../utils');
9
+
10
+ function passthrough(op, forward) {
11
+ return (forward ? forward(op) : utilities.Observable.of());
12
+ }
13
+ function toLink(handler) {
14
+ return typeof handler === 'function' ? new ApolloLink(handler) : handler;
15
+ }
16
+ function isTerminating(link) {
17
+ return link.request.length <= 1;
18
+ }
19
+ var LinkError = (function (_super) {
20
+ tslib.__extends(LinkError, _super);
21
+ function LinkError(message, link) {
22
+ var _this = _super.call(this, message) || this;
23
+ _this.link = link;
24
+ return _this;
25
+ }
26
+ return LinkError;
27
+ }(Error));
28
+ var ApolloLink = (function () {
29
+ function ApolloLink(request) {
30
+ if (request)
31
+ this.request = request;
32
+ }
33
+ ApolloLink.empty = function () {
34
+ return new ApolloLink(function () { return utilities.Observable.of(); });
35
+ };
36
+ ApolloLink.from = function (links) {
37
+ if (links.length === 0)
38
+ return ApolloLink.empty();
39
+ return links.map(toLink).reduce(function (x, y) { return x.concat(y); });
40
+ };
41
+ ApolloLink.split = function (test, left, right) {
42
+ var leftLink = toLink(left);
43
+ var rightLink = toLink(right || new ApolloLink(passthrough));
44
+ if (isTerminating(leftLink) && isTerminating(rightLink)) {
45
+ return new ApolloLink(function (operation) {
46
+ return test(operation)
47
+ ? leftLink.request(operation) || utilities.Observable.of()
48
+ : rightLink.request(operation) || utilities.Observable.of();
49
+ });
50
+ }
51
+ else {
52
+ return new ApolloLink(function (operation, forward) {
53
+ return test(operation)
54
+ ? leftLink.request(operation, forward) || utilities.Observable.of()
55
+ : rightLink.request(operation, forward) || utilities.Observable.of();
56
+ });
57
+ }
58
+ };
59
+ ApolloLink.execute = function (link, operation) {
60
+ return (link.request(utils.createOperation(operation.context, utils.transformOperation(utils.validateOperation(operation)))) || utilities.Observable.of());
61
+ };
62
+ ApolloLink.concat = function (first, second) {
63
+ var firstLink = toLink(first);
64
+ if (isTerminating(firstLink)) {
65
+ __DEV__ && globals.invariant.warn(new LinkError("You are calling concat on a terminating link, which will have no effect", firstLink));
66
+ return firstLink;
67
+ }
68
+ var nextLink = toLink(second);
69
+ if (isTerminating(nextLink)) {
70
+ return new ApolloLink(function (operation) {
71
+ return firstLink.request(operation, function (op) { return nextLink.request(op) || utilities.Observable.of(); }) || utilities.Observable.of();
72
+ });
73
+ }
74
+ else {
75
+ return new ApolloLink(function (operation, forward) {
76
+ return (firstLink.request(operation, function (op) {
77
+ return nextLink.request(op, forward) || utilities.Observable.of();
78
+ }) || utilities.Observable.of());
79
+ });
80
+ }
81
+ };
82
+ ApolloLink.prototype.split = function (test, left, right) {
83
+ return this.concat(ApolloLink.split(test, left, right || new ApolloLink(passthrough)));
84
+ };
85
+ ApolloLink.prototype.concat = function (next) {
86
+ return ApolloLink.concat(this, next);
87
+ };
88
+ ApolloLink.prototype.request = function (operation, forward) {
89
+ throw __DEV__ ? new globals.InvariantError('request is not implemented') : new globals.InvariantError(19);
90
+ };
91
+ ApolloLink.prototype.onError = function (error, observer) {
92
+ if (observer && observer.error) {
93
+ observer.error(error);
94
+ return false;
95
+ }
96
+ throw error;
97
+ };
98
+ ApolloLink.prototype.setOnError = function (fn) {
99
+ this.onError = fn;
100
+ return this;
101
+ };
102
+ return ApolloLink;
103
+ }());
104
+
105
+ var empty = ApolloLink.empty;
106
+
107
+ var from = ApolloLink.from;
108
+
109
+ var split = ApolloLink.split;
110
+
111
+ var concat = ApolloLink.concat;
112
+
113
+ var execute = ApolloLink.execute;
114
+
115
+ exports.ApolloLink = ApolloLink;
116
+ exports.concat = concat;
117
+ exports.empty = empty;
118
+ exports.execute = execute;
119
+ exports.from = from;
120
+ exports.split = split;
121
+ //# sourceMappingURL=core.cjs.map
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var tslib = require('tslib');
6
+ var utilities = require('../../utilities');
7
+ var core = require('../core');
8
+
9
+ function onError(errorHandler) {
10
+ return new core.ApolloLink(function (operation, forward) {
11
+ return new utilities.Observable(function (observer) {
12
+ var sub;
13
+ var retriedSub;
14
+ var retriedResult;
15
+ try {
16
+ sub = forward(operation).subscribe({
17
+ next: function (result) {
18
+ if (result.errors) {
19
+ retriedResult = errorHandler({
20
+ graphQLErrors: result.errors,
21
+ response: result,
22
+ operation: operation,
23
+ forward: forward,
24
+ });
25
+ if (retriedResult) {
26
+ retriedSub = retriedResult.subscribe({
27
+ next: observer.next.bind(observer),
28
+ error: observer.error.bind(observer),
29
+ complete: observer.complete.bind(observer),
30
+ });
31
+ return;
32
+ }
33
+ }
34
+ observer.next(result);
35
+ },
36
+ error: function (networkError) {
37
+ retriedResult = errorHandler({
38
+ operation: operation,
39
+ networkError: networkError,
40
+ graphQLErrors: networkError &&
41
+ networkError.result &&
42
+ networkError.result.errors,
43
+ forward: forward,
44
+ });
45
+ if (retriedResult) {
46
+ retriedSub = retriedResult.subscribe({
47
+ next: observer.next.bind(observer),
48
+ error: observer.error.bind(observer),
49
+ complete: observer.complete.bind(observer),
50
+ });
51
+ return;
52
+ }
53
+ observer.error(networkError);
54
+ },
55
+ complete: function () {
56
+ if (!retriedResult) {
57
+ observer.complete.bind(observer)();
58
+ }
59
+ },
60
+ });
61
+ }
62
+ catch (e) {
63
+ errorHandler({ networkError: e, operation: operation, forward: forward });
64
+ observer.error(e);
65
+ }
66
+ return function () {
67
+ if (sub)
68
+ sub.unsubscribe();
69
+ if (retriedSub)
70
+ sub.unsubscribe();
71
+ };
72
+ });
73
+ });
74
+ }
75
+ var ErrorLink = (function (_super) {
76
+ tslib.__extends(ErrorLink, _super);
77
+ function ErrorLink(errorHandler) {
78
+ var _this = _super.call(this) || this;
79
+ _this.link = onError(errorHandler);
80
+ return _this;
81
+ }
82
+ ErrorLink.prototype.request = function (operation, forward) {
83
+ return this.link.request(operation, forward);
84
+ };
85
+ return ErrorLink;
86
+ }(core.ApolloLink));
87
+
88
+ exports.ErrorLink = ErrorLink;
89
+ exports.onError = onError;
90
+ //# sourceMappingURL=error.cjs.map