@graphql-box/cache-manager 2.0.4 → 2.1.3

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.
@@ -0,0 +1,32 @@
1
+ import { DehydratedCacheMetadata } from "@graphql-box/core";
2
+ import Cacheability from "cacheability";
3
+ import { isEmpty } from "lodash";
4
+ import { HEADER_CACHE_CONTROL } from "../consts";
5
+
6
+ export type Params = {
7
+ _cacheMetadata?: DehydratedCacheMetadata;
8
+ fallback: string;
9
+ headers?: Headers;
10
+ };
11
+
12
+ export default ({ _cacheMetadata, fallback, headers }: Params): Cacheability => {
13
+ if (_cacheMetadata && !isEmpty(_cacheMetadata)) {
14
+ const [first, ...rest] = Object.values(_cacheMetadata);
15
+
16
+ return new Cacheability({
17
+ metadata: rest.reduce((acc, metadata) => {
18
+ if (metadata.ttl < acc.ttl) {
19
+ return metadata;
20
+ }
21
+
22
+ return acc;
23
+ }, first),
24
+ });
25
+ }
26
+
27
+ if (headers && headers.has(HEADER_CACHE_CONTROL)) {
28
+ return new Cacheability({ headers });
29
+ }
30
+
31
+ return new Cacheability({ cacheControl: fallback });
32
+ };
package/src/index.test.ts CHANGED
@@ -42,6 +42,7 @@ describe("@graphql-box/cache-manager >>", () => {
42
42
  cache: new Cachemap({
43
43
  name: "cachemap",
44
44
  store: map(),
45
+ type: "someType",
45
46
  }),
46
47
  cascadeCacheControl: true,
47
48
  typeIDKey: DEFAULT_TYPE_ID_KEY,
@@ -72,6 +73,7 @@ describe("@graphql-box/cache-manager >>", () => {
72
73
  cache: new Cachemap({
73
74
  name: "cachemap",
74
75
  store: map(),
76
+ type: "someType",
75
77
  }),
76
78
  typeCacheDirectives: {
77
79
  AddStarPayload: "no-cache, no-store",
@@ -110,6 +112,7 @@ describe("@graphql-box/cache-manager >>", () => {
110
112
  cache: new Cachemap({
111
113
  name: "cachemap",
112
114
  store: map(),
115
+ type: "someType",
113
116
  }),
114
117
  cascadeCacheControl: true,
115
118
  typeIDKey: DEFAULT_TYPE_ID_KEY,
@@ -140,6 +143,7 @@ describe("@graphql-box/cache-manager >>", () => {
140
143
  cache: new Cachemap({
141
144
  name: "cachemap",
142
145
  store: map(),
146
+ type: "someType",
143
147
  }),
144
148
  typeCacheDirectives: {
145
149
  Email: "public, max-age=5",
@@ -181,6 +185,7 @@ describe("@graphql-box/cache-manager >>", () => {
181
185
  cache: new Cachemap({
182
186
  name: "cachemap",
183
187
  store: map(),
188
+ type: "someType",
184
189
  }),
185
190
  cascadeCacheControl: true,
186
191
  typeIDKey: DEFAULT_TYPE_ID_KEY,
@@ -212,6 +217,7 @@ describe("@graphql-box/cache-manager >>", () => {
212
217
  cache: new Cachemap({
213
218
  name: "cachemap",
214
219
  store: map(),
220
+ type: "someType",
215
221
  }),
216
222
  typeCacheDirectives: {
217
223
  Organization: "public, max-age=1",
@@ -247,6 +253,7 @@ describe("@graphql-box/cache-manager >>", () => {
247
253
  cache: new Cachemap({
248
254
  name: "cachemap",
249
255
  store: map(),
256
+ type: "someType",
250
257
  }),
251
258
  cascadeCacheControl: true,
252
259
  typeIDKey: DEFAULT_TYPE_ID_KEY,
@@ -278,6 +285,7 @@ describe("@graphql-box/cache-manager >>", () => {
278
285
  cache: new Cachemap({
279
286
  name: "cachemap",
280
287
  store: map(),
288
+ type: "someType",
281
289
  }),
282
290
  typeCacheDirectives: {
283
291
  Organization: "public, max-age=3",
@@ -316,6 +324,7 @@ describe("@graphql-box/cache-manager >>", () => {
316
324
  cache: new Cachemap({
317
325
  name: "cachemap",
318
326
  store: map(),
327
+ type: "someType",
319
328
  }),
320
329
  cascadeCacheControl: true,
321
330
  typeIDKey: DEFAULT_TYPE_ID_KEY,
@@ -347,6 +356,7 @@ describe("@graphql-box/cache-manager >>", () => {
347
356
  cache: new Cachemap({
348
357
  name: "cachemap",
349
358
  store: map(),
359
+ type: "someType",
350
360
  }),
351
361
  typeCacheDirectives: {
352
362
  SearchResultItem: "public, max-age=1",
@@ -388,6 +398,7 @@ describe("@graphql-box/cache-manager >>", () => {
388
398
  cache: new Cachemap({
389
399
  name: "cachemap",
390
400
  store: map(),
401
+ type: "someType",
391
402
  }),
392
403
  cascadeCacheControl: true,
393
404
  typeIDKey: DEFAULT_TYPE_ID_KEY,
@@ -438,6 +449,7 @@ describe("@graphql-box/cache-manager >>", () => {
438
449
  cache: new Cachemap({
439
450
  name: "cachemap",
440
451
  store: map(),
452
+ type: "someType",
441
453
  }),
442
454
  typeCacheDirectives: {
443
455
  Organization: "public, max-age=1",
@@ -492,6 +504,7 @@ describe("@graphql-box/cache-manager >>", () => {
492
504
  cache: new Cachemap({
493
505
  name: "cachemap",
494
506
  store: map(),
507
+ type: "someType",
495
508
  }),
496
509
  cascadeCacheControl: true,
497
510
  typeIDKey: DEFAULT_TYPE_ID_KEY,
@@ -542,6 +555,7 @@ describe("@graphql-box/cache-manager >>", () => {
542
555
  cache: new Cachemap({
543
556
  name: "cachemap",
544
557
  store: map(),
558
+ type: "someType",
545
559
  }),
546
560
  typeCacheDirectives: {
547
561
  Organization: "public, max-age=3",
@@ -599,6 +613,7 @@ describe("@graphql-box/cache-manager >>", () => {
599
613
  cache: new Cachemap({
600
614
  name: "cachemap",
601
615
  store: map(),
616
+ type: "someType",
602
617
  }),
603
618
  cascadeCacheControl: true,
604
619
  typeIDKey: DEFAULT_TYPE_ID_KEY,
@@ -649,6 +664,7 @@ describe("@graphql-box/cache-manager >>", () => {
649
664
  cache: new Cachemap({
650
665
  name: "cachemap",
651
666
  store: map(),
667
+ type: "someType",
652
668
  }),
653
669
  typeCacheDirectives: {
654
670
  SearchResultItem: "public, max-age=1",
@@ -706,6 +722,7 @@ describe("@graphql-box/cache-manager >>", () => {
706
722
  cache: new Cachemap({
707
723
  name: "cachemap",
708
724
  store: map(),
725
+ type: "someType",
709
726
  }),
710
727
  typeIDKey: DEFAULT_TYPE_ID_KEY,
711
728
  });
@@ -733,6 +750,7 @@ describe("@graphql-box/cache-manager >>", () => {
733
750
  cache: new Cachemap({
734
751
  name: "cachemap",
735
752
  store: map(),
753
+ type: "someType",
736
754
  }),
737
755
  typeIDKey: DEFAULT_TYPE_ID_KEY,
738
756
  });
@@ -760,6 +778,7 @@ describe("@graphql-box/cache-manager >>", () => {
760
778
  cache: new Cachemap({
761
779
  name: "cachemap",
762
780
  store: map(),
781
+ type: "someType",
763
782
  }),
764
783
  typeIDKey: DEFAULT_TYPE_ID_KEY,
765
784
  });
@@ -792,6 +811,7 @@ describe("@graphql-box/cache-manager >>", () => {
792
811
  cache: new Cachemap({
793
812
  name: "cachemap",
794
813
  store: map(),
814
+ type: "someType",
795
815
  }),
796
816
  typeCacheDirectives: {
797
817
  Organization: "public, max-age=1",
@@ -838,6 +858,7 @@ describe("@graphql-box/cache-manager >>", () => {
838
858
  cache: new Cachemap({
839
859
  name: "cachemap",
840
860
  store: map(),
861
+ type: "someType",
841
862
  }),
842
863
  typeCacheDirectives: {
843
864
  Organization: "public, max-age=3",
@@ -887,6 +908,7 @@ describe("@graphql-box/cache-manager >>", () => {
887
908
  cache: new Cachemap({
888
909
  name: "cachemap",
889
910
  store: map(),
911
+ type: "someType",
890
912
  }),
891
913
  typeCacheDirectives: {
892
914
  SearchResultItem: "public, max-age=1",
@@ -936,6 +958,7 @@ describe("@graphql-box/cache-manager >>", () => {
936
958
  cache: new Cachemap({
937
959
  name: "cachemap",
938
960
  store: map(),
961
+ type: "someType",
939
962
  }),
940
963
  typeCacheDirectives: {
941
964
  Organization: "public, max-age=1",
@@ -988,6 +1011,7 @@ describe("@graphql-box/cache-manager >>", () => {
988
1011
  cache: new Cachemap({
989
1012
  name: "cachemap",
990
1013
  store: map(),
1014
+ type: "someType",
991
1015
  }),
992
1016
  typeCacheDirectives: {
993
1017
  Organization: "public, max-age=3",
@@ -1043,6 +1067,7 @@ describe("@graphql-box/cache-manager >>", () => {
1043
1067
  cache: new Cachemap({
1044
1068
  name: "cachemap",
1045
1069
  store: map(),
1070
+ type: "someType",
1046
1071
  }),
1047
1072
  typeCacheDirectives: {
1048
1073
  SearchResultItem: "public, max-age=1",
package/src/main/index.ts CHANGED
@@ -36,7 +36,7 @@ import {
36
36
  import Cacheability from "cacheability";
37
37
  import { FieldNode, print } from "graphql";
38
38
  import { cloneDeep, get, isArray, isNumber, isObjectLike, isPlainObject, isUndefined, set, unset } from "lodash";
39
- import { CACHE_CONTROL, HEADER_CACHE_CONTROL, HEADER_NO_CACHE, METADATA, NO_CACHE } from "../consts";
39
+ import { CACHE_CONTROL, HEADER_NO_CACHE, METADATA, NO_CACHE } from "../consts";
40
40
  import { logCacheEntry, logCacheQuery, logPartialCompiled } from "../debug";
41
41
  import {
42
42
  AnalyzeQueryResult,
@@ -66,6 +66,7 @@ import {
66
66
  TypeNames,
67
67
  UserOptions,
68
68
  } from "../defs";
69
+ import deriveOpCacheability from "../helpers/deriveOpCacheability";
69
70
 
70
71
  export class CacheManager implements CacheManagerDef {
71
72
  public static async init(options: InitOptions): Promise<CacheManager> {
@@ -556,10 +557,19 @@ export class CacheManager implements CacheManagerDef {
556
557
  { operation }: RequestContext,
557
558
  ): CacheMetadata {
558
559
  const cacheMetadata = new Map();
559
- const cacheControl = (headers && headers.get(HEADER_CACHE_CONTROL)) || this._fallbackOperationCacheability;
560
- const cacheability = new Cacheability({ cacheControl });
560
+
561
+ const cacheability = deriveOpCacheability({
562
+ _cacheMetadata,
563
+ fallback: this._fallbackOperationCacheability,
564
+ headers,
565
+ });
566
+
561
567
  cacheMetadata.set(operation, cacheability);
562
- if (_cacheMetadata) rehydrateCacheMetadata(_cacheMetadata, cacheMetadata);
568
+
569
+ if (_cacheMetadata) {
570
+ rehydrateCacheMetadata(_cacheMetadata, cacheMetadata);
571
+ }
572
+
563
573
  return cacheMetadata;
564
574
  }
565
575