@graphql-box/cache-manager 2.1.1 → 2.2.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 (116) 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 +139 -17
  4. package/lib/main/debug/log-cache-entry/index.js.map +1 -1
  5. package/lib/main/debug/log-partial-compiled/index.js.map +1 -1
  6. package/lib/main/helpers/buildKeysAndPaths.js +73 -0
  7. package/lib/main/helpers/buildKeysAndPaths.js.map +1 -0
  8. package/lib/main/helpers/checkFieldPathChecklist.js +40 -0
  9. package/lib/main/helpers/checkFieldPathChecklist.js.map +1 -0
  10. package/lib/main/helpers/createFragmentSpreadChecklist.js +28 -0
  11. package/lib/main/helpers/createFragmentSpreadChecklist.js.map +1 -0
  12. package/lib/main/helpers/deriveOpCacheability.js +46 -0
  13. package/lib/main/helpers/deriveOpCacheability.js.map +1 -0
  14. package/lib/main/helpers/filterField.js +97 -0
  15. package/lib/main/helpers/filterField.js.map +1 -0
  16. package/lib/main/helpers/filterFragmentDefinitions.js +50 -0
  17. package/lib/main/helpers/filterFragmentDefinitions.js.map +1 -0
  18. package/lib/main/helpers/filterFragmentSpreads.js +37 -0
  19. package/lib/main/helpers/filterFragmentSpreads.js.map +1 -0
  20. package/lib/main/helpers/filterIDsAndTypeNames.js +47 -0
  21. package/lib/main/helpers/filterIDsAndTypeNames.js.map +1 -0
  22. package/lib/main/helpers/filterInlineFragments.js +42 -0
  23. package/lib/main/helpers/filterInlineFragments.js.map +1 -0
  24. package/lib/main/helpers/filterOutPropsWithArgsOrDirectives.js +39 -0
  25. package/lib/main/helpers/filterOutPropsWithArgsOrDirectives.js.map +1 -0
  26. package/lib/main/helpers/filterQuery.js +59 -0
  27. package/lib/main/helpers/filterQuery.js.map +1 -0
  28. package/lib/main/helpers/normalizeResponseData.js +23 -0
  29. package/lib/main/helpers/normalizeResponseData.js.map +1 -0
  30. package/lib/main/helpers/validTypeIDValue.js +20 -0
  31. package/lib/main/helpers/validTypeIDValue.js.map +1 -0
  32. package/lib/main/main/index.js +477 -476
  33. package/lib/main/main/index.js.map +1 -1
  34. package/lib/module/debug/log-cache-entry/index.js.map +1 -1
  35. package/lib/module/debug/log-partial-compiled/index.js.map +1 -1
  36. package/lib/module/helpers/buildKeysAndPaths.js +54 -0
  37. package/lib/module/helpers/buildKeysAndPaths.js.map +1 -0
  38. package/lib/module/helpers/checkFieldPathChecklist.js +31 -0
  39. package/lib/module/helpers/checkFieldPathChecklist.js.map +1 -0
  40. package/lib/module/helpers/createFragmentSpreadChecklist.js +15 -0
  41. package/lib/module/helpers/createFragmentSpreadChecklist.js.map +1 -0
  42. package/lib/module/helpers/deriveOpCacheability.js +32 -0
  43. package/lib/module/helpers/deriveOpCacheability.js.map +1 -0
  44. package/lib/module/helpers/filterField.js +81 -0
  45. package/lib/module/helpers/filterField.js.map +1 -0
  46. package/lib/module/helpers/filterFragmentDefinitions.js +39 -0
  47. package/lib/module/helpers/filterFragmentDefinitions.js.map +1 -0
  48. package/lib/module/helpers/filterFragmentSpreads.js +23 -0
  49. package/lib/module/helpers/filterFragmentSpreads.js.map +1 -0
  50. package/lib/module/helpers/filterIDsAndTypeNames.js +36 -0
  51. package/lib/module/helpers/filterIDsAndTypeNames.js.map +1 -0
  52. package/lib/module/helpers/filterInlineFragments.js +32 -0
  53. package/lib/module/helpers/filterInlineFragments.js.map +1 -0
  54. package/lib/module/helpers/filterOutPropsWithArgsOrDirectives.js +25 -0
  55. package/lib/module/helpers/filterOutPropsWithArgsOrDirectives.js.map +1 -0
  56. package/lib/module/helpers/filterQuery.js +43 -0
  57. package/lib/module/helpers/filterQuery.js.map +1 -0
  58. package/lib/module/helpers/normalizeResponseData.js +11 -0
  59. package/lib/module/helpers/normalizeResponseData.js.map +1 -0
  60. package/lib/module/helpers/validTypeIDValue.js +8 -0
  61. package/lib/module/helpers/validTypeIDValue.js.map +1 -0
  62. package/lib/module/main/index.js +474 -475
  63. package/lib/module/main/index.js.map +1 -1
  64. package/lib/types/debug/log-cache-entry/index.d.ts.map +1 -1
  65. package/lib/types/debug/log-cache-query/index.d.ts.map +1 -1
  66. package/lib/types/debug/log-partial-compiled/index.d.ts.map +1 -1
  67. package/lib/types/defs/index.d.ts +19 -9
  68. package/lib/types/defs/index.d.ts.map +1 -1
  69. package/lib/types/helpers/buildKeysAndPaths.d.ts +10 -0
  70. package/lib/types/helpers/buildKeysAndPaths.d.ts.map +1 -0
  71. package/lib/types/helpers/checkFieldPathChecklist.d.ts +4 -0
  72. package/lib/types/helpers/checkFieldPathChecklist.d.ts.map +1 -0
  73. package/lib/types/helpers/createFragmentSpreadChecklist.d.ts +11 -0
  74. package/lib/types/helpers/createFragmentSpreadChecklist.d.ts.map +1 -0
  75. package/lib/types/helpers/deriveOpCacheability.d.ts +10 -0
  76. package/lib/types/helpers/deriveOpCacheability.d.ts.map +1 -0
  77. package/lib/types/helpers/filterField.d.ts +6 -0
  78. package/lib/types/helpers/filterField.d.ts.map +1 -0
  79. package/lib/types/helpers/filterFragmentDefinitions.d.ts +10 -0
  80. package/lib/types/helpers/filterFragmentDefinitions.d.ts.map +1 -0
  81. package/lib/types/helpers/filterFragmentSpreads.d.ts +6 -0
  82. package/lib/types/helpers/filterFragmentSpreads.d.ts.map +1 -0
  83. package/lib/types/helpers/filterIDsAndTypeNames.d.ts +5 -0
  84. package/lib/types/helpers/filterIDsAndTypeNames.d.ts.map +1 -0
  85. package/lib/types/helpers/filterInlineFragments.d.ts +5 -0
  86. package/lib/types/helpers/filterInlineFragments.d.ts.map +1 -0
  87. package/lib/types/helpers/filterOutPropsWithArgsOrDirectives.d.ts +6 -0
  88. package/lib/types/helpers/filterOutPropsWithArgsOrDirectives.d.ts.map +1 -0
  89. package/lib/types/helpers/filterQuery.d.ts +5 -0
  90. package/lib/types/helpers/filterQuery.d.ts.map +1 -0
  91. package/lib/types/helpers/normalizeResponseData.d.ts +10 -0
  92. package/lib/types/helpers/normalizeResponseData.d.ts.map +1 -0
  93. package/lib/types/helpers/validTypeIDValue.d.ts +3 -0
  94. package/lib/types/helpers/validTypeIDValue.d.ts.map +1 -0
  95. package/lib/types/main/index.d.ts +13 -20
  96. package/lib/types/main/index.d.ts.map +1 -1
  97. package/package.json +2 -2
  98. package/src/__snapshots__/index.test.ts.snap +17449 -7185
  99. package/src/debug/log-cache-entry/index.ts +1 -1
  100. package/src/debug/log-partial-compiled/index.ts +1 -1
  101. package/src/defs/index.ts +18 -10
  102. package/src/helpers/buildKeysAndPaths.ts +71 -0
  103. package/src/helpers/checkFieldPathChecklist.ts +21 -0
  104. package/src/helpers/createFragmentSpreadChecklist.ts +17 -0
  105. package/src/helpers/deriveOpCacheability.ts +32 -0
  106. package/src/helpers/filterField.ts +73 -0
  107. package/src/helpers/filterFragmentDefinitions.ts +40 -0
  108. package/src/helpers/filterFragmentSpreads.ts +28 -0
  109. package/src/helpers/filterIDsAndTypeNames.ts +31 -0
  110. package/src/helpers/filterInlineFragments.ts +29 -0
  111. package/src/helpers/filterOutPropsWithArgsOrDirectives.ts +30 -0
  112. package/src/helpers/filterQuery.ts +38 -0
  113. package/src/helpers/normalizeResponseData.ts +9 -0
  114. package/src/helpers/validTypeIDValue.ts +11 -0
  115. package/src/index.test.ts +179 -3
  116. package/src/main/index.ts +516 -499
@@ -24,19 +24,19 @@ var _isPlainObject2 = _interopRequireDefault(require("lodash/isPlainObject"));
24
24
 
25
25
  var _isObjectLike2 = _interopRequireDefault(require("lodash/isObjectLike"));
26
26
 
27
- var _isNumber2 = _interopRequireDefault(require("lodash/isNumber"));
28
-
29
27
  var _isArray2 = _interopRequireDefault(require("lodash/isArray"));
30
28
 
31
29
  var _get2 = _interopRequireDefault(require("lodash/get"));
32
30
 
33
31
  var _cloneDeep2 = _interopRequireDefault(require("lodash/cloneDeep"));
34
32
 
33
+ var _assign2 = _interopRequireDefault(require("lodash/assign"));
34
+
35
35
  var _core = require("@graphql-box/core");
36
36
 
37
37
  var _helpers = require("@graphql-box/helpers");
38
38
 
39
- var _cacheability = _interopRequireDefault(require("cacheability"));
39
+ var _cacheability2 = _interopRequireDefault(require("cacheability"));
40
40
 
41
41
  var _graphql = require("graphql");
42
42
 
@@ -44,6 +44,18 @@ var _consts = require("../consts");
44
44
 
45
45
  var _debug = require("../debug");
46
46
 
47
+ var _buildKeysAndPaths = require("../helpers/buildKeysAndPaths");
48
+
49
+ var _deriveOpCacheability = _interopRequireDefault(require("../helpers/deriveOpCacheability"));
50
+
51
+ var _filterOutPropsWithArgsOrDirectives = _interopRequireDefault(require("../helpers/filterOutPropsWithArgsOrDirectives"));
52
+
53
+ var _filterQuery = _interopRequireDefault(require("../helpers/filterQuery"));
54
+
55
+ var _normalizeResponseData = _interopRequireDefault(require("../helpers/normalizeResponseData"));
56
+
57
+ var _validTypeIDValue = require("../helpers/validTypeIDValue");
58
+
47
59
  var _dec, _dec2, _dec3, _class;
48
60
 
49
61
  let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCacheEntry)(), _dec3 = (0, _debug.logPartialCompiled)(), (_class = class CacheManager {
@@ -59,85 +71,11 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
59
71
  errors.push(new TypeError(message));
60
72
  }
61
73
 
62
- if (errors.length) return Promise.reject(errors);
63
- return new CacheManager(options);
64
- }
65
-
66
- static _analyzeLeafField(field, cachedAncestorFieldData, {
67
- data,
68
- fieldPathChecklist
69
- }, _options, _context) {
70
- const keysAndPaths = CacheManager._getFieldKeysAndPaths(field, cachedAncestorFieldData);
71
-
72
- const {
73
- propNameOrIndex,
74
- requestFieldPath
75
- } = keysAndPaths;
76
- const {
77
- dataEntityData,
78
- requestFieldPathData,
79
- typeName
80
- } = cachedAncestorFieldData;
81
-
82
- const cachedFieldData = CacheManager._getFieldDataFromAncestor(dataEntityData, propNameOrIndex) || CacheManager._getFieldDataFromAncestor(requestFieldPathData, propNameOrIndex);
83
-
84
- const typeNames = {
85
- dataTypeName: dataEntityData.__typename || requestFieldPathData.__typename,
86
- fieldTypeName: typeName
87
- };
88
-
89
- CacheManager._setFieldPathChecklist(fieldPathChecklist, {
90
- data: cachedFieldData
91
- }, requestFieldPath, typeNames);
92
-
93
- CacheManager._setCachedData(data, {
94
- data: cachedFieldData
95
- }, propNameOrIndex);
96
- }
97
-
98
- static _buildKey(key, path) {
99
- const paths = [];
100
- if (path.length) paths.push(path);
101
- paths.push(key);
102
- return paths.join(".");
103
- }
104
-
105
- static _buildRequestFieldCacheKey(name, requestFieldCacheKey, args, directives, index) {
106
- let key = `${(0, _isNumber2.default)(index) ? index : name}`;
107
- if (args) key = `${key}(${JSON.stringify(args)})`;
108
- if (directives) key = `${key}(${JSON.stringify(directives)})`;
109
- return CacheManager._buildKey(key, requestFieldCacheKey);
110
- }
111
-
112
- static _checkFieldPathChecklist(fieldPathChecklistValues, fieldTypeName) {
113
- if (!fieldPathChecklistValues || !fieldPathChecklistValues.length) {
114
- return {
115
- hasData: false,
116
- typeUnused: !!fieldTypeName
117
- };
118
- }
119
-
120
- if (fieldPathChecklistValues.length === 1) {
121
- const {
122
- hasData,
123
- typeName
124
- } = fieldPathChecklistValues[0];
125
- const typeUnused = !typeName ? undefined : typeName !== fieldTypeName;
126
- return {
127
- hasData,
128
- typeUnused
129
- };
74
+ if (errors.length) {
75
+ return Promise.reject(errors);
130
76
  }
131
77
 
132
- return {
133
- hasData: fieldPathChecklistValues.some(({
134
- hasData,
135
- typeName
136
- }) => typeName === fieldTypeName && hasData),
137
- typeUnused: !fieldPathChecklistValues.every(({
138
- typeName
139
- }) => typeName === fieldTypeName)
140
- };
78
+ return new CacheManager(options);
141
79
  }
142
80
 
143
81
  static _countFieldPathChecklist(fieldPathChecklist) {
@@ -156,49 +94,33 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
156
94
  }
157
95
 
158
96
  static _getFieldDataFromAncestor(ancestorFieldData, propNameOrIndex) {
159
- return (0, _isObjectLike2.default)(ancestorFieldData) ? ancestorFieldData[propNameOrIndex] : undefined;
160
- }
161
-
162
- static _getFieldKeysAndPaths(field, options) {
163
- const {
164
- index,
165
- requestFieldCacheKey = "",
166
- requestFieldPath = "",
167
- responseDataPath = ""
168
- } = options;
169
- const name = (0, _helpers.getName)(field);
170
-
171
- const updatedRequestFieldCacheKey = CacheManager._buildRequestFieldCacheKey(name, requestFieldCacheKey, (0, _helpers.getArguments)(field), (0, _helpers.getDirectives)(field), index);
172
-
173
- const fieldAliasOrName = (0, _helpers.getAlias)(field) || name;
174
- const updatedRequestFieldPath = (0, _isNumber2.default)(index) ? requestFieldPath : CacheManager._buildKey(fieldAliasOrName, requestFieldPath);
175
- const propNameOrIndex = (0, _isNumber2.default)(index) ? index : fieldAliasOrName;
176
-
177
- const updatedResponseDataPath = CacheManager._buildKey(propNameOrIndex, responseDataPath);
178
-
179
- return {
180
- hashedRequestFieldCacheKey: (0, _helpers.hashRequest)(updatedRequestFieldCacheKey),
181
- propNameOrIndex,
182
- requestFieldCacheKey: updatedRequestFieldCacheKey,
183
- requestFieldPath: updatedRequestFieldPath,
184
- responseDataPath: updatedResponseDataPath
185
- };
97
+ return (0, _isObjectLike2.default)(ancestorFieldData) ? (0, _cloneDeep2.default)(ancestorFieldData[propNameOrIndex]) : undefined;
186
98
  }
187
99
 
188
100
  static _getOperationCacheControl(cacheMetadata, operation) {
189
101
  const defaultCacheControl = _consts.HEADER_NO_CACHE;
190
- if (!cacheMetadata) return defaultCacheControl;
102
+
103
+ if (!cacheMetadata) {
104
+ return defaultCacheControl;
105
+ }
106
+
191
107
  const cacheability = cacheMetadata.get(operation);
192
108
  return cacheability ? cacheability.printCacheControl() : defaultCacheControl;
193
109
  }
194
110
 
195
111
  static _getResponseCacheMetadata(cacheMetadata, partialQueryResponse) {
196
- if (!partialQueryResponse) return cacheMetadata;
112
+ if (!partialQueryResponse) {
113
+ return cacheMetadata;
114
+ }
115
+
197
116
  return new Map([...partialQueryResponse.cacheMetadata, ...cacheMetadata]);
198
117
  }
199
118
 
200
- static _isDataEntity(fieldTypeInfo) {
201
- if (!fieldTypeInfo) return false;
119
+ static _isNodeEntity(fieldTypeInfo) {
120
+ if (!fieldTypeInfo) {
121
+ return false;
122
+ }
123
+
202
124
  const {
203
125
  isEntity,
204
126
  possibleTypes
@@ -206,8 +128,8 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
206
128
  return isEntity || possibleTypes.some(type => !!type.isEntity);
207
129
  }
208
130
 
209
- static _isRequestFieldPath(fieldTypeInfo) {
210
- return !!fieldTypeInfo && (this._isDataEntity(fieldTypeInfo) || fieldTypeInfo.hasArguments || fieldTypeInfo.hasDirectives);
131
+ static _isNodeRequestFieldPath(fieldTypeInfo) {
132
+ return !!fieldTypeInfo && (this._isNodeEntity(fieldTypeInfo) || fieldTypeInfo.hasArguments || fieldTypeInfo.hasDirectives);
211
133
  }
212
134
 
213
135
  static _isValid(cacheability) {
@@ -233,18 +155,21 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
233
155
  }, {
234
156
  propNameOrIndex,
235
157
  requestFieldPath
236
- }, typeNames, _options, {
158
+ }, typeNamesAndKind, _options, {
237
159
  operation
238
160
  }) {
239
161
  CacheManager._setCacheMetadata(cacheMetadata, cachedFieldData.cacheability, requestFieldPath, operation);
240
162
 
241
- CacheManager._setFieldPathChecklist(fieldPathChecklist, cachedFieldData, requestFieldPath, typeNames);
163
+ CacheManager._setFieldPathChecklist(fieldPathChecklist, cachedFieldData, requestFieldPath, typeNamesAndKind);
242
164
 
243
165
  CacheManager._setCachedData(data, cachedFieldData, propNameOrIndex);
244
166
  }
245
167
 
246
168
  static _setCacheMetadata(cacheMetadata, cacheability, requestFieldPath, operation) {
247
- if (!cacheability) return;
169
+ if (!cacheability) {
170
+ return;
171
+ }
172
+
248
173
  cacheMetadata.set(requestFieldPath, cacheability);
249
174
  const operationCacheability = cacheMetadata.get(operation);
250
175
 
@@ -257,23 +182,39 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
257
182
  data
258
183
  }, requestFieldPath, {
259
184
  dataTypeName,
260
- fieldTypeName
185
+ fieldTypeName,
186
+ fragmentKind,
187
+ fragmentName
261
188
  }) {
262
- if ((0, _isUndefined2.default)(fieldTypeName)) {
263
- if (fieldPathChecklist.has(requestFieldPath)) return;
189
+ if ((0, _isUndefined2.default)(fieldTypeName) || fragmentKind === _helpers.FRAGMENT_SPREAD) {
190
+ if (fieldPathChecklist.has(requestFieldPath)) {
191
+ return;
192
+ }
193
+
264
194
  fieldPathChecklist.set(requestFieldPath, [{
195
+ fragmentKind,
196
+ fragmentName,
265
197
  hasData: !(0, _isUndefined2.default)(data)
266
198
  }]);
267
199
  return;
268
200
  }
269
201
 
270
- if (dataTypeName !== fieldTypeName) return;
202
+ if (dataTypeName !== fieldTypeName) {
203
+ return;
204
+ }
205
+
271
206
  const entry = fieldPathChecklist.get(requestFieldPath);
272
207
  const checklistValues = entry ? entry : [];
208
+
273
209
  if (checklistValues.some(({
274
210
  typeName
275
- }) => typeName === dataTypeName)) return;
211
+ }) => typeName === dataTypeName)) {
212
+ return;
213
+ }
214
+
276
215
  fieldPathChecklist.set(requestFieldPath, [...checklistValues, {
216
+ fragmentKind,
217
+ fragmentName,
277
218
  hasData: !(0, _isUndefined2.default)(data),
278
219
  typeName: dataTypeName
279
220
  }]);
@@ -314,23 +255,33 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
314
255
  return Promise.reject(new TypeError("@graphql-box/cache-manager expected an AST."));
315
256
  }
316
257
 
317
- const cachedResponseData = await this._getCachedResponseData(requestData, options, context);
258
+ const cacheManagerContext = { ...context,
259
+ fragmentDefinitions: (0, _helpers.getFragmentDefinitions)(ast),
260
+ typeIDKey: this._typeIDKey
261
+ };
262
+ const cachedResponseData = await this._retrieveCachedResponseData(requestData, options, cacheManagerContext);
318
263
  const {
319
264
  cacheMetadata,
320
265
  data,
321
266
  fieldCount
322
267
  } = cachedResponseData;
323
- if (fieldCount.missing === fieldCount.total) return {
324
- updated: requestData
325
- };
268
+
269
+ if (fieldCount.missing === fieldCount.total) {
270
+ return {
271
+ updated: requestData
272
+ };
273
+ }
326
274
 
327
275
  if (!fieldCount.missing) {
328
276
  const dataCaching = this._setQueryResponseCacheEntry(hash, {
329
277
  cacheMetadata,
330
278
  data
331
- }, options, context);
279
+ }, options, cacheManagerContext);
280
+
281
+ if (options.awaitDataCaching) {
282
+ await dataCaching;
283
+ }
332
284
 
333
- if (options.awaitDataCaching) await dataCaching;
334
285
  return {
335
286
  response: {
336
287
  cacheMetadata,
@@ -342,14 +293,19 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
342
293
  this._setPartialQueryResponse(hash, {
343
294
  cacheMetadata,
344
295
  data
345
- }, options, context);
296
+ }, options, cacheManagerContext);
346
297
 
347
- this._filterQuery(requestData, cachedResponseData, context);
348
-
349
- const request = (0, _graphql.print)(ast);
298
+ const filteredAST = (0, _filterQuery.default)(requestData, cachedResponseData, cacheManagerContext);
299
+ const {
300
+ fragmentDefinitions,
301
+ typeIDKey,
302
+ ...rest
303
+ } = cacheManagerContext;
304
+ (0, _assign2.default)(context, rest);
305
+ const request = (0, _graphql.print)(filteredAST);
350
306
  return {
351
307
  updated: {
352
- ast,
308
+ ast: filteredAST,
353
309
  hash: (0, _helpers.hashRequest)(request),
354
310
  request
355
311
  }
@@ -362,7 +318,11 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
362
318
 
363
319
  async checkQueryResponseCacheEntry(hash, options, context) {
364
320
  const result = await this._checkCacheEntry(_core.QUERY_RESPONSES, hash, options, context);
365
- if (!result) return false;
321
+
322
+ if (!result) {
323
+ return false;
324
+ }
325
+
366
326
  const {
367
327
  cacheMetadata,
368
328
  data
@@ -378,18 +338,23 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
378
338
  }
379
339
 
380
340
  async resolveQuery(requestData, updatedRequestData, rawResponseData, options, context) {
341
+ const cacheManagerContext = { ...context,
342
+ fragmentDefinitions: (0, _helpers.getFragmentDefinitions)(updatedRequestData.ast),
343
+ typeIDKey: this._typeIDKey
344
+ };
381
345
  const dataCaching = [];
382
346
  const {
383
347
  cacheMetadata,
384
- data
385
- } = await this._resolveRequest(updatedRequestData, rawResponseData, options, context);
348
+ data,
349
+ hasNext
350
+ } = await this._resolveRequest(updatedRequestData, rawResponseData, options, cacheManagerContext);
386
351
  let partialQueryResponse;
387
352
 
388
- if (context.queryFiltered) {
353
+ if (cacheManagerContext.queryFiltered) {
389
354
  dataCaching.push(this._setQueryResponseCacheEntry(updatedRequestData.hash, {
390
355
  cacheMetadata,
391
356
  data
392
- }, options, context));
357
+ }, options, cacheManagerContext));
393
358
  partialQueryResponse = this._getPartialQueryResponse(requestData.hash);
394
359
  }
395
360
 
@@ -400,79 +365,127 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
400
365
  dataCaching.push(this._setQueryResponseCacheEntry(requestData.hash, {
401
366
  cacheMetadata: responseCacheMetadata,
402
367
  data: responseData
403
- }, options, context));
404
- if (options.awaitDataCaching) await Promise.all(dataCaching);
368
+ }, options, cacheManagerContext));
369
+
370
+ if (options.awaitDataCaching) {
371
+ await Promise.all(dataCaching);
372
+ }
373
+
405
374
  return {
406
375
  cacheMetadata: responseCacheMetadata,
407
- data: responseData
376
+ data: responseData,
377
+ hasNext
408
378
  };
409
379
  }
410
380
 
411
381
  async resolveRequest(requestData, rawResponseData, options, context) {
412
- return this._resolveRequest(requestData, rawResponseData, options, context);
382
+ const cacheManagerContext = { ...context,
383
+ fragmentDefinitions: (0, _helpers.getFragmentDefinitions)(requestData.ast),
384
+ typeIDKey: this._typeIDKey
385
+ };
386
+ return this._resolveRequest(requestData, rawResponseData, options, cacheManagerContext);
413
387
  }
414
388
 
415
- async _analyzeField(field, cachedAncestorFieldData, cachedResponseData, options, context) {
416
- if ((0, _helpers.hasChildFields)(field)) {
417
- await this._analyzeParentField(field, cachedAncestorFieldData, cachedResponseData, options, context);
389
+ async _analyzeFieldNode(fieldNode, cachedAncestorFieldData, cachedResponseData, options, context) {
390
+ if ((0, _helpers.hasChildFields)(fieldNode)) {
391
+ await this._analyzeParentFieldNode(fieldNode, cachedAncestorFieldData, cachedResponseData, options, context);
418
392
  } else {
419
- await CacheManager._analyzeLeafField(field, cachedAncestorFieldData, cachedResponseData, options, context);
393
+ await this._analyzeLeafFieldNode(fieldNode, cachedAncestorFieldData, cachedResponseData, options, context);
420
394
  }
421
395
  }
422
396
 
423
- async _analyzeParentField(field, cachedAncestorFieldData, cachedResponseData, options, context) {
424
- const keysAndPaths = CacheManager._getFieldKeysAndPaths(field, cachedAncestorFieldData);
425
-
397
+ async _analyzeLeafFieldNode(fieldNode, cachedAncestorFieldData, cachedResponseData, options, context) {
398
+ const keysAndPaths = (0, _buildKeysAndPaths.buildFieldKeysAndPaths)(fieldNode, cachedAncestorFieldData, context);
426
399
  const {
427
400
  hashedRequestFieldCacheKey,
428
401
  propNameOrIndex,
429
- requestFieldCacheKey,
430
402
  requestFieldPath
431
403
  } = keysAndPaths;
432
404
  const fieldTypeInfo = context.fieldTypeMap.get(requestFieldPath);
433
405
  const {
434
- dataEntityData: ancestorDataEntityData,
435
- requestFieldPathData: ancestorRequestFieldPathData,
406
+ entityData,
407
+ fragmentKind,
408
+ fragmentName,
409
+ requestFieldPathData,
436
410
  typeName
437
411
  } = cachedAncestorFieldData;
438
- const cachedFieldData = {
439
- dataEntityData: CacheManager._getFieldDataFromAncestor(ancestorDataEntityData, propNameOrIndex),
440
- requestFieldPathData: CacheManager._getFieldDataFromAncestor(ancestorRequestFieldPathData, propNameOrIndex)
412
+ const typeNamesAndKind = {
413
+ dataTypeName: (entityData === null || entityData === void 0 ? void 0 : entityData.__typename) || (requestFieldPathData === null || requestFieldPathData === void 0 ? void 0 : requestFieldPathData.__typename),
414
+ fieldTypeName: typeName,
415
+ fragmentKind,
416
+ fragmentName
441
417
  };
442
418
 
443
- if (CacheManager._isRequestFieldPath(fieldTypeInfo)) {
444
- await this._setRequestFieldPathData(cachedFieldData, hashedRequestFieldCacheKey, options, context);
445
- }
419
+ if (CacheManager._isNodeRequestFieldPath(fieldTypeInfo)) {
420
+ const {
421
+ cacheability,
422
+ entry
423
+ } = await this._retrieveCachedRequestFieldPathData(hashedRequestFieldCacheKey, options, context);
424
+
425
+ CacheManager._setCachedResponseData({
426
+ cacheability,
427
+ data: entry
428
+ }, cachedResponseData, keysAndPaths, typeNamesAndKind, options, context);
429
+ } else {
430
+ const cachedFieldData = CacheManager._getFieldDataFromAncestor(entityData, propNameOrIndex) || CacheManager._getFieldDataFromAncestor(requestFieldPathData, propNameOrIndex);
431
+
432
+ CacheManager._setFieldPathChecklist(cachedResponseData.fieldPathChecklist, {
433
+ data: cachedFieldData
434
+ }, requestFieldPath, typeNamesAndKind);
446
435
 
447
- if (CacheManager._isDataEntity(fieldTypeInfo)) {
448
- await this._setDataEntityData(cachedFieldData, fieldTypeInfo, options, context);
436
+ CacheManager._setCachedData(cachedResponseData.data, {
437
+ data: cachedFieldData
438
+ }, propNameOrIndex);
449
439
  }
440
+ }
450
441
 
442
+ async _analyzeParentFieldNode(fieldNode, cachedAncestorFieldData, cachedResponseData, options, context) {
443
+ const keysAndPaths = (0, _buildKeysAndPaths.buildFieldKeysAndPaths)(fieldNode, cachedAncestorFieldData, context);
444
+ const {
445
+ propNameOrIndex,
446
+ requestFieldCacheKey,
447
+ requestFieldPath
448
+ } = keysAndPaths;
449
+ const fieldTypeInfo = context.fieldTypeMap.get(requestFieldPath);
451
450
  const {
452
451
  cacheability,
453
- dataEntityData,
452
+ data,
453
+ entityData,
454
454
  requestFieldPathData
455
- } = cachedFieldData;
456
- const data = !(0, _isUndefined2.default)(requestFieldPathData) || !(0, _isUndefined2.default)(dataEntityData) ? this._mergeObjects(requestFieldPathData, dataEntityData) : undefined;
455
+ } = await this._retrieveCachedParentNodeData(cachedAncestorFieldData, keysAndPaths, fieldTypeInfo, options, context);
456
+ const {
457
+ fragmentKind,
458
+ fragmentName,
459
+ typeName
460
+ } = cachedAncestorFieldData;
457
461
 
458
462
  CacheManager._setCachedResponseData({
459
463
  cacheability,
460
464
  data
461
465
  }, cachedResponseData, keysAndPaths, {
462
466
  dataTypeName: (0, _get2.default)(data, _core.TYPE_NAME_KEY),
463
- fieldTypeName: typeName
467
+ fieldTypeName: typeName,
468
+ fragmentKind,
469
+ fragmentName
464
470
  }, options, context);
465
471
 
466
- if (!(0, _isObjectLike2.default)(data)) return;
472
+ if (!(0, _isObjectLike2.default)(data)) {
473
+ return;
474
+ }
475
+
467
476
  const objectLikeData = data;
468
477
  const promises = [];
469
- (0, _helpers.iterateChildFields)(field, objectLikeData, (childField, childTypeName, childIndex) => {
470
- promises.push(this._analyzeField(childField, {
478
+ (0, _helpers.iterateChildFields)(fieldNode, objectLikeData, context.fragmentDefinitions, (childField, childTypeName, childFragmentKind, childFragmentName, childIndex) => {
479
+ promises.push(this._analyzeFieldNode(childField, {
480
+ cacheability,
481
+ entityData,
482
+ fragmentKind: childFragmentKind,
483
+ fragmentName: childFragmentName,
471
484
  index: childIndex,
472
485
  requestFieldCacheKey,
473
486
  requestFieldPath,
474
- typeName: childTypeName,
475
- ...cachedFieldData
487
+ requestFieldPathData,
488
+ typeName: childTypeName
476
489
  }, { ...cachedResponseData,
477
490
  data: cachedResponseData.data[propNameOrIndex]
478
491
  }, options, context));
@@ -493,7 +506,11 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
493
506
 
494
507
  const queryNode = (0, _helpers.getOperationDefinitions)(ast, context.operation)[0];
495
508
  const fieldsAndTypeNames = (0, _helpers.getChildFields)(queryNode);
496
- if (!fieldsAndTypeNames) return cacheMetadata;
509
+
510
+ if (!fieldsAndTypeNames) {
511
+ return cacheMetadata;
512
+ }
513
+
497
514
  fieldsAndTypeNames.forEach(({
498
515
  fieldNode
499
516
  }) => this._setFieldCacheability(fieldNode, {
@@ -508,9 +525,17 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
508
525
  async _checkCacheEntry(cacheType, hash, options, context) {
509
526
  try {
510
527
  const cacheability = await this._hasCacheEntry(cacheType, hash);
511
- if (!cacheability || !CacheManager._isValid(cacheability)) return false;
528
+
529
+ if (!cacheability || !CacheManager._isValid(cacheability)) {
530
+ return false;
531
+ }
532
+
512
533
  const entry = await this._getCacheEntry(cacheType, hash, options, context);
513
- if (!entry) return false;
534
+
535
+ if ((0, _isUndefined2.default)(entry)) {
536
+ return false;
537
+ }
538
+
514
539
  return {
515
540
  cacheability,
516
541
  entry
@@ -527,157 +552,18 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
527
552
  operation
528
553
  }) {
529
554
  const cacheMetadata = new Map();
530
-
531
- const cacheControl = headers && headers.get(_consts.HEADER_CACHE_CONTROL) || this._fallbackOperationCacheability;
532
-
533
- const cacheability = new _cacheability.default({
534
- cacheControl
555
+ const cacheability = (0, _deriveOpCacheability.default)({
556
+ _cacheMetadata,
557
+ fallback: this._fallbackOperationCacheability,
558
+ headers
535
559
  });
536
560
  cacheMetadata.set(operation, cacheability);
537
- if (_cacheMetadata) (0, _helpers.rehydrateCacheMetadata)(_cacheMetadata, cacheMetadata);
538
- return cacheMetadata;
539
- }
540
-
541
- _filterField(field, fieldPathChecklist, ancestorRequestFieldPath, context) {
542
- const fieldsAndTypeNames = (0, _helpers.getChildFields)(field);
543
- if (!fieldsAndTypeNames) return false;
544
561
 
545
- for (let i = fieldsAndTypeNames.length - 1; i >= 0; i -= 1) {
546
- const {
547
- fieldNode: childField,
548
- typeName: childTypeName
549
- } = fieldsAndTypeNames[i];
550
- const childFieldName = (0, _helpers.getName)(childField);
551
- if (childFieldName === this._typeIDKey || childFieldName === _core.TYPE_NAME_KEY) continue;
552
-
553
- const {
554
- requestFieldPath
555
- } = CacheManager._getFieldKeysAndPaths(childField, {
556
- requestFieldPath: ancestorRequestFieldPath
557
- });
558
-
559
- const {
560
- hasData,
561
- typeUnused
562
- } = CacheManager._checkFieldPathChecklist(fieldPathChecklist.get(requestFieldPath), childTypeName);
563
-
564
- if (hasData || typeUnused) {
565
- if (!(0, _helpers.hasChildFields)(childField)) {
566
- (0, _helpers.deleteChildFields)(field, childField);
567
- } else if (this._filterField(childField, fieldPathChecklist, requestFieldPath, context)) {
568
- (0, _helpers.deleteChildFields)(field, childField);
569
- }
570
- }
562
+ if (_cacheMetadata) {
563
+ (0, _helpers.rehydrateCacheMetadata)(_cacheMetadata, cacheMetadata);
571
564
  }
572
565
 
573
- this._filterInlineFragments(field);
574
-
575
- this._filterIDsAndTypeNames(field);
576
-
577
- return !(0, _helpers.hasChildFields)(field);
578
- }
579
-
580
- _filterIDsAndTypeNames(field) {
581
- const fieldsAndTypeNames = (0, _helpers.getChildFields)(field);
582
- if (!fieldsAndTypeNames || fieldsAndTypeNames.length > 3) return false;
583
- const fieldNames = fieldsAndTypeNames.map(({
584
- fieldNode
585
- }) => (0, _helpers.getName)(fieldNode));
586
-
587
- if (fieldNames.length === 2 && fieldNames.every(name => name === this._typeIDKey || name === _core.TYPE_NAME_KEY)) {
588
- (0, _helpers.deleteChildFields)(field, fieldsAndTypeNames.map(({
589
- fieldNode
590
- }) => fieldNode));
591
- return true;
592
- }
593
-
594
- if (fieldNames.length === 1 && fieldNames[0] === this._typeIDKey || fieldNames[0] === _core.TYPE_NAME_KEY) {
595
- const {
596
- fieldNode
597
- } = fieldsAndTypeNames[0];
598
- (0, _helpers.deleteChildFields)(field, fieldNode);
599
- return true;
600
- }
601
-
602
- return false;
603
- }
604
-
605
- _filterInlineFragments(field) {
606
- const inlineFragments = (0, _helpers.getInlineFragments)(field);
607
- let filtered = false;
608
- inlineFragments.forEach(fragment => {
609
- const fieldsAndTypeNames = (0, _helpers.getChildFields)(fragment);
610
-
611
- if (!fieldsAndTypeNames || !fieldsAndTypeNames.length) {
612
- (0, _helpers.deleteInlineFragments)(field, fragment);
613
- filtered = true;
614
- return;
615
- }
616
-
617
- if (fieldsAndTypeNames.length === 1) {
618
- const {
619
- fieldNode
620
- } = fieldsAndTypeNames[0];
621
-
622
- if ((0, _helpers.getName)(fieldNode) === this._typeIDKey) {
623
- (0, _helpers.deleteInlineFragments)(field, fragment);
624
- filtered = true;
625
- }
626
- }
627
- });
628
- return filtered;
629
- }
630
-
631
- _filterQuery({
632
- ast
633
- }, {
634
- fieldPathChecklist
635
- }, context) {
636
- const queryNode = (0, _helpers.getOperationDefinitions)(ast, context.operation)[0];
637
- const fieldsAndTypeNames = (0, _helpers.getChildFields)(queryNode);
638
- if (!fieldsAndTypeNames) return;
639
-
640
- for (let i = fieldsAndTypeNames.length - 1; i >= 0; i -= 1) {
641
- const {
642
- fieldNode
643
- } = fieldsAndTypeNames[i];
644
-
645
- const {
646
- requestFieldPath
647
- } = CacheManager._getFieldKeysAndPaths(fieldNode, {
648
- requestFieldPath: context.operation
649
- });
650
-
651
- if (this._filterField(fieldNode, fieldPathChecklist, requestFieldPath, context)) {
652
- (0, _helpers.deleteChildFields)(queryNode, fieldNode);
653
- }
654
- }
655
-
656
- context.queryFiltered = true;
657
- }
658
-
659
- async _getCachedResponseData({
660
- ast
661
- }, options, context) {
662
- const cachedResponseData = {
663
- cacheMetadata: new Map(),
664
- data: {},
665
- fieldCount: {
666
- missing: 0,
667
- total: 0
668
- },
669
- fieldPathChecklist: new Map()
670
- };
671
- const queryNode = (0, _helpers.getOperationDefinitions)(ast, context.operation)[0];
672
- const fieldsAndTypeNames = (0, _helpers.getChildFields)(queryNode);
673
- if (!fieldsAndTypeNames) return cachedResponseData;
674
- await Promise.all(fieldsAndTypeNames.map(({
675
- fieldNode
676
- }) => this._analyzeField(fieldNode, {
677
- requestFieldPath: context.operation
678
- }, cachedResponseData, options, context)));
679
- cachedResponseData.fieldCount = CacheManager._countFieldPathChecklist(cachedResponseData.fieldPathChecklist);
680
- return cachedResponseData;
566
+ return cacheMetadata;
681
567
  }
682
568
 
683
569
  async _getCacheEntry(cacheType, hash, _options, _context) {
@@ -697,7 +583,10 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
697
583
  }
698
584
 
699
585
  _getResponseData(responseData, partialQueryResponse) {
700
- if (!partialQueryResponse) return responseData;
586
+ if (!partialQueryResponse) {
587
+ return responseData;
588
+ }
589
+
701
590
  return this._mergeObjects(partialQueryResponse.data, responseData);
702
591
  }
703
592
 
@@ -713,9 +602,18 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
713
602
  isEntity,
714
603
  possibleTypes
715
604
  }) {
716
- if (!(0, _get2.default)(fieldData, this._typeIDKey, null)) return false;
717
- if (isEntity) return true;
718
- if (!possibleTypes.length) return false;
605
+ if (!(0, _get2.default)(fieldData, this._typeIDKey, null)) {
606
+ return false;
607
+ }
608
+
609
+ if (isEntity) {
610
+ return true;
611
+ }
612
+
613
+ if (!possibleTypes.length) {
614
+ return false;
615
+ }
616
+
719
617
  return possibleTypes.some(type => type.typeName === fieldData.__typename);
720
618
  }
721
619
 
@@ -725,126 +623,79 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
725
623
  });
726
624
  }
727
625
 
728
- async _parseFieldDataEntityAndRequestFieldPathCacheEntryData(field, ancestorKeysAndPaths, {
626
+ async _parseEntityAndRequestFieldPathCacheEntryData(field, ancestorKeysAndPaths, {
729
627
  cacheMetadata,
730
- dataEntityData,
628
+ entityData,
731
629
  requestFieldPathData
732
630
  }, options, context) {
733
- const keysAndPaths = CacheManager._getFieldKeysAndPaths(field, ancestorKeysAndPaths);
734
-
631
+ const keysAndPaths = (0, _buildKeysAndPaths.buildFieldKeysAndPaths)(field, ancestorKeysAndPaths, context);
735
632
  const {
736
633
  requestFieldCacheKey,
737
634
  requestFieldPath,
738
635
  responseDataPath
739
636
  } = keysAndPaths;
740
- const fieldData = (0, _get2.default)(requestFieldPathData, responseDataPath, null);
741
- if (!(0, _isObjectLike2.default)(fieldData)) return;
742
- const objectLikeFieldData = fieldData;
743
- const promises = [];
744
- (0, _helpers.iterateChildFields)(field, objectLikeFieldData, (childField, _typeName, childIndex) => {
745
- promises.push(this._parseFieldDataEntityAndRequestFieldPathCacheEntryData(childField, {
746
- index: childIndex,
747
- requestFieldCacheKey,
748
- requestFieldPath,
749
- responseDataPath
750
- }, {
751
- cacheMetadata,
752
- dataEntityData,
753
- requestFieldPathData
754
- }, options, context));
755
- });
756
- await Promise.all(promises);
757
- await this._setFieldDataEntityAndRequestFieldPathCacheEntry(field, keysAndPaths, {
637
+ const fieldData = (0, _get2.default)(requestFieldPathData, responseDataPath);
638
+ const fieldTypeInfo = context.fieldTypeMap.get(requestFieldPath);
639
+
640
+ if (!(0, _isObjectLike2.default)(fieldData) && !(fieldTypeInfo !== null && fieldTypeInfo !== void 0 && fieldTypeInfo.hasDirectives)) {
641
+ return;
642
+ }
643
+
644
+ if ((0, _isObjectLike2.default)(fieldData)) {
645
+ const promises = [];
646
+ (0, _helpers.iterateChildFields)(field, fieldData, context.fragmentDefinitions, (childField, _typeName, _fragmentKind, _fragmentName, childIndex) => {
647
+ promises.push(this._parseEntityAndRequestFieldPathCacheEntryData(childField, {
648
+ index: childIndex,
649
+ requestFieldCacheKey,
650
+ requestFieldPath,
651
+ responseDataPath
652
+ }, {
653
+ cacheMetadata,
654
+ entityData,
655
+ requestFieldPathData
656
+ }, options, context));
657
+ });
658
+ await Promise.all(promises);
659
+ }
660
+
661
+ await this._setEntityAndRequestFieldPathCacheEntry(field, keysAndPaths, {
758
662
  cacheMetadata,
759
- dataEntityData,
663
+ entityData,
760
664
  requestFieldPathData
761
665
  }, options, context);
762
666
  }
763
667
 
764
668
  async _resolveRequest(requestData, rawResponseData, options, context) {
669
+ const normalizedResponseData = rawResponseData.path ? (0, _normalizeResponseData.default)(rawResponseData) : rawResponseData;
765
670
  const dataCaching = [];
766
671
 
767
- const cacheMetadata = this._buildCacheMetadata(requestData, rawResponseData, options, context);
672
+ const cacheMetadata = this._buildCacheMetadata(requestData, normalizedResponseData, options, context);
768
673
 
769
674
  const {
770
- data
771
- } = rawResponseData;
772
- dataCaching.push(this._setDataEntityAndRequestFieldPathCacheEntries(requestData, {
675
+ data,
676
+ hasNext
677
+ } = normalizedResponseData;
678
+ dataCaching.push(this._setEntityAndRequestFieldPathCacheEntries(requestData, {
773
679
  cacheMetadata,
774
- dataEntityData: (0, _cloneDeep2.default)(data),
680
+ entityData: (0, _cloneDeep2.default)(data),
775
681
  requestFieldPathData: (0, _cloneDeep2.default)(data)
776
682
  }, options, context));
777
- if (options.awaitDataCaching) await Promise.all(dataCaching);
778
- return {
779
- cacheMetadata,
780
- data
781
- };
782
- }
783
-
784
- async _setCacheEntry(cacheType, hash, value, cachemapOptions, _options, _context) {
785
- try {
786
- await this._cache.set(`${cacheType}::${hash}`, (0, _cloneDeep2.default)(value), cachemapOptions);
787
- } catch (error) {}
788
- }
789
683
 
790
- async _setDataEntityAndRequestFieldPathCacheEntries(requestData, responseData, options, context) {
791
- const operationNode = (0, _helpers.getOperationDefinitions)(requestData.ast, context.operation)[0];
792
- const fieldsAndTypeNames = (0, _helpers.getChildFields)(operationNode);
793
- if (!fieldsAndTypeNames) return;
794
- await Promise.all(fieldsAndTypeNames.map(({
795
- fieldNode
796
- }) => {
797
- return this._parseFieldDataEntityAndRequestFieldPathCacheEntryData(fieldNode, {
798
- requestFieldPath: context.operation
799
- }, responseData, options, context);
800
- }));
801
- }
802
-
803
- async _setDataEntityCacheEntry({
804
- responseDataPath
805
- }, {
806
- cacheability,
807
- data,
808
- fieldTypeInfo
809
- }, options, context) {
810
- const hasArgsOrDirectives = fieldTypeInfo.hasArguments || fieldTypeInfo.hasDirectives;
811
- let fieldData = (0, _get2.default)(data, responseDataPath, null);
812
-
813
- const isEntity = this._isFieldEntity(fieldData, fieldTypeInfo);
814
-
815
- if (!isEntity && hasArgsOrDirectives) {
816
- (0, _unset2.default)(data, responseDataPath);
684
+ if (options.awaitDataCaching) {
685
+ await Promise.all(dataCaching);
817
686
  }
818
687
 
819
- if (isEntity) {
820
- const fieldTypeName = fieldTypeInfo.isEntity ? fieldTypeInfo.typeName : fieldData.__typename;
821
- const entityDataKey = `${fieldTypeName}::${fieldData[this._typeIDKey]}`;
822
- const result = await this._checkCacheEntry(_core.DATA_ENTITIES, entityDataKey, options, context);
823
-
824
- if (result) {
825
- fieldData = this._mergeObjects(result.entry, fieldData);
826
- }
827
-
828
- await this._setCacheEntry(_core.DATA_ENTITIES, entityDataKey, fieldData, {
829
- cacheHeaders: {
830
- cacheControl: cacheability.printCacheControl()
831
- },
832
- tag: options.tag
833
- }, options, context);
834
- (0, _set2.default)(data, responseDataPath, {
835
- __cacheKey: `${_core.DATA_ENTITIES}::${entityDataKey}`
836
- });
837
- }
688
+ return {
689
+ cacheMetadata,
690
+ data,
691
+ hasNext
692
+ };
838
693
  }
839
694
 
840
- async _setDataEntityData(cachedFieldData, {
695
+ async _retrieveCachedEntityData(validTypeIDValue, {
841
696
  possibleTypes,
842
- typeIDValue,
843
697
  typeName
844
698
  }, options, context) {
845
- const requestFieldPathDataIDValue = (0, _isPlainObject2.default)(cachedFieldData.requestFieldPathData) ? cachedFieldData.requestFieldPathData[this._typeIDKey] : undefined;
846
- const validTypeIDValue = typeIDValue || requestFieldPathDataIDValue;
847
- if (!validTypeIDValue) return;
848
699
  const typeNames = [...possibleTypes.map(type => type.typeName), typeName];
849
700
  const checkResults = await Promise.all(typeNames.map(name => this._checkCacheEntry(_core.DATA_ENTITIES, `${name}::${validTypeIDValue}`, options, context)));
850
701
  const validResults = checkResults.filter(result => !!result);
@@ -866,76 +717,228 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
866
717
  };
867
718
  }
868
719
 
869
- if (validResult) {
720
+ return validResult || {};
721
+ }
722
+
723
+ async _retrieveCachedParentNodeData({
724
+ entityData: ancestorEntityData,
725
+ requestFieldPathData: ancestorRequestFieldPathData
726
+ }, {
727
+ hashedRequestFieldCacheKey,
728
+ propNameOrIndex
729
+ }, fieldTypeInfo, options, context) {
730
+ var _entityData;
731
+
732
+ let entityData = CacheManager._getFieldDataFromAncestor(ancestorEntityData, propNameOrIndex);
733
+
734
+ let requestFieldPathData = CacheManager._getFieldDataFromAncestor(ancestorRequestFieldPathData, propNameOrIndex);
735
+
736
+ let cacheability;
737
+
738
+ if (CacheManager._isNodeRequestFieldPath(fieldTypeInfo)) {
870
739
  const {
871
- cacheability,
740
+ cacheability: entryCacheability,
872
741
  entry
873
- } = validResult;
874
- if (cacheability && !cachedFieldData.cacheability) cachedFieldData.cacheability = cacheability;
875
- if (entry) cachedFieldData.dataEntityData = entry;
742
+ } = await this._retrieveCachedRequestFieldPathData(hashedRequestFieldCacheKey, options, context);
743
+
744
+ if (entry) {
745
+ requestFieldPathData = this._mergeObjects(requestFieldPathData, entry);
746
+ }
747
+
748
+ if (entryCacheability) {
749
+ cacheability = entryCacheability;
750
+ }
876
751
  }
752
+
753
+ const validTypeIDValue = (0, _validTypeIDValue.getValidTypeIDValue)(requestFieldPathData, fieldTypeInfo, this._typeIDKey);
754
+
755
+ if (CacheManager._isNodeEntity(fieldTypeInfo) && validTypeIDValue) {
756
+ var _cacheability;
757
+
758
+ const {
759
+ cacheability: entryCacheability,
760
+ entry
761
+ } = await this._retrieveCachedEntityData(validTypeIDValue, fieldTypeInfo, options, context);
762
+
763
+ if (entry) {
764
+ entityData = this._mergeObjects(entityData, entry);
765
+ }
766
+
767
+ if (entryCacheability && (!cacheability || entryCacheability.metadata.ttl > ((_cacheability = cacheability) === null || _cacheability === void 0 ? void 0 : _cacheability.metadata.ttl))) {
768
+ cacheability = entryCacheability;
769
+ }
770
+ }
771
+
772
+ const data = !(0, _isUndefined2.default)(requestFieldPathData) || !(0, _isUndefined2.default)(entityData) ? this._mergeObjects(requestFieldPathData, entityData) : (_entityData = entityData) !== null && _entityData !== void 0 ? _entityData : requestFieldPathData;
773
+ return {
774
+ cacheability,
775
+ data,
776
+ entityData,
777
+ requestFieldPathData
778
+ };
877
779
  }
878
780
 
879
- _setFieldCacheability(field, ancestorKeysAndPaths, {
880
- cacheMetadata,
881
- data
781
+ async _retrieveCachedRequestFieldPathData(hash, options, context) {
782
+ return this._checkCacheEntry(_core.REQUEST_FIELD_PATHS, hash, options, context) || {};
783
+ }
784
+
785
+ async _retrieveCachedResponseData({
786
+ ast
882
787
  }, options, context) {
883
- const {
884
- requestFieldPath: ancestorRequestFieldPath
885
- } = ancestorKeysAndPaths;
788
+ const cachedResponseData = {
789
+ cacheMetadata: new Map(),
790
+ data: {},
791
+ fieldCount: {
792
+ missing: 0,
793
+ total: 0
794
+ },
795
+ fieldPathChecklist: new Map()
796
+ };
797
+ const queryNode = (0, _helpers.getOperationDefinitions)(ast, context.operation)[0];
798
+ const fieldsAndTypeNames = (0, _helpers.getChildFields)(queryNode);
886
799
 
887
- const keysAndPaths = CacheManager._getFieldKeysAndPaths(field, ancestorKeysAndPaths);
800
+ if (!fieldsAndTypeNames) {
801
+ return cachedResponseData;
802
+ }
888
803
 
889
- const {
890
- requestFieldPath,
891
- responseDataPath
892
- } = keysAndPaths;
893
- const fieldData = (0, _get2.default)(data, responseDataPath, null);
894
- if (!(0, _isObjectLike2.default)(fieldData)) return;
895
- const objectLikeFieldData = fieldData;
804
+ await Promise.all(fieldsAndTypeNames.map(({
805
+ fieldNode
806
+ }) => this._analyzeFieldNode(fieldNode, {
807
+ requestFieldPath: context.operation
808
+ }, cachedResponseData, options, context)));
809
+ cachedResponseData.fieldCount = CacheManager._countFieldPathChecklist(cachedResponseData.fieldPathChecklist);
810
+ return cachedResponseData;
811
+ }
896
812
 
897
- this._setFieldTypeCacheDirective(cacheMetadata, {
898
- ancestorRequestFieldPath,
899
- requestFieldPath
900
- }, context);
813
+ async _setCacheEntry(cacheType, hash, value, cachemapOptions, _options, _context) {
814
+ try {
815
+ await this._cache.set(`${cacheType}::${hash}`, (0, _cloneDeep2.default)(value), cachemapOptions);
816
+ } catch (error) {}
817
+ }
901
818
 
902
- (0, _helpers.iterateChildFields)(field, objectLikeFieldData, (childField, _typeName, childIndex) => {
903
- this._setFieldCacheability(childField, {
904
- index: childIndex,
905
- requestFieldPath,
906
- responseDataPath
907
- }, {
908
- cacheMetadata,
909
- data
910
- }, options, context);
911
- });
819
+ async _setEntityAndRequestFieldPathCacheEntries(requestData, responseData, options, context) {
820
+ const operationNode = (0, _helpers.getOperationDefinitions)(requestData.ast, context.operation)[0];
821
+ const fieldsAndTypeNames = (0, _helpers.getChildFields)(operationNode);
822
+
823
+ if (!fieldsAndTypeNames) {
824
+ return;
825
+ }
826
+
827
+ await Promise.all(fieldsAndTypeNames.map(({
828
+ fieldNode
829
+ }) => {
830
+ return this._parseEntityAndRequestFieldPathCacheEntryData(fieldNode, {
831
+ requestFieldPath: context.operation
832
+ }, responseData, options, context);
833
+ }));
912
834
  }
913
835
 
914
- async _setFieldDataEntityAndRequestFieldPathCacheEntry(field, keysAndPaths, {
836
+ async _setEntityAndRequestFieldPathCacheEntry(field, keysAndPaths, {
915
837
  cacheMetadata,
916
- dataEntityData,
838
+ entityData,
917
839
  requestFieldPathData
918
840
  }, options, context) {
919
841
  const {
920
- requestFieldPath
842
+ requestFieldPath,
843
+ responseDataPath
921
844
  } = keysAndPaths;
845
+ const fieldData = (0, _get2.default)(entityData, responseDataPath);
922
846
  const fieldTypeInfo = context.fieldTypeMap.get(requestFieldPath);
923
847
  const cacheability = cacheMetadata.get(requestFieldPath);
924
- if (!fieldTypeInfo || !cacheability) return;
848
+
849
+ if ((0, _isUndefined2.default)(fieldData) || !fieldTypeInfo || !cacheability) {
850
+ return;
851
+ }
852
+
925
853
  const promises = [];
926
854
  promises.push(this._setRequestFieldPathCacheEntry(field, keysAndPaths, {
927
855
  cacheability,
928
856
  data: requestFieldPathData,
929
857
  fieldTypeInfo
930
858
  }, options, context));
931
- promises.push(this._setDataEntityCacheEntry(keysAndPaths, {
932
- cacheability,
933
- data: dataEntityData,
934
- fieldTypeInfo
935
- }, options, context));
859
+
860
+ const isEntity = this._isFieldEntity(fieldData, fieldTypeInfo);
861
+
862
+ if (!isEntity && fieldTypeInfo.hasArguments) {
863
+ (0, _unset2.default)(entityData, responseDataPath);
864
+ }
865
+
866
+ if (isEntity) {
867
+ promises.push(this._setEntityCacheEntry(keysAndPaths, {
868
+ cacheability,
869
+ data: entityData,
870
+ fieldTypeInfo
871
+ }, options, context));
872
+ }
873
+
936
874
  await Promise.all(promises);
937
875
  }
938
876
 
877
+ async _setEntityCacheEntry({
878
+ responseDataPath
879
+ }, {
880
+ cacheability,
881
+ data,
882
+ fieldTypeInfo
883
+ }, options, context) {
884
+ let fieldData = (0, _get2.default)(data, responseDataPath);
885
+ const fieldTypeName = fieldTypeInfo.isEntity ? fieldTypeInfo.typeName : fieldData.__typename;
886
+ const entityDataKey = `${fieldTypeName}::${fieldData[this._typeIDKey]}`;
887
+ const result = await this._checkCacheEntry(_core.DATA_ENTITIES, entityDataKey, options, context);
888
+
889
+ if (result) {
890
+ fieldData = this._mergeObjects(result.entry, fieldData);
891
+ }
892
+
893
+ await this._setCacheEntry(_core.DATA_ENTITIES, entityDataKey, fieldData, {
894
+ cacheHeaders: {
895
+ cacheControl: cacheability.printCacheControl()
896
+ },
897
+ tag: options.tag
898
+ }, options, context);
899
+ (0, _set2.default)(data, responseDataPath, {
900
+ __cacheKey: `${_core.DATA_ENTITIES}::${entityDataKey}`
901
+ });
902
+ }
903
+
904
+ _setFieldCacheability(field, ancestorKeysAndPaths, {
905
+ cacheMetadata,
906
+ data
907
+ }, options, context) {
908
+ const {
909
+ requestFieldPath: ancestorRequestFieldPath
910
+ } = ancestorKeysAndPaths;
911
+ const keysAndPaths = (0, _buildKeysAndPaths.buildFieldKeysAndPaths)(field, ancestorKeysAndPaths, context);
912
+ const {
913
+ requestFieldPath,
914
+ responseDataPath
915
+ } = keysAndPaths;
916
+ const fieldData = (0, _get2.default)(data, responseDataPath);
917
+ const fieldTypeInfo = context.fieldTypeMap.get(requestFieldPath);
918
+
919
+ if (!(0, _isObjectLike2.default)(fieldData) && !(fieldTypeInfo !== null && fieldTypeInfo !== void 0 && fieldTypeInfo.hasDirectives)) {
920
+ return;
921
+ }
922
+
923
+ this._setFieldTypeCacheDirective(cacheMetadata, {
924
+ ancestorRequestFieldPath,
925
+ requestFieldPath
926
+ }, context);
927
+
928
+ if ((0, _isObjectLike2.default)(fieldData)) {
929
+ (0, _helpers.iterateChildFields)(field, fieldData, context.fragmentDefinitions, (childField, _typeName, _fragmentKind, _fragmentName, childIndex) => {
930
+ this._setFieldCacheability(childField, {
931
+ index: childIndex,
932
+ requestFieldPath,
933
+ responseDataPath
934
+ }, {
935
+ cacheMetadata,
936
+ data
937
+ }, options, context);
938
+ });
939
+ }
940
+ }
941
+
939
942
  _setFieldTypeCacheDirective(cacheMetadata, {
940
943
  ancestorRequestFieldPath,
941
944
  requestFieldPath
@@ -943,11 +946,14 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
943
946
  fieldTypeMap,
944
947
  operation
945
948
  }) {
946
- if (cacheMetadata.has(requestFieldPath)) return;
949
+ if (cacheMetadata.has(requestFieldPath)) {
950
+ return;
951
+ }
952
+
947
953
  const fieldTypeInfo = fieldTypeMap.get(requestFieldPath);
948
954
 
949
955
  if (fieldTypeInfo && this._typeCacheDirectives[fieldTypeInfo.typeName]) {
950
- const cacheability = new _cacheability.default({
956
+ const cacheability = new _cacheability2.default({
951
957
  cacheControl: this._typeCacheDirectives[fieldTypeInfo.typeName]
952
958
  });
953
959
 
@@ -980,23 +986,31 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
980
986
  }, options, context);
981
987
  }
982
988
 
983
- async _setRequestFieldPathCacheEntry(field, {
984
- hashedRequestFieldCacheKey,
985
- responseDataPath
986
- }, {
989
+ async _setRequestFieldPathCacheEntry(field, keysAndPaths, {
987
990
  cacheability,
988
991
  data,
989
992
  fieldTypeInfo
990
993
  }, options, context) {
991
- const hasArgsOrDirectives = fieldTypeInfo.hasArguments || fieldTypeInfo.hasDirectives;
992
- let fieldData = (0, _get2.default)(data, responseDataPath, null);
994
+ const {
995
+ hashedRequestFieldCacheKey,
996
+ responseDataPath
997
+ } = keysAndPaths;
998
+ let fieldData = (0, _get2.default)(data, responseDataPath);
993
999
 
994
1000
  const isEntity = this._isFieldEntity(fieldData, fieldTypeInfo);
995
1001
 
1002
+ const hasArgsOrDirectives = fieldTypeInfo.hasArguments || fieldTypeInfo.hasDirectives;
1003
+
996
1004
  if (context.operation === _core.QUERY && (isEntity || hasArgsOrDirectives)) {
1005
+ var _field$selectionSet;
1006
+
1007
+ if ((0, _isPlainObject2.default)(fieldData) && (_field$selectionSet = field.selectionSet) !== null && _field$selectionSet !== void 0 && _field$selectionSet.selections) {
1008
+ fieldData = (0, _filterOutPropsWithArgsOrDirectives.default)(fieldData, field.selectionSet.selections, keysAndPaths, context);
1009
+ }
1010
+
997
1011
  const result = await this._checkCacheEntry(_core.REQUEST_FIELD_PATHS, hashedRequestFieldCacheKey, options, context);
998
1012
 
999
- if (result) {
1013
+ if (result && (0, _isObjectLike2.default)(fieldData)) {
1000
1014
  fieldData = this._mergeObjects(result.entry, fieldData);
1001
1015
  }
1002
1016
 
@@ -1019,19 +1033,6 @@ let CacheManager = (_dec = (0, _debug.logCacheQuery)(), _dec2 = (0, _debug.logCa
1019
1033
  }
1020
1034
  }
1021
1035
 
1022
- async _setRequestFieldPathData(cachedFieldData, hash, options, context) {
1023
- const checkResult = await this._checkCacheEntry(_core.REQUEST_FIELD_PATHS, hash, options, context);
1024
-
1025
- if (checkResult) {
1026
- const {
1027
- cacheability,
1028
- entry
1029
- } = checkResult;
1030
- if (cacheability) cachedFieldData.cacheability = cacheability;
1031
- if (entry) cachedFieldData.requestFieldPathData = entry;
1032
- }
1033
- }
1034
-
1035
1036
  }, ((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));
1036
1037
  exports.CacheManager = CacheManager;
1037
1038