@graphql-box/cache-manager 3.0.0 → 3.1.2

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 (59) 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 +32 -32
  4. package/lib/main/helpers/createFragmentSpreadChecklist.js +5 -2
  5. package/lib/main/helpers/createFragmentSpreadChecklist.js.map +1 -1
  6. package/lib/main/helpers/filterField.js.map +1 -1
  7. package/lib/main/helpers/filterFragmentDefinitions.js +5 -0
  8. package/lib/main/helpers/filterFragmentDefinitions.js.map +1 -1
  9. package/lib/main/helpers/filterFragmentSpreads.js +0 -1
  10. package/lib/main/helpers/filterFragmentSpreads.js.map +1 -1
  11. package/lib/main/helpers/filterIDsAndTypeNames.js.map +1 -1
  12. package/lib/main/helpers/filterInlineFragments.js.map +1 -1
  13. package/lib/main/helpers/filterOperationAndFragmentDefinitions.js +15 -0
  14. package/lib/main/helpers/filterOperationAndFragmentDefinitions.js.map +1 -0
  15. package/lib/main/helpers/filterQuery.js +5 -4
  16. package/lib/main/helpers/filterQuery.js.map +1 -1
  17. package/lib/module/helpers/createFragmentSpreadChecklist.js +5 -2
  18. package/lib/module/helpers/createFragmentSpreadChecklist.js.map +1 -1
  19. package/lib/module/helpers/filterField.js.map +1 -1
  20. package/lib/module/helpers/filterFragmentDefinitions.js +4 -0
  21. package/lib/module/helpers/filterFragmentDefinitions.js.map +1 -1
  22. package/lib/module/helpers/filterFragmentSpreads.js +0 -1
  23. package/lib/module/helpers/filterFragmentSpreads.js.map +1 -1
  24. package/lib/module/helpers/filterIDsAndTypeNames.js.map +1 -1
  25. package/lib/module/helpers/filterInlineFragments.js.map +1 -1
  26. package/lib/module/helpers/filterOperationAndFragmentDefinitions.js +5 -0
  27. package/lib/module/helpers/filterOperationAndFragmentDefinitions.js.map +1 -0
  28. package/lib/module/helpers/filterQuery.js +5 -4
  29. package/lib/module/helpers/filterQuery.js.map +1 -1
  30. package/lib/types/defs/index.d.ts +2 -1
  31. package/lib/types/defs/index.d.ts.map +1 -1
  32. package/lib/types/helpers/createFragmentSpreadChecklist.d.ts +2 -2
  33. package/lib/types/helpers/createFragmentSpreadChecklist.d.ts.map +1 -1
  34. package/lib/types/helpers/filterField.d.ts +2 -2
  35. package/lib/types/helpers/filterField.d.ts.map +1 -1
  36. package/lib/types/helpers/filterFragmentDefinitions.d.ts +1 -5
  37. package/lib/types/helpers/filterFragmentDefinitions.d.ts.map +1 -1
  38. package/lib/types/helpers/filterFragmentSpreads.d.ts +2 -2
  39. package/lib/types/helpers/filterFragmentSpreads.d.ts.map +1 -1
  40. package/lib/types/helpers/filterIDsAndTypeNames.d.ts +2 -2
  41. package/lib/types/helpers/filterIDsAndTypeNames.d.ts.map +1 -1
  42. package/lib/types/helpers/filterInlineFragments.d.ts +2 -2
  43. package/lib/types/helpers/filterInlineFragments.d.ts.map +1 -1
  44. package/lib/types/helpers/filterOperationAndFragmentDefinitions.d.ts +4 -0
  45. package/lib/types/helpers/filterOperationAndFragmentDefinitions.d.ts.map +1 -0
  46. package/lib/types/helpers/filterQuery.d.ts +1 -1
  47. package/lib/types/helpers/filterQuery.d.ts.map +1 -1
  48. package/package.json +3 -3
  49. package/src/__snapshots__/index.test.ts.snap +25475 -0
  50. package/src/defs/index.ts +2 -1
  51. package/src/helpers/createFragmentSpreadChecklist.ts +3 -3
  52. package/src/helpers/filterField.ts +2 -2
  53. package/src/helpers/filterFragmentDefinitions.ts +4 -0
  54. package/src/helpers/filterFragmentSpreads.ts +2 -3
  55. package/src/helpers/filterIDsAndTypeNames.ts +5 -2
  56. package/src/helpers/filterInlineFragments.ts +5 -2
  57. package/src/helpers/filterOperationAndFragmentDefinitions.ts +10 -0
  58. package/src/helpers/filterQuery.ts +3 -2
  59. package/src/index.test.ts +52 -0
package/src/defs/index.ts CHANGED
@@ -1,4 +1,5 @@
1
- import Cachemap, { CacheHeaders, Metadata } from "@cachemap/core";
1
+ import Cachemap, { CacheHeaders } from "@cachemap/core";
2
+ import { Metadata } from "@cachemap/types";
2
3
  import {
3
4
  CacheMetadata,
4
5
  CacheTypes,
@@ -1,5 +1,5 @@
1
+ import { RequestData } from "@graphql-box/core";
1
2
  import { getFragmentDefinitions } from "@graphql-box/helpers";
2
- import { DocumentNode } from "graphql";
3
3
  import { keys } from "lodash";
4
4
 
5
5
  export type FragmentSpreadCheckist = {
@@ -10,8 +10,8 @@ export type FragmentSpreadCheckist = {
10
10
  };
11
11
  };
12
12
 
13
- export default (ast: DocumentNode) =>
13
+ export default ({ ast, request }: RequestData) =>
14
14
  keys(getFragmentDefinitions(ast) ?? {}).reduce((acc: FragmentSpreadCheckist, name) => {
15
- acc[name] = { deleted: 0, paths: [], total: 0 };
15
+ acc[name] = { deleted: 0, paths: [], total: (request.match(new RegExp(`\\.\\.\\.${name}`, "g")) || []).length };
16
16
  return acc;
17
17
  }, {});
@@ -1,6 +1,6 @@
1
1
  import { TYPE_NAME_KEY } from "@graphql-box/core";
2
2
  import { FRAGMENT_SPREAD, deleteChildFields, getChildFields, getName, hasChildFields } from "@graphql-box/helpers";
3
- import { FieldNode, FragmentDefinitionNode } from "graphql";
3
+ import { FieldNode, FragmentDefinitionNode, OperationDefinitionNode } from "graphql";
4
4
  import { CacheManagerContext, FieldPathChecklist, FragmentSpreadFieldCounter } from "../defs";
5
5
  import { buildFieldKeysAndPaths } from "./buildKeysAndPaths";
6
6
  import checkFieldPathChecklist from "./checkFieldPathChecklist";
@@ -10,7 +10,7 @@ import filterIDsAndTypeNames from "./filterIDsAndTypeNames";
10
10
  import filterInlineFragments from "./filterInlineFragments";
11
11
 
12
12
  const filterField = (
13
- field: FieldNode | FragmentDefinitionNode,
13
+ field: FieldNode | FragmentDefinitionNode | OperationDefinitionNode,
14
14
  fieldPathChecklist: FieldPathChecklist,
15
15
  fragmentSpreadChecklist: FragmentSpreadCheckist,
16
16
  ancestorRequestFieldPath: string,
@@ -34,6 +34,10 @@ export default (
34
34
  return deleted > 0 && deleted === total ? [...names, key] : names;
35
35
  }, []);
36
36
 
37
+ if (!definitionsToDelete.length) {
38
+ return ast;
39
+ }
40
+
37
41
  return deleteFragmentDefinitions(ast, {
38
42
  include: definitionsToDelete,
39
43
  });
@@ -1,11 +1,11 @@
1
1
  import { deleteFragmentSpreads } from "@graphql-box/helpers";
2
- import { FieldNode, FragmentDefinitionNode } from "graphql";
2
+ import { FieldNode, FragmentDefinitionNode, OperationDefinitionNode } from "graphql";
3
3
  import { isEmpty, keys } from "lodash";
4
4
  import { FragmentSpreadFieldCounter } from "../defs";
5
5
  import { FragmentSpreadCheckist } from "./createFragmentSpreadChecklist";
6
6
 
7
7
  export default (
8
- field: FieldNode | FragmentDefinitionNode,
8
+ field: FieldNode | FragmentDefinitionNode | OperationDefinitionNode,
9
9
  fragmentSpreadFieldCounter: FragmentSpreadFieldCounter,
10
10
  fragmentSpreadChecklist: FragmentSpreadCheckist,
11
11
  ancestorRequestFieldPath: string,
@@ -15,7 +15,6 @@ export default (
15
15
  }
16
16
 
17
17
  keys(fragmentSpreadFieldCounter).forEach(key => {
18
- fragmentSpreadChecklist[key].total += 1;
19
18
  fragmentSpreadChecklist[key].paths.push(ancestorRequestFieldPath);
20
19
 
21
20
  const { hasData, total } = fragmentSpreadFieldCounter[key];
@@ -1,9 +1,12 @@
1
1
  import { TYPE_NAME_KEY } from "@graphql-box/core";
2
2
  import { deleteChildFields, getChildFields, getName } from "@graphql-box/helpers";
3
- import { FieldNode, FragmentDefinitionNode } from "graphql";
3
+ import { FieldNode, FragmentDefinitionNode, OperationDefinitionNode } from "graphql";
4
4
  import { CacheManagerContext } from "../defs";
5
5
 
6
- export default (field: FieldNode | FragmentDefinitionNode, { fragmentDefinitions, typeIDKey }: CacheManagerContext) => {
6
+ export default (
7
+ field: FieldNode | FragmentDefinitionNode | OperationDefinitionNode,
8
+ { fragmentDefinitions, typeIDKey }: CacheManagerContext,
9
+ ) => {
7
10
  const fieldsAndTypeNames = getChildFields(field, { fragmentDefinitions });
8
11
 
9
12
  if (!fieldsAndTypeNames || fieldsAndTypeNames.length > 3) {
@@ -1,8 +1,11 @@
1
1
  import { deleteInlineFragments, getChildFields, getInlineFragments, getName } from "@graphql-box/helpers";
2
- import { FieldNode, FragmentDefinitionNode } from "graphql";
2
+ import { FieldNode, FragmentDefinitionNode, OperationDefinitionNode } from "graphql";
3
3
  import { CacheManagerContext } from "..";
4
4
 
5
- export default (field: FieldNode | FragmentDefinitionNode, { fragmentDefinitions, typeIDKey }: CacheManagerContext) => {
5
+ export default (
6
+ field: FieldNode | FragmentDefinitionNode | OperationDefinitionNode,
7
+ { fragmentDefinitions, typeIDKey }: CacheManagerContext,
8
+ ) => {
6
9
  const inlineFragments = getInlineFragments(field);
7
10
  let filtered = false;
8
11
 
@@ -0,0 +1,10 @@
1
+ import { FRAGMENT_DEFINITION, OPERATION_DEFINITION, isKind } from "@graphql-box/helpers";
2
+ import { DocumentNode, FragmentDefinitionNode, OperationDefinitionNode } from "graphql";
3
+
4
+ export default (ast: DocumentNode) => {
5
+ return ast.definitions.filter(
6
+ definition =>
7
+ isKind<OperationDefinitionNode>(definition, OPERATION_DEFINITION) ||
8
+ isKind<FragmentDefinitionNode>(definition, FRAGMENT_DEFINITION),
9
+ ) as (OperationDefinitionNode | FragmentDefinitionNode)[];
10
+ };
@@ -6,7 +6,8 @@ import createFragmentSpreadChecklist from "./createFragmentSpreadChecklist";
6
6
  import filterField from "./filterField";
7
7
  import filterFragmentDefinitions from "./filterFragmentDefinitions";
8
8
 
9
- export default ({ ast }: RequestData, { fieldPathChecklist }: CachedResponseData, context: CacheManagerContext) => {
9
+ export default (requestData: RequestData, { fieldPathChecklist }: CachedResponseData, context: CacheManagerContext) => {
10
+ const { ast } = requestData;
10
11
  const queryNode = getOperationDefinitions(ast, context.operation)[0];
11
12
  const { fragmentDefinitions } = context;
12
13
  const fieldsAndTypeNames = getChildFields(queryNode, { fragmentDefinitions });
@@ -15,7 +16,7 @@ export default ({ ast }: RequestData, { fieldPathChecklist }: CachedResponseData
15
16
  return ast;
16
17
  }
17
18
 
18
- const fragmentSpreadChecklist = createFragmentSpreadChecklist(ast);
19
+ const fragmentSpreadChecklist = createFragmentSpreadChecklist(requestData);
19
20
 
20
21
  for (let i = fieldsAndTypeNames.length - 1; i >= 0; i -= 1) {
21
22
  const { fieldNode } = fieldsAndTypeNames[i];
package/src/index.test.ts CHANGED
@@ -1312,6 +1312,58 @@ describe("@graphql-box/cache-manager >>", () => {
1312
1312
  });
1313
1313
  });
1314
1314
 
1315
+ describe("fragment spreads >> within fragment spreads >>", () => {
1316
+ beforeAll(async () => {
1317
+ analyzeQueryResult = undefined;
1318
+ // @ts-ignore
1319
+ jest.spyOn(CacheManager, "_isValid").mockReturnValue(true);
1320
+
1321
+ cacheManager = new CacheManager({
1322
+ cache: new Cachemap({
1323
+ name: "cachemap",
1324
+ store: map(),
1325
+ type: "someType",
1326
+ }),
1327
+ cascadeCacheControl: true,
1328
+ typeIDKey: DEFAULT_TYPE_ID_KEY,
1329
+ });
1330
+
1331
+ const requestData = getRequestData(parsedRequests.getSearchResultsQuery);
1332
+
1333
+ await cacheManager.cacheQuery(
1334
+ requestData,
1335
+ requestData,
1336
+ responses.getSearchResultsQuery,
1337
+ { awaitDataCaching: true },
1338
+ getRequestContext({ fieldTypeMap: requestFieldTypeMaps.getSearchResultsQuery, hasDeferOrStream: true }),
1339
+ );
1340
+
1341
+ analyzeQueryResult = await cacheManager.analyzeQuery(
1342
+ getRequestData(parsedRequests.getMoviePreviewQuery),
1343
+ { awaitDataCaching: true },
1344
+ getRequestContext({ fieldTypeMap: requestFieldTypeMaps.getMoviePreviewQuery, hasDeferOrStream: true }),
1345
+ );
1346
+ });
1347
+
1348
+ it("correct request data", () => {
1349
+ const { ast, ...otherProps } = analyzeQueryResult?.updated as RequestData;
1350
+ expect(otherProps).toMatchSnapshot();
1351
+ });
1352
+
1353
+ it("no response data", () => {
1354
+ expect(analyzeQueryResult?.response).toBeUndefined();
1355
+ });
1356
+
1357
+ it("correct cache data", async () => {
1358
+ expect(await cacheManager.cache.export()).toMatchSnapshot();
1359
+ });
1360
+
1361
+ it("correct partial data", () => {
1362
+ // @ts-ignore
1363
+ expect(cacheManager._partialQueryResponses).toMatchSnapshot();
1364
+ });
1365
+ });
1366
+
1315
1367
  describe("defer >>", () => {
1316
1368
  beforeAll(async () => {
1317
1369
  analyzeQueryResult = undefined;