@graphql-box/cache-manager 2.4.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/lib/browser/index.js +1 -1
  2. package/lib/browser/index.js.map +1 -1
  3. package/lib/browser/production.analysis.txt +64 -28
  4. package/lib/main/helpers/isFirstResponseChunk.js +11 -0
  5. package/lib/main/helpers/isFirstResponseChunk.js.map +1 -0
  6. package/lib/main/helpers/isLastResponseChunk.js +11 -0
  7. package/lib/main/helpers/isLastResponseChunk.js.map +1 -0
  8. package/lib/main/helpers/isNotLastResponseChunk.js +11 -0
  9. package/lib/main/helpers/isNotLastResponseChunk.js.map +1 -0
  10. package/lib/main/helpers/isNotResponseChunk.js +11 -0
  11. package/lib/main/helpers/isNotResponseChunk.js.map +1 -0
  12. package/lib/main/helpers/mergeResponseDataSets.js +53 -0
  13. package/lib/main/helpers/mergeResponseDataSets.js.map +1 -0
  14. package/lib/main/helpers/{normalizeResponseData.js → normalizePatchResponseData.js} +11 -6
  15. package/lib/main/helpers/normalizePatchResponseData.js.map +1 -0
  16. package/lib/main/main/index.js +145 -121
  17. package/lib/main/main/index.js.map +1 -1
  18. package/lib/module/helpers/isFirstResponseChunk.js +2 -0
  19. package/lib/module/helpers/isFirstResponseChunk.js.map +1 -0
  20. package/lib/module/helpers/isLastResponseChunk.js +2 -0
  21. package/lib/module/helpers/isLastResponseChunk.js.map +1 -0
  22. package/lib/module/helpers/isNotLastResponseChunk.js +2 -0
  23. package/lib/module/helpers/isNotLastResponseChunk.js.map +1 -0
  24. package/lib/module/helpers/isNotResponseChunk.js +2 -0
  25. package/lib/module/helpers/isNotResponseChunk.js.map +1 -0
  26. package/lib/module/helpers/mergeResponseDataSets.js +41 -0
  27. package/lib/module/helpers/mergeResponseDataSets.js.map +1 -0
  28. package/lib/module/helpers/normalizePatchResponseData.js +17 -0
  29. package/lib/module/helpers/normalizePatchResponseData.js.map +1 -0
  30. package/lib/module/main/index.js +143 -122
  31. package/lib/module/main/index.js.map +1 -1
  32. package/lib/types/defs/index.d.ts +3 -10
  33. package/lib/types/defs/index.d.ts.map +1 -1
  34. package/lib/types/helpers/isFirstResponseChunk.d.ts +5 -0
  35. package/lib/types/helpers/isFirstResponseChunk.d.ts.map +1 -0
  36. package/lib/types/helpers/isLastResponseChunk.d.ts +5 -0
  37. package/lib/types/helpers/isLastResponseChunk.d.ts.map +1 -0
  38. package/lib/types/helpers/isNotLastResponseChunk.d.ts +5 -0
  39. package/lib/types/helpers/isNotLastResponseChunk.d.ts.map +1 -0
  40. package/lib/types/helpers/isNotResponseChunk.d.ts +5 -0
  41. package/lib/types/helpers/isNotResponseChunk.d.ts.map +1 -0
  42. package/lib/types/helpers/mergeResponseDataSets.d.ts +4 -0
  43. package/lib/types/helpers/mergeResponseDataSets.d.ts.map +1 -0
  44. package/lib/types/helpers/normalizePatchResponseData.d.ts +5 -0
  45. package/lib/types/helpers/normalizePatchResponseData.d.ts.map +1 -0
  46. package/lib/types/main/index.d.ts +7 -5
  47. package/lib/types/main/index.d.ts.map +1 -1
  48. package/package.json +2 -2
  49. package/src/__snapshots__/index.test.ts.snap +12481 -11881
  50. package/src/defs/index.ts +14 -22
  51. package/src/helpers/isFirstResponseChunk.ts +5 -0
  52. package/src/helpers/isLastResponseChunk.ts +5 -0
  53. package/src/helpers/isNotLastResponseChunk.ts +5 -0
  54. package/src/helpers/isNotResponseChunk.ts +5 -0
  55. package/src/helpers/mergeResponseDataSets.ts +35 -0
  56. package/src/helpers/normalizePatchResponseData.ts +17 -0
  57. package/src/index.test.ts +191 -115
  58. package/src/main/index.ts +172 -134
  59. package/lib/main/helpers/normalizeResponseData.js.map +0 -1
  60. package/lib/module/helpers/normalizeResponseData.js +0 -12
  61. package/lib/module/helpers/normalizeResponseData.js.map +0 -1
  62. package/lib/types/helpers/normalizeResponseData.d.ts +0 -11
  63. package/lib/types/helpers/normalizeResponseData.d.ts.map +0 -1
  64. package/src/helpers/normalizeResponseData.ts +0 -10
@@ -52,32 +52,21 @@ var _filterOutPropsWithArgsOrDirectives = _interopRequireDefault(require("../hel
52
52
 
53
53
  var _filterQuery = _interopRequireDefault(require("../helpers/filterQuery"));
54
54
 
55
- var _normalizeResponseData = _interopRequireDefault(require("../helpers/normalizeResponseData"));
55
+ var _isLastResponseChunk = _interopRequireDefault(require("../helpers/isLastResponseChunk"));
56
56
 
57
- var _validTypeIDValue = require("../helpers/validTypeIDValue");
57
+ var _isNotLastResponseChunk = _interopRequireDefault(require("../helpers/isNotLastResponseChunk"));
58
58
 
59
- var _dec, _dec2, _dec3, _class;
59
+ var _isNotResponseChunk = _interopRequireDefault(require("../helpers/isNotResponseChunk"));
60
60
 
61
- let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCacheEntry)(), _dec3 = (0, _debug.logPartialCompiled)(), (_class = class CacheManager {
62
- static async init(options) {
63
- const errors = [];
61
+ var _mergeResponseDataSets = _interopRequireDefault(require("../helpers/mergeResponseDataSets"));
64
62
 
65
- if (!options.cache) {
66
- errors.push(new TypeError("@graphql-box/cache-manager expected options.cache."));
67
- }
63
+ var _normalizePatchResponseData = _interopRequireDefault(require("../helpers/normalizePatchResponseData"));
68
64
 
69
- if (!!options.typeCacheDirectives && !(0, _isPlainObject2.default)(options.typeCacheDirectives)) {
70
- const message = "@graphql-box/cache-manager expected options.typeCacheDirectives to be a plain object.";
71
- errors.push(new TypeError(message));
72
- }
73
-
74
- if (errors.length) {
75
- return Promise.reject(errors);
76
- }
65
+ var _validTypeIDValue = require("../helpers/validTypeIDValue");
77
66
 
78
- return new CacheManager(options);
79
- }
67
+ var _dec, _dec2, _dec3, _class;
80
68
 
69
+ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCacheEntry)(), _dec3 = (0, _debug.logPartialCompiled)(), (_class = class CacheManager {
81
70
  static _countFieldPathChecklist(fieldPathChecklist) {
82
71
  const fieldCount = {
83
72
  missing: 0,
@@ -225,20 +214,29 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
225
214
  (0, _defineProperty2.default)(this, "_cascadeCacheControl", void 0);
226
215
  (0, _defineProperty2.default)(this, "_fallbackOperationCacheability", void 0);
227
216
  (0, _defineProperty2.default)(this, "_partialQueryResponses", new Map());
217
+ (0, _defineProperty2.default)(this, "_responseChunksAwaitingCaching", new Map());
228
218
  (0, _defineProperty2.default)(this, "_typeCacheDirectives", void 0);
229
219
  (0, _defineProperty2.default)(this, "_typeIDKey", void 0);
230
- const {
231
- cache,
232
- cascadeCacheControl,
233
- fallbackOperationCacheability,
234
- typeCacheDirectives,
235
- typeIDKey
236
- } = options;
237
- this._cache = cache;
238
- this._cascadeCacheControl = cascadeCacheControl || false;
239
- this._fallbackOperationCacheability = fallbackOperationCacheability || _consts.NO_CACHE;
240
- this._typeCacheDirectives = typeCacheDirectives || {};
241
- this._typeIDKey = typeIDKey;
220
+ const errors = [];
221
+
222
+ if (!options.cache) {
223
+ errors.push(new TypeError("@graphql-box/cache-manager expected options.cache."));
224
+ }
225
+
226
+ if (!!options.typeCacheDirectives && !(0, _isPlainObject2.default)(options.typeCacheDirectives)) {
227
+ const message = "@graphql-box/cache-manager expected options.typeCacheDirectives to be a plain object.";
228
+ errors.push(new TypeError(message));
229
+ }
230
+
231
+ if (errors.length) {
232
+ throw errors;
233
+ }
234
+
235
+ this._cache = options.cache;
236
+ this._cascadeCacheControl = options.cascadeCacheControl || false;
237
+ this._fallbackOperationCacheability = options.fallbackOperationCacheability || _consts.NO_CACHE;
238
+ this._typeCacheDirectives = options.typeCacheDirectives || {};
239
+ this._typeIDKey = options.typeIDKey;
242
240
  }
243
241
 
244
242
  get cache() {
@@ -312,6 +310,22 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
312
310
  };
313
311
  }
314
312
 
313
+ async cacheQuery(requestData, updatedRequestData, rawResponseData, options, context) {
314
+ const cacheManagerContext = { ...context,
315
+ fragmentDefinitions: (0, _helpers.getFragmentDefinitions)(updatedRequestData.ast),
316
+ typeIDKey: this._typeIDKey
317
+ };
318
+ return this._cacheResponse(requestData, updatedRequestData, rawResponseData, options, cacheManagerContext);
319
+ }
320
+
321
+ async cacheResponse(requestData, rawResponseData, options, context) {
322
+ const cacheManagerContext = { ...context,
323
+ fragmentDefinitions: (0, _helpers.getFragmentDefinitions)(requestData.ast),
324
+ typeIDKey: this._typeIDKey
325
+ };
326
+ return this._cacheResponse(requestData, undefined, rawResponseData, options, cacheManagerContext);
327
+ }
328
+
315
329
  async checkCacheEntry(cacheType, hash, options, context) {
316
330
  return this._checkCacheEntry(cacheType, hash, options, context);
317
331
  }
@@ -337,66 +351,10 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
337
351
  this._partialQueryResponses.delete(hash);
338
352
  }
339
353
 
340
- async resolveQuery(requestData, updatedRequestData, rawResponseData, options, context) {
341
- const cacheManagerContext = { ...context,
342
- fragmentDefinitions: (0, _helpers.getFragmentDefinitions)(updatedRequestData.ast),
343
- typeIDKey: this._typeIDKey
344
- };
345
- const dataCaching = [];
346
- const {
347
- cacheMetadata,
348
- data,
349
- hasNext,
350
- paths
351
- } = await this._resolveRequest(updatedRequestData, rawResponseData, options, cacheManagerContext);
352
- let partialQueryResponse;
353
-
354
- if (cacheManagerContext.queryFiltered) {
355
- if (!(rawResponseData.hasNext || rawResponseData.paths)) {
356
- dataCaching.push(this._setQueryResponseCacheEntry(updatedRequestData.hash, {
357
- cacheMetadata,
358
- data
359
- }, options, cacheManagerContext));
360
- }
361
-
362
- if (!rawResponseData.paths) {
363
- partialQueryResponse = this._getPartialQueryResponse(requestData.hash);
364
- }
365
- }
366
-
367
- const responseCacheMetadata = CacheManager._mergeResponseCacheMetadata(cacheMetadata, partialQueryResponse);
368
-
369
- const responseData = this._mergeResponseData(data, partialQueryResponse);
370
-
371
- if (!(rawResponseData.hasNext || rawResponseData.paths)) {
372
- dataCaching.push(this._setQueryResponseCacheEntry(requestData.hash, {
373
- cacheMetadata: responseCacheMetadata,
374
- data: responseData
375
- }, options, cacheManagerContext));
376
- }
377
-
378
- if (options.awaitDataCaching) {
379
- await Promise.all(dataCaching);
380
- }
381
-
382
- return {
383
- cacheMetadata: responseCacheMetadata,
384
- data: responseData,
385
- hasNext,
386
- paths
387
- };
388
- }
389
-
390
- async resolveRequest(requestData, rawResponseData, options, context) {
391
- const cacheManagerContext = { ...context,
392
- fragmentDefinitions: (0, _helpers.getFragmentDefinitions)(requestData.ast),
393
- typeIDKey: this._typeIDKey
394
- };
395
- return this._resolveRequest(requestData, rawResponseData, options, cacheManagerContext);
396
- }
397
-
398
354
  async _analyzeFieldNode(fieldNode, cachedAncestorFieldData, cachedResponseData, options, context) {
399
- if ((0, _helpers.hasChildFields)(fieldNode)) {
355
+ if ((0, _helpers.hasChildFields)(fieldNode, {
356
+ fragmentDefinitions: context.fragmentDefinitions
357
+ })) {
400
358
  await this._analyzeParentFieldNode(fieldNode, cachedAncestorFieldData, cachedResponseData, options, context);
401
359
  } else {
402
360
  await this._analyzeLeafFieldNode(fieldNode, cachedAncestorFieldData, cachedResponseData, options, context);
@@ -531,6 +489,81 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
531
489
  return cacheMetadata;
532
490
  }
533
491
 
492
+ async _cacheResponse(requestData, updatedRequestData, rawResponseData, options, context) {
493
+ const normalizedResponseData = (0, _normalizePatchResponseData.default)(rawResponseData, context);
494
+ let responseDataForCaching = normalizedResponseData;
495
+
496
+ if ((0, _isNotLastResponseChunk.default)(rawResponseData, context)) {
497
+ this._setResponseChunksAwaitingCaching(normalizedResponseData, context);
498
+
499
+ responseDataForCaching = undefined;
500
+ }
501
+
502
+ if ((0, _isLastResponseChunk.default)(rawResponseData, context)) {
503
+ responseDataForCaching = this._retrieveResponseDataForCaching(normalizedResponseData, context);
504
+ }
505
+
506
+ const dataCaching = [];
507
+
508
+ if (responseDataForCaching) {
509
+ const {
510
+ data
511
+ } = responseDataForCaching;
512
+
513
+ const cacheMetadata = this._buildCacheMetadata(requestData, responseDataForCaching, options, context);
514
+
515
+ dataCaching.push(this._setEntityAndRequestFieldPathCacheEntries(requestData, {
516
+ cacheMetadata,
517
+ entityData: (0, _cloneDeep2.default)(data),
518
+ requestFieldPathData: (0, _cloneDeep2.default)(data)
519
+ }, options, context));
520
+ let queryCacheMetadata;
521
+ let queryData;
522
+
523
+ if (context.operation === _core.QUERY) {
524
+ let partialQueryResponse;
525
+
526
+ if (context.queryFiltered && updatedRequestData) {
527
+ dataCaching.push(this._setQueryResponseCacheEntry(updatedRequestData.hash, {
528
+ cacheMetadata,
529
+ data
530
+ }, options, context));
531
+ partialQueryResponse = this._getPartialQueryResponse(requestData.hash);
532
+ }
533
+
534
+ queryCacheMetadata = CacheManager._mergeResponseCacheMetadata(cacheMetadata, partialQueryResponse);
535
+ queryData = this._mergeResponseData(data, partialQueryResponse);
536
+ dataCaching.push(this._setQueryResponseCacheEntry(requestData.hash, {
537
+ cacheMetadata: queryCacheMetadata,
538
+ data: queryData
539
+ }, options, context));
540
+ }
541
+
542
+ if (options.awaitDataCaching) {
543
+ await Promise.all(dataCaching);
544
+ }
545
+
546
+ if ((0, _isNotResponseChunk.default)(normalizedResponseData, context) && queryCacheMetadata && queryData) {
547
+ return {
548
+ cacheMetadata: queryCacheMetadata,
549
+ data: queryData
550
+ };
551
+ }
552
+ }
553
+
554
+ const {
555
+ data,
556
+ hasNext,
557
+ paths
558
+ } = normalizedResponseData;
559
+ return {
560
+ cacheMetadata: this._buildCacheMetadata(requestData, normalizedResponseData, options, context),
561
+ data,
562
+ hasNext,
563
+ paths
564
+ };
565
+ }
566
+
534
567
  async _checkCacheEntry(cacheType, hash, options, context) {
535
568
  try {
536
569
  const cacheability = await this._hasCacheEntry(cacheType, hash);
@@ -674,35 +707,6 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
674
707
  }, options, context);
675
708
  }
676
709
 
677
- async _resolveRequest(requestData, rawResponseData, options, context) {
678
- const normalizedResponseData = rawResponseData.paths ? (0, _normalizeResponseData.default)(rawResponseData) : rawResponseData;
679
- const dataCaching = [];
680
-
681
- const cacheMetadata = this._buildCacheMetadata(requestData, normalizedResponseData, options, context);
682
-
683
- const {
684
- data,
685
- hasNext,
686
- paths
687
- } = normalizedResponseData;
688
- dataCaching.push(this._setEntityAndRequestFieldPathCacheEntries(requestData, {
689
- cacheMetadata,
690
- entityData: (0, _cloneDeep2.default)(data),
691
- requestFieldPathData: (0, _cloneDeep2.default)(data)
692
- }, options, context));
693
-
694
- if (options.awaitDataCaching) {
695
- await Promise.all(dataCaching);
696
- }
697
-
698
- return {
699
- cacheMetadata,
700
- data,
701
- hasNext,
702
- paths
703
- };
704
- }
705
-
706
710
  async _retrieveCachedEntityData(validTypeIDValue, {
707
711
  possibleTypes,
708
712
  typeName
@@ -821,6 +825,14 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
821
825
  return cachedResponseData;
822
826
  }
823
827
 
828
+ _retrieveResponseDataForCaching(normalizedResponseData, context) {
829
+ const responseChunks = this._responseChunksAwaitingCaching.get(context.boxID);
830
+
831
+ this._responseChunksAwaitingCaching.delete(context.boxID);
832
+
833
+ return (0, _mergeResponseDataSets.default)([...responseChunks, normalizedResponseData]);
834
+ }
835
+
824
836
  async _setCacheEntry(cacheType, hash, value, cachemapOptions, _options, _context) {
825
837
  try {
826
838
  await this._cache.set(`${cacheType}::${hash}`, (0, _cloneDeep2.default)(value), cachemapOptions);
@@ -1032,7 +1044,9 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
1032
1044
  tag: options.tag
1033
1045
  }, options, context);
1034
1046
 
1035
- if ((0, _helpers.hasChildFields)(field)) {
1047
+ if ((0, _helpers.hasChildFields)(field, {
1048
+ fragmentDefinitions: context.fragmentDefinitions
1049
+ })) {
1036
1050
  if (isEntity) {
1037
1051
  (0, _set2.default)(data, responseDataPath, {
1038
1052
  __cacheKey: `${_core.REQUEST_FIELD_PATHS}::${hashedRequestFieldCacheKey}`
@@ -1044,6 +1058,16 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
1044
1058
  }
1045
1059
  }
1046
1060
 
1061
+ _setResponseChunksAwaitingCaching(normalizedResponseData, context) {
1062
+ const responseChunks = this._responseChunksAwaitingCaching.get(context.boxID);
1063
+
1064
+ if (responseChunks) {
1065
+ this._responseChunksAwaitingCaching.set(context.boxID, [...responseChunks, normalizedResponseData]);
1066
+ } else {
1067
+ this._responseChunksAwaitingCaching.set(context.boxID, [normalizedResponseData]);
1068
+ }
1069
+ }
1070
+
1047
1071
  }, ((0, _applyDecoratedDescriptor2.default)(_class.prototype, "_getCacheEntry", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "_getCacheEntry"), _class.prototype), (0, _applyDecoratedDescriptor2.default)(_class.prototype, "_setCacheEntry", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "_setCacheEntry"), _class.prototype), (0, _applyDecoratedDescriptor2.default)(_class.prototype, "_setPartialQueryResponse", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "_setPartialQueryResponse"), _class.prototype)), _class));
1048
1072
  exports.CacheManager = CacheManager;
1049
1073
 
@@ -1052,7 +1076,7 @@ function init(userOptions) {
1052
1076
  throw new TypeError("@graphql-box/cache-manager expected userOptions to be a plain object.");
1053
1077
  }
1054
1078
 
1055
- return clientOptions => CacheManager.init({ ...clientOptions,
1079
+ return clientOptions => new CacheManager({ ...clientOptions,
1056
1080
  ...userOptions
1057
1081
  });
1058
1082
  }