@eddeee888/gcg-typescript-resolver-files 0.2.1 → 0.3.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 (52) hide show
  1. package/README.md +2 -0
  2. package/package.json +1 -1
  3. package/src/addVirtualTypesFileToTsMorphProject/addVirtualTypesFileToTsMorphProject.d.ts +8 -0
  4. package/src/addVirtualTypesFileToTsMorphProject/addVirtualTypesFileToTsMorphProject.js +35 -0
  5. package/src/addVirtualTypesFileToTsMorphProject/addVirtualTypesFileToTsMorphProject.js.map +1 -0
  6. package/src/addVirtualTypesFileToTsMorphProject/index.d.ts +1 -0
  7. package/src/addVirtualTypesFileToTsMorphProject/index.js +5 -0
  8. package/src/addVirtualTypesFileToTsMorphProject/index.js.map +1 -0
  9. package/src/generateResolverFiles/ensureObjectTypeResolversAreGenerated.d.ts +6 -0
  10. package/src/generateResolverFiles/ensureObjectTypeResolversAreGenerated.js +80 -0
  11. package/src/generateResolverFiles/ensureObjectTypeResolversAreGenerated.js.map +1 -0
  12. package/src/generateResolverFiles/generateResolverFiles.js +3 -3
  13. package/src/generateResolverFiles/generateResolverFiles.js.map +1 -1
  14. package/src/generateResolverFiles/getVariableStatementWithExpectedIdentifier.d.ts +6 -0
  15. package/src/generateResolverFiles/getVariableStatementWithExpectedIdentifier.js +27 -0
  16. package/src/generateResolverFiles/getVariableStatementWithExpectedIdentifier.js.map +1 -0
  17. package/src/generateResolverFiles/handleGraphQLObjectType.js +5 -2
  18. package/src/generateResolverFiles/handleGraphQLObjectType.js.map +1 -1
  19. package/src/generateResolverFiles/postProcessFiles.d.ts +8 -0
  20. package/src/generateResolverFiles/postProcessFiles.js +72 -0
  21. package/src/generateResolverFiles/postProcessFiles.js.map +1 -0
  22. package/src/generateResolverFiles/types.d.ts +9 -0
  23. package/src/getGraphQLObjectTypeResolversToGenerate/getGraphQLObjectTypeResolversToGenerate.d.ts +11 -0
  24. package/src/getGraphQLObjectTypeResolversToGenerate/getGraphQLObjectTypeResolversToGenerate.js +70 -0
  25. package/src/getGraphQLObjectTypeResolversToGenerate/getGraphQLObjectTypeResolversToGenerate.js.map +1 -0
  26. package/src/getGraphQLObjectTypeResolversToGenerate/index.d.ts +1 -0
  27. package/src/getGraphQLObjectTypeResolversToGenerate/index.js +5 -0
  28. package/src/getGraphQLObjectTypeResolversToGenerate/index.js.map +1 -0
  29. package/src/getPluginsConfig/getPluginsConfig.d.ts +7 -1
  30. package/src/getPluginsConfig/getPluginsConfig.js +14 -6
  31. package/src/getPluginsConfig/getPluginsConfig.js.map +1 -1
  32. package/src/parseTypeMappers/collectTypeMappersFromSourceFile.d.ts +3 -2
  33. package/src/parseTypeMappers/collectTypeMappersFromSourceFile.js +38 -8
  34. package/src/parseTypeMappers/collectTypeMappersFromSourceFile.js.map +1 -1
  35. package/src/parseTypeMappers/parseTypeMappers.d.ts +6 -1
  36. package/src/parseTypeMappers/parseTypeMappers.js +9 -7
  37. package/src/parseTypeMappers/parseTypeMappers.js.map +1 -1
  38. package/src/preset.d.ts +2 -1
  39. package/src/preset.js +66 -28
  40. package/src/preset.js.map +1 -1
  41. package/src/utils/getNodePropertyMap.d.ts +9 -0
  42. package/src/utils/getNodePropertyMap.js +49 -0
  43. package/src/utils/getNodePropertyMap.js.map +1 -0
  44. package/src/utils/index.d.ts +1 -0
  45. package/src/utils/index.js +1 -0
  46. package/src/utils/index.js.map +1 -1
  47. package/src/validatePresetConfig/validatePresetConfig.d.ts +6 -1
  48. package/src/validatePresetConfig/validatePresetConfig.js +29 -10
  49. package/src/validatePresetConfig/validatePresetConfig.js.map +1 -1
  50. package/src/generateResolverFiles/fixExistingResolvers.d.ts +0 -2
  51. package/src/generateResolverFiles/fixExistingResolvers.js +0 -62
  52. package/src/generateResolverFiles/fixExistingResolvers.js.map +0 -1
package/README.md CHANGED
@@ -96,3 +96,5 @@ Running codegen will generate the following files:
96
96
  | `typesPluginsConfig` | `(@graphql-codegen/typescript).TypeScriptPluginConfig & (@graphql-codegen/typescript-resolvers).TypeScriptResolversPluginConfig` | Takes [typescript config](https://www.the-guild.dev/graphql/codegen/plugins/typescript/typescript) and [typescript-resolvers config](https://www.the-guild.dev/graphql/codegen/plugins/typescript/typescript-resolvers) to override the defaults |
97
97
  | `mappersFileExtension` | `string` | (Default: `.mappers.ts`) The files with this extension provides mappers interfaces and types for the schema files in the same dir. |
98
98
  | `mappersSuffix` | `string` | (Default: `Mapper`) Exported interfaces and types with this suffix from `mappersFile` in each module are put into the mappers object of [@graphql-codegen/typescript-resolvers](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-resolvers) . |
99
+ | `tsConfigFilePath` | `string` | (Default: `./tsconfig.json`) Project's TypeScript config, relative from project root. This helps type analysis such as resolving custom module paths. |
100
+ | `fixObjectTypeResolvers` | `smart` or `disabled` | (Default: `smart`) (Experimental) Statically compares object type's mapper types' field against schema types' fields, creating resolvers if required |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eddeee888/gcg-typescript-resolver-files",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/eddeee888/graphql-code-generator-plugins.git",
@@ -0,0 +1,8 @@
1
+ import type { GraphQLSchema } from 'graphql';
2
+ import { type SourceFile, Project } from 'ts-morph';
3
+ export declare const addVirtualTypesFileToTsMorphProject: ({ tsMorphProject, schemaAst, resolverTypesPath, resolverTypesConfig, }: {
4
+ tsMorphProject: Project;
5
+ schemaAst: GraphQLSchema;
6
+ resolverTypesPath: string;
7
+ resolverTypesConfig: Record<string, unknown>;
8
+ }) => Promise<SourceFile>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addVirtualTypesFileToTsMorphProject = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const typeScriptPlugin = require("@graphql-codegen/typescript");
6
+ const typeScriptResolversPlugin = require("@graphql-codegen/typescript-resolvers");
7
+ const addVirtualTypesFileToTsMorphProject = ({ tsMorphProject, schemaAst, resolverTypesPath, resolverTypesConfig, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
8
+ const typesFile = yield generateVirtualTypesFile({
9
+ schemaAst,
10
+ resolverTypesPath,
11
+ resolverTypesConfig,
12
+ });
13
+ const typesSourceFile = tsMorphProject.createSourceFile(typesFile.filePath, typesFile.content, { overwrite: true });
14
+ return typesSourceFile;
15
+ });
16
+ exports.addVirtualTypesFileToTsMorphProject = addVirtualTypesFileToTsMorphProject;
17
+ /**
18
+ * getVirtualTypesFile generates a virtual types.generated.ts file
19
+ * This is used to statically detect and compare types in the parse and post-process steps
20
+ */
21
+ const generateVirtualTypesFile = ({ schemaAst, resolverTypesPath, resolverTypesConfig, }) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
22
+ var _a, _b;
23
+ const [typescriptResult, typescriptResolversResult] = yield Promise.all([
24
+ typeScriptPlugin.plugin(schemaAst, [], resolverTypesConfig),
25
+ typeScriptResolversPlugin.plugin(schemaAst, [], resolverTypesConfig),
26
+ ]);
27
+ return {
28
+ filePath: resolverTypesPath,
29
+ content: `
30
+ ${(_a = typescriptResult.prepend) === null || _a === void 0 ? void 0 : _a.join('\n')}
31
+ ${(_b = typescriptResolversResult.prepend) === null || _b === void 0 ? void 0 : _b.join('\n')}
32
+ ${typescriptResult.content}${typescriptResolversResult.content}`,
33
+ };
34
+ });
35
+ //# sourceMappingURL=addVirtualTypesFileToTsMorphProject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addVirtualTypesFileToTsMorphProject.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/addVirtualTypesFileToTsMorphProject/addVirtualTypesFileToTsMorphProject.ts"],"names":[],"mappings":";;;;AAAA,gEAAgE;AAChE,mFAAmF;AAI5E,MAAM,mCAAmC,GAAG,CAAO,EACxD,cAAc,EACd,SAAS,EACT,iBAAiB,EACjB,mBAAmB,GAMpB,EAAuB,EAAE;IACxB,MAAM,SAAS,GAAG,MAAM,wBAAwB,CAAC;QAC/C,SAAS;QACT,iBAAiB;QACjB,mBAAmB;KACpB,CAAC,CAAC;IAEH,MAAM,eAAe,GAAG,cAAc,CAAC,gBAAgB,CACrD,SAAS,CAAC,QAAQ,EAClB,SAAS,CAAC,OAAO,EACjB,EAAE,SAAS,EAAE,IAAI,EAAE,CACpB,CAAC;IAEF,OAAO,eAAe,CAAC;AACzB,CAAC,CAAA,CAAC;AAxBW,QAAA,mCAAmC,uCAwB9C;AAEF;;;GAGG;AACH,MAAM,wBAAwB,GAAG,CAAO,EACtC,SAAS,EACT,iBAAiB,EACjB,mBAAmB,GAKpB,EAGE,EAAE;;IACH,MAAM,CAAC,gBAAgB,EAAE,yBAAyB,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QACtE,gBAAgB,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,EAAE,mBAAmB,CAAC;QAC3D,yBAAyB,CAAC,MAAM,CAAC,SAAS,EAAE,EAAE,EAAE,mBAAmB,CAAC;KACrE,CAAC,CAAC;IAEH,OAAO;QACL,QAAQ,EAAE,iBAAiB;QAC3B,OAAO,EAAE;MACP,MAAA,gBAAgB,CAAC,OAAO,0CAAE,IAAI,CAAC,IAAI,CAAC;MACpC,MAAA,yBAAyB,CAAC,OAAO,0CAAE,IAAI,CAAC,IAAI,CAAC;MAC7C,gBAAgB,CAAC,OAAO,GAAG,yBAAyB,CAAC,OAAO,EAAE;KACjE,CAAC;AACJ,CAAC,CAAA,CAAC"}
@@ -0,0 +1 @@
1
+ export * from './addVirtualTypesFileToTsMorphProject';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./addVirtualTypesFileToTsMorphProject"), exports);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/addVirtualTypesFileToTsMorphProject/index.ts"],"names":[],"mappings":";;;AAAA,gFAAsD"}
@@ -0,0 +1,6 @@
1
+ import { type SourceFile } from 'ts-morph';
2
+ import type { ObjectTypeFile } from './types';
3
+ /**
4
+ * Ensure objectTypeResolver files have all the resolvers due to mismatched types
5
+ */
6
+ export declare const ensureObjectTypeResolversAreGenerated: (sourceFile: SourceFile, resolverFile: ObjectTypeFile) => void;
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ensureObjectTypeResolversAreGenerated = void 0;
4
+ const ts_morph_1 = require("ts-morph");
5
+ const getVariableStatementWithExpectedIdentifier_1 = require("./getVariableStatementWithExpectedIdentifier");
6
+ /**
7
+ * Ensure objectTypeResolver files have all the resolvers due to mismatched types
8
+ */
9
+ const ensureObjectTypeResolversAreGenerated = (sourceFile, resolverFile) => {
10
+ const resolversToGenerate = resolverFile.meta.resolversToGenerate || {};
11
+ if (!Object.keys(resolversToGenerate).length) {
12
+ return;
13
+ }
14
+ const { variableStatement } = (0, getVariableStatementWithExpectedIdentifier_1.getVariableStatementWithExpectedIdentifier)(sourceFile, resolverFile);
15
+ if (!variableStatement) {
16
+ throw new Error('Missing variableStatement in ensureObjectTypeResolversAreGenerated.');
17
+ }
18
+ /**
19
+ * FIXME: TS API does not expose `.isAssignable(Type, Type)` so we cannot use it to compare a field's type in schema type vs mapper type
20
+ *
21
+ * In this workaround, we try to put the type together and use TS's diagnostic tool to see if there's error:
22
+ * 1. If there's error, it means the mapper field cannot be safely mapped to schema field using GraphQL's default behaviour i.e. there will be runtime error
23
+ * In this case, we leave the resolver function as-is. There should be an error message from the parsing phrase explaining the types cannot be mapped.
24
+ * 2. If there's no error, it means we can safely remove the resolver and let GraphQL handle the mapping.
25
+ *
26
+ * Note: this only looks at fields that's not declared by the user. If it's already declared, we assume TS will report naturally if there's an error.
27
+ */
28
+ // 1. Check to see which generated to-be-generated resolvers are implemented
29
+ const resolversData = Object.assign({}, resolversToGenerate);
30
+ variableStatement
31
+ .getDescendantsOfKind(ts_morph_1.SyntaxKind.PropertyAssignment)
32
+ .forEach((propertyAssignment) => {
33
+ const resolverName = propertyAssignment.getName();
34
+ if (resolversData[resolverName]) {
35
+ resolversData[resolverName].implemented = true;
36
+ }
37
+ });
38
+ variableStatement
39
+ .getDescendantsOfKind(ts_morph_1.SyntaxKind.MethodDeclaration)
40
+ .forEach((methodDeclaration) => {
41
+ const resolverName = methodDeclaration.getName();
42
+ if (resolversData[resolverName]) {
43
+ resolversData[resolverName].implemented = true;
44
+ }
45
+ });
46
+ // 2. Add missing resolver properties if they haven't been implemented
47
+ const addedPropertyAssignmentNodes = [];
48
+ Object.values(resolversData).forEach(({ resolverName, resolverDeclaration, implemented }) => {
49
+ if (!implemented) {
50
+ const addedNode = variableStatement
51
+ .getDescendantsOfKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression)[0]
52
+ .addPropertyAssignment({
53
+ name: resolverName,
54
+ initializer: resolverDeclaration,
55
+ });
56
+ addedPropertyAssignmentNodes[addedNode.getStartLineNumber()] = {
57
+ node: addedNode,
58
+ __toBeRemoved: true,
59
+ };
60
+ }
61
+ });
62
+ // 3. Check to see if there's error at the newly added properties:
63
+ // a. If there's error, leave it as user needs to manually map mapper type to schema type
64
+ // b. If there's no error, remove the node because GraphQL default mapping behaviour should work
65
+ sourceFile.getPreEmitDiagnostics().forEach((d) => {
66
+ const lineNumberWithError = d.getLineNumber();
67
+ // If erroring on a recently added line, do not remove as user needs to implement it
68
+ if (lineNumberWithError &&
69
+ addedPropertyAssignmentNodes[lineNumberWithError]) {
70
+ addedPropertyAssignmentNodes[lineNumberWithError].__toBeRemoved = false;
71
+ }
72
+ });
73
+ Object.values(addedPropertyAssignmentNodes).forEach(({ node, __toBeRemoved }) => {
74
+ if (__toBeRemoved) {
75
+ node.remove();
76
+ }
77
+ });
78
+ };
79
+ exports.ensureObjectTypeResolversAreGenerated = ensureObjectTypeResolversAreGenerated;
80
+ //# sourceMappingURL=ensureObjectTypeResolversAreGenerated.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ensureObjectTypeResolversAreGenerated.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/generateResolverFiles/ensureObjectTypeResolversAreGenerated.ts"],"names":[],"mappings":";;;AAAA,uCAA2E;AAE3E,6GAA0G;AAE1G;;GAEG;AACI,MAAM,qCAAqC,GAAG,CACnD,UAAsB,EACtB,YAA4B,EACtB,EAAE;IACR,MAAM,mBAAmB,GAAG,YAAY,CAAC,IAAI,CAAC,mBAAmB,IAAI,EAAE,CAAC;IACxE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC,MAAM,EAAE;QAC5C,OAAO;KACR;IAED,MAAM,EAAE,iBAAiB,EAAE,GAAG,IAAA,uFAA0C,EACtE,UAAU,EACV,YAAY,CACb,CAAC;IAEF,IAAI,CAAC,iBAAiB,EAAE;QACtB,MAAM,IAAI,KAAK,CACb,qEAAqE,CACtE,CAAC;KACH;IAED;;;;;;;;;OASG;IAEH,4EAA4E;IAC5E,MAAM,aAAa,qBAOV,mBAAmB,CAAE,CAAC;IAE/B,iBAAiB;SACd,oBAAoB,CAAC,qBAAU,CAAC,kBAAkB,CAAC;SACnD,OAAO,CAAC,CAAC,kBAAkB,EAAE,EAAE;QAC9B,MAAM,YAAY,GAAG,kBAAkB,CAAC,OAAO,EAAE,CAAC;QAClD,IAAI,aAAa,CAAC,YAAY,CAAC,EAAE;YAC/B,aAAa,CAAC,YAAY,CAAC,CAAC,WAAW,GAAG,IAAI,CAAC;SAChD;IACH,CAAC,CAAC,CAAC;IAEL,iBAAiB;SACd,oBAAoB,CAAC,qBAAU,CAAC,iBAAiB,CAAC;SAClD,OAAO,CAAC,CAAC,iBAAiB,EAAE,EAAE;QAC7B,MAAM,YAAY,GAAG,iBAAiB,CAAC,OAAO,EAAE,CAAC;QACjD,IAAI,aAAa,CAA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qCAAqC,yCAsGhD"}
@@ -4,7 +4,7 @@ exports.generateResolverFiles = void 0;
4
4
  const graphql_1 = require("graphql");
5
5
  const utils_1 = require("../utils");
6
6
  const addResolverMainFile_1 = require("./addResolverMainFile");
7
- const fixExistingResolvers_1 = require("./fixExistingResolvers");
7
+ const postProcessFiles_1 = require("./postProcessFiles");
8
8
  const handleGraphQLRootObjectTypeField_1 = require("./handleGraphQLRootObjectTypeField");
9
9
  const handleGraphQLObjectType_1 = require("./handleGraphQLObjectType");
10
10
  const handleGraphQLUninionType_1 = require("./handleGraphQLUninionType");
@@ -46,8 +46,8 @@ const generateResolverFiles = (ctx) => {
46
46
  visitor,
47
47
  }, ctx);
48
48
  });
49
- // Check to see which resolver file exists already
50
- (0, fixExistingResolvers_1.fixExistingResolvers)(ctx);
49
+ // Post process generated files (could be existing files or files to be generated)
50
+ (0, postProcessFiles_1.postProcessFiles)(ctx);
51
51
  // Put all resolvers into a barrel file (or main file)
52
52
  (0, addResolverMainFile_1.addResolverMainFile)(ctx);
53
53
  };
@@ -1 +1 @@
1
- {"version":3,"file":"generateResolverFiles.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/generateResolverFiles/generateResolverFiles.ts"],"names":[],"mappings":";;;AAAA,qCAAuC;AACvC,oCAA+D;AAC/D,+DAA4D;AAC5D,iEAA8D;AAC9D,yFAAsF;AACtF,uEAAoE;AACpE,yEAAsE;AACtE,uEAAoE;AACpE,qDAAwE;AAGjE,MAAM,qBAAqB,GAAG,CACnC,GAAiC,EAC3B,EAAE;IACR,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC,OAAO,CACpD,CAAC,CAAC,UAAU,EAAE,SAAS,CAAC,EAAE,EAAE;;QAC1B,IAAI,IAAA,yBAAiB,EAAC,SAAS,CAAC,EAAE;YAChC,OAAO;SACR;QAED,EAAE;QACF,oBAAoB;QACpB,EAAE;QACF,MAAM,OAAO,GAAoC;YAC/C,mBAAmB,EAAE,mEAAgC;YACrD,UAAU,EAAE,iDAAuB;YACnC,SAAS,EAAE,mDAAwB;YACnC,UAAU,EAAE,iDAAuB;SACpC,CAAC;QAEF,IAAI,IAAA,wBAAgB,EAAC,UAAU,CAAC,IAAI,IAAA,sBAAY,EAAC,SAAS,CAAC,EAAE;YAC3D,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC,OAAO,CAC3C,CAAC,CAAC,SAAS,EAAE,SAAS,CAAC,EAAE,EAAE;;gBACzB,OAAA,IAAA,+BAAc,EACZ;oBACE,SAAS;oBACT,YAAY,EAAE,SAAS;oBACvB,mBAAmB,EAAE,UAAU;oBAC/B,QAAQ,EAAE,MAAA,SAAS,CAAC,OAAO,0CAAE,GAAG;oBAChC,OAAO;iBACR,EACD,GAAG,CACJ,CAAA;aAAA,CACJ,CAAC;YACF,OAAO;SACR;QAED,IAAA,+BAAc,EACZ;YACE,SAAS;YACT,YAAY,EAAE,SAAS,CAAC,IAAI;YAC5B,mBAAmB,EAAE,IAAI;YACzB,QAAQ,EAAE,MAAA,SAAS,CAAC,OAAO,0CAAE,GAAG;YAChC,OAAO;SACR,EACD,GAAG,CACJ,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,kDAAkD;IAClD,IAAA,2CAAoB,EAAC,GAAG,CAAC,CAAC;IAE1B,sDAAsD;IACtD,IAAA,yCAAmB,EAAC,GAAG,CAAC,CAAC;AAC3B,CAAC,CAAC;AAtDW,QAAA,qBAAqB,yBAsDhC"}
1
+ {"version":3,"file":"generateResolverFiles.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/generateResolverFiles/generateResolverFiles.ts"],"names":[],"mappings":";;;AAAA,qCAAuC;AACvC,oCAA+D;AAC/D,+DAA4D;AAC5D,yDAAsD;AACtD,yFAAsF;AACtF,uEAAoE;AACpE,yEAAsE;AACtE,uEAAoE;AACpE,qDAAwE;AAGjE,MAAM,qBAAqB,GA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C;AAtDW,QAAA,qBAAqB,yBAsDhC"}
@@ -0,0 +1,6 @@
1
+ import type { SourceFile, VariableStatement } from 'ts-morph';
2
+ import type { ResolverFile } from './types';
3
+ export declare const getVariableStatementWithExpectedIdentifier: (sourceFile: SourceFile, resolverFile: ResolverFile) => {
4
+ variableStatement: VariableStatement | undefined;
5
+ isExported: boolean;
6
+ };
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getVariableStatementWithExpectedIdentifier = void 0;
4
+ const getVariableStatementWithExpectedIdentifier = (sourceFile, resolverFile) => {
5
+ let isExported = false;
6
+ const variableStatementWithExpectedIdentifier = sourceFile.getVariableStatement((statement) => {
7
+ let hasExpectedIdentifier = false;
8
+ statement
9
+ .getDeclarationList()
10
+ .getDeclarations()
11
+ .forEach((declarationNode) => {
12
+ if (declarationNode.getName() === resolverFile.mainImportIdentifier) {
13
+ hasExpectedIdentifier = true;
14
+ if (statement.isExported()) {
15
+ isExported = true;
16
+ }
17
+ }
18
+ });
19
+ return hasExpectedIdentifier;
20
+ });
21
+ return {
22
+ variableStatement: variableStatementWithExpectedIdentifier,
23
+ isExported,
24
+ };
25
+ };
26
+ exports.getVariableStatementWithExpectedIdentifier = getVariableStatementWithExpectedIdentifier;
27
+ //# sourceMappingURL=getVariableStatementWithExpectedIdentifier.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getVariableStatementWithExpectedIdentifier.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/generateResolverFiles/getVariableStatementWithExpectedIdentifier.ts"],"names":[],"mappings":";;;AAGO,MAAM,0CAA0C,GAAG,CACxD,UAAsB,EACtB,YAA0B,EAI1B,EAAE;IACF,IAAI,UAAU,GAAG,KAAK,CAAC;IACvB,MAAM,uCAAuC,GAC3C,UAAU,CAAC,oBAAoB,CAAC,CAAC,SAAS,EAAE,EAAE;QAC5C,IAAI,qBAAqB,GAAG,KAAK,CAAC;QAClC,SAAS;aACN,kBAAkB,EAAE;aACpB,eAAe,EAAE;aACjB,OAAO,CAAC,CAAC,eAAe,EAAE,EAAE;YAC3B,IAAI,eAAe,CAAC,OAAO,EAAE,KAAK,YAAY,CAAC,oBAAoB,EAAE;gBACnE,qBAAqB,GAAG,IAAI,CAAC;gBAC7B,IAAI,SAAS,CAAC,UAAU,EAAE,EAAE;oBAC1B,UAAU,GAAG,IAAI,CAAC;iBACnB;aACF;QACH,CAAC,CAAC,CAAC;QACL,OAAO,qBAAqB,CAAC;IAC/B,CAAC,CAAC,CAAC;IAEL,OAAO;QACL,iBAAiB,EAAE,uCAAuC;QAC1D,UAAU;KACX,CAAC;AACJ,CAAC,CAAC;AA7BW,QAAA,0CAA0C,8CA6BrD"}
@@ -2,8 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.handleGraphQLObjectType = void 0;
4
4
  const utils_1 = require("../utils");
5
- const handleGraphQLObjectType = ({ fieldFilePath, resolverName, normalizedResolverName, resolversTypeMeta }, { result }) => {
6
- const variableStatement = `export const ${resolverName}: ${resolversTypeMeta.typeString} = { /* Implement ${resolverName} resolver logic here */ };`;
5
+ const handleGraphQLObjectType = ({ fieldFilePath, resolverName, normalizedResolverName, resolversTypeMeta }, { result, config: { graphQLObjectTypeResolversToGenerate } }) => {
6
+ const variableStatement = `export const ${resolverName}: ${resolversTypeMeta.typeString} = {
7
+ /* Implement ${resolverName} resolver logic here */
8
+ };`;
7
9
  result.files[fieldFilePath] = {
8
10
  __filetype: 'objectType',
9
11
  content: `
@@ -17,6 +19,7 @@ const handleGraphQLObjectType = ({ fieldFilePath, resolverName, normalizedResolv
17
19
  meta: {
18
20
  normalizedResolverName,
19
21
  variableStatement,
22
+ resolversToGenerate: graphQLObjectTypeResolversToGenerate[resolverName], // Array of all resolvers that may need type checking
20
23
  },
21
24
  };
22
25
  };
@@ -1 +1 @@
1
- {"version":3,"file":"handleGraphQLObjectType.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/generateResolverFiles/handleGraphQLObjectType.ts"],"names":[],"mappings":";;;AACA,oCAA2C;AAEpC,MAAM,uBAAuB,GAAuB,CACzD,EAAE,aAAa,EAAE,YAAY,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,EAC1E,EAAE,MAAM,EAAE,EACV,EAAE;IACF,MAAM,iBAAiB,GAAG,gBAAgB,YAAY,KAAK,iBAAiB,CAAC,UAAU,qBAAqB,YAAY,4BAA4B,CAAC;IAErJ,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,GAAG;QAC5B,UAAU,EAAE,YAAY;QACxB,OAAO,EAAE;MACP,IAAA,uBAAe,EAAC;YAChB,YAAY,EAAE,IAAI;YAClB,MAAM,EAAE,iBAAiB,CAAC,MAAM;YAChC,YAAY,EAAE,CAAC,iBAAiB,CAAC,eAAe,CAAC;SAClD,CAAC;MACA,iBAAiB,EAAE;QACrB,oBAAoB,EAAE,YAAY;QAClC,IAAI,EAAE;YACJ,sBAAsB;YACtB,iBAAiB;SAClB;KACF,CAAC;AACJ,CAAC,CAAC;AArBW,QAAA,uBAAuB,2BAqBlC"}
1
+ {"version":3,"file":"handleGraphQLObjectType.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/generateResolverFiles/handleGraphQLObjectType.ts"],"names":[],"mappings":";;;AACA,oCAA2C;AAEpC,MAAM,uBAAuB,GAAuB,CACzD,EAAE,aAAa,EAAE,YAAY,EAAE,sBAAsB,EAAE,iBAAiB,EAAE,EAC1E,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,oCAAoC,EAAE,EAAE,EAC5D,EAAE;IACF,MAAM,iBAAiB,GAAG,gBAAgB,YAAY,KAAK,iBAAiB,CAAC,UAAU;mBACtE,YAAY;KAC1B,CAAC;IAEJ,MAAM,CAAC,KAAK,CAAC,aAAa,CAAC,GAAG;QAC5B,UAAU,EAAE,YAAY;QACxB,OAAO,EAAE;MACP,IAAA,uBAAe,EAAC;YAChB,YAAY,EAAE,IAAI;YAClB,MAAM,EAAE,iBAAiB,CAAC,MAAM;YAChC,YAAY,EAAE,CAAC,iBAAiB,CAAC,eAAe,CAAC;SAClD,CAAC;MACA,iBAAiB,EAAE;QACrB,oBAAoB,EAAE,YAAY;QAClC,IAAI,EAAE;YACJ,sBAAsB;YACtB,iBAAiB;YACjB,mBAAmB,EAAE,oCAAoC,CAAC,YAAY,CAAC,EAAE,qDAAqD;SAC/H;KACF,CAAC;AACJ,CAAC,CAAC;AAxBW,QAAA,uBAAuB,2BAwBlC"}
@@ -0,0 +1,8 @@
1
+ import type { GenerateResolverFilesContext } from './types';
2
+ /**
3
+ * postProcessFiles does static analysis on existing files OR to-be-generated files
4
+ * e.g.
5
+ * - Make sure correct variables are exported
6
+ * - Make sure object types have field resolvers if mapper type's field cannot be used as schema type's field
7
+ */
8
+ export declare const postProcessFiles: ({ config: { tsMorph: { project }, fixObjectTypeResolvers, }, result, }: GenerateResolverFilesContext) => void;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.postProcessFiles = void 0;
4
+ const path = require("path");
5
+ const getVariableStatementWithExpectedIdentifier_1 = require("./getVariableStatementWithExpectedIdentifier");
6
+ const ensureObjectTypeResolversAreGenerated_1 = require("./ensureObjectTypeResolversAreGenerated");
7
+ /**
8
+ * postProcessFiles does static analysis on existing files OR to-be-generated files
9
+ * e.g.
10
+ * - Make sure correct variables are exported
11
+ * - Make sure object types have field resolvers if mapper type's field cannot be used as schema type's field
12
+ */
13
+ const postProcessFiles = ({ config: { tsMorph: { project }, fixObjectTypeResolvers, }, result, }) => {
14
+ const sourceFilesToProcess = [];
15
+ Object.entries(result.files).forEach(([filePath, file]) => {
16
+ if (file.__filetype === 'file') {
17
+ return;
18
+ }
19
+ const existingSourceFile = project.addSourceFileAtPathIfExists(filePath);
20
+ if (existingSourceFile) {
21
+ sourceFilesToProcess.push({
22
+ sourceFile: existingSourceFile,
23
+ resolverFile: file,
24
+ });
25
+ return;
26
+ }
27
+ // If cannot find existing source files, load files that need post-processing into sourceFilesToProcess
28
+ if (file.__filetype === 'objectType') {
29
+ const virtualSourceFile = project.createSourceFile(filePath, file.content);
30
+ sourceFilesToProcess.push({
31
+ sourceFile: virtualSourceFile,
32
+ resolverFile: file,
33
+ });
34
+ }
35
+ });
36
+ sourceFilesToProcess.forEach(({ sourceFile, resolverFile }) => {
37
+ const normalizedRelativePath = path.relative(process.cwd(), sourceFile.getFilePath());
38
+ // TODO: Check missing import
39
+ // ...
40
+ ensureExportedResolver(sourceFile, resolverFile);
41
+ if (fixObjectTypeResolvers === 'smart' &&
42
+ resolverFile.__filetype === 'objectType') {
43
+ (0, ensureObjectTypeResolversAreGenerated_1.ensureObjectTypeResolversAreGenerated)(sourceFile, resolverFile);
44
+ }
45
+ // Overwrite existing files with fixes
46
+ result.files[normalizedRelativePath] = Object.assign(Object.assign({}, resolverFile), { content: sourceFile.getText() });
47
+ });
48
+ };
49
+ exports.postProcessFiles = postProcessFiles;
50
+ /**
51
+ * Ensure correctly named resolvers are exported
52
+ */
53
+ const ensureExportedResolver = (sourceFile, resolverFile) => {
54
+ const { variableStatement, isExported } = (0, getVariableStatementWithExpectedIdentifier_1.getVariableStatementWithExpectedIdentifier)(sourceFile, resolverFile);
55
+ if (!variableStatement) {
56
+ // Did not find variable statement with expected identifier, add it to the end with a warning
57
+ sourceFile.addStatements('/* WARNING: The following resolver was missing from this file. Make sure it is properly implemented or there could be runtime errors. */');
58
+ sourceFile.addStatements(resolverFile.meta.variableStatement);
59
+ }
60
+ else if (variableStatement && !isExported) {
61
+ // If has identifier but not exported
62
+ // Add export keyword to statement
63
+ const isExpectedIdentifierExported = Boolean(sourceFile
64
+ .getExportedDeclarations()
65
+ .get(resolverFile.mainImportIdentifier));
66
+ if (!isExpectedIdentifierExported) {
67
+ variableStatement.setIsExported(true);
68
+ }
69
+ // else, if identifier's been exported do nothing
70
+ }
71
+ };
72
+ //# sourceMappingURL=postProcessFiles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postProcessFiles.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/generateResolverFiles/postProcessFiles.ts"],"names":[],"mappings":";;;AACA,6BAA6B;AAE7B,6GAA0G;AAC1G,mGAAgG;AAEhG;;;;;GAKG;AACI,MAAM,gBAAgB,GAAG,CAAC,EAC/B,MAAM,EAAE,EACN,OAAO,EAAE,EAAE,OAAO,EAAE,EACpB,sBAAsB,GACvB,EACD,MAAM,GACuB,EAAQ,EAAE;IACvC,MAAM,oBAAoB,GAGpB,EAAE,CAAC;IACT,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,EAAE;QACxD,IAAI,IAAI,CAAC,UAAU,KAAK,MAAM,EAAE;YAC9B,OAAO;SACR;QAED,MAAM,kBAAkB,GAAG,OAAO,CAAC,2BAA2B,CAAC,QAAQ,CAAC,CAAC;QACzE,IAAI,kBAAkB,EAAE;YACtB,oBAAoB,CAAC,IAAI,CAAC;gBACxB,UAAU,EAAE,kBAAkB;gBAC9B,YAAY,EAAE,IAAI;aACnB,CAAC,CAAC;YACH,OAAO;SACR;QAED,uGAAuG;QACvG,IAAI,IAAI,CAAC,UAAU,KAAK,YAAY,EAAE;YACpC,MAAM,iBAAiB,GAAG,OAAO,CAAC,gBAAgB,CAChD,QAAQ,EACR,IAAI,CAAC,OAAO,CACb,CAAC;YACF,oBAAoB,CAAC,IAAI,CAAC;gBACxB,UAAU,EAAE,iBAAiB;gBAC7B,YAAY,EAAE,IAAI;aACnB,CAAC,CAAC;SACJ;IACH,CAAC,CAAC,CAAC;IAEH,oBAAoB,CAAC,OAAO,CAAC,CAAC,EAAE,UAAU,EAAE,YAAY,EAAE,EAAE,EAAE;QAC5D,MAAM,sBAAsB,GAAG,IAAI,CAAC,QAAQ,CAC1C,OAAO,CAAC,GAAG,EAAE,EACb,UAAU,CAAC,WAAW,EAAE,CACzB,CAAC;QAEF,6BAA6B;QAC7B,MAAM;QAEN,sBAAsB,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;QACjD,IACE,sBAAsB,KAAK,OAAO;YAClC,YAAY,CAAC,UAAU,KAAK,YAAY,EACxC;YACA,IAAA,6EAAqC,EAAC,UAAU,EAAE,YAAY,CAAC,CAAC;SACjE;QAED,sCAAsC;QACtC,MAAM,CAAC,KAAK,CAAC,sBAAsB,CAAC,mCAC/B,YAAY,KACf,OAAO,EAAE,UAAU,CAAC,OAAO,EAAE,GAC9B,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AA7DW,QAAA,gBAAgB,oBA6D3B;AAEF;;GAEG;AACH,MAAM,sBAAsB,GAAG,CAC7B,UAAsB,EACtB,YAA0B,EACpB,EAAE;IACR,MAAM,EAAE,iBAAiB,EAAE,UAAU,EAAE,GACrC,IAAA,uFAA0C,EAAC,UAAU,EAAE,YAAY,CAAC,CAAC;IAEvE,IAAI,CAAC,iBAAiB,EAAE;QACtB,6FAA6F;QAC7F,UAAU,CAAC,aAAa,CACtB,0IAA0I,CAC3I,CAAC;QACF,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;KAC/D;SAAM,IAAI,iBAAiB,IAAI,CAAC,UAAU,EAAE;QAC3C,qCAAqC;QACrC,kCAAkC;QAClC,MAAM,4BAA4B,GAAG,OAAO,CAC1C,UAAU;aACP,uBAAuB,EAAE;aACzB,GAAG,CAAC,YAAY,CAAC,oBAAoB,CAAC,CAC1C,CAAC;QACF,IAAI,CAAC,4BAA4B,EAAE;YACjC,iBAAiB,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;SACvC;QACD,iDAAiD;KAClD;AACH,CAAC,CAAC"}
@@ -1,4 +1,6 @@
1
1
  import type { GraphQLSchema } from 'graphql';
2
+ import { type SourceFile, Project } from 'ts-morph';
3
+ import type { GraphQLObjectTypeResolversToGenerate } from '../getGraphQLObjectTypeResolversToGenerate';
2
4
  import type { ParseSourcesResult } from '../parseSources';
3
5
  import type { ImportLineMeta, RootObjectType } from '../utils';
4
6
  interface BaseVirtualFile {
@@ -29,6 +31,7 @@ export interface ObjectTypeFile extends BaseVirtualFile {
29
31
  meta: {
30
32
  variableStatement: string;
31
33
  normalizedResolverName: string;
34
+ resolversToGenerate?: GraphQLObjectTypeResolversToGenerate[number];
32
35
  };
33
36
  }
34
37
  export declare type ResolverFile = GeneralResolverFile | RootObjectTypeFieldResolverFile | ObjectTypeFile;
@@ -44,6 +47,12 @@ export interface GenerateResolverFilesContext {
44
47
  whitelistedModules: string[];
45
48
  blacklistedModules: string[];
46
49
  externalResolvers: Record<string, string>;
50
+ tsMorph: {
51
+ project: Project;
52
+ typesSourceFile: SourceFile;
53
+ };
54
+ graphQLObjectTypeResolversToGenerate: GraphQLObjectTypeResolversToGenerate;
55
+ fixObjectTypeResolvers: 'smart' | 'disabled';
47
56
  };
48
57
  result: {
49
58
  files: Record<string, StandardFile | ResolverFile>;
@@ -0,0 +1,11 @@
1
+ import { type SourceFile } from 'ts-morph';
2
+ import type { TypeMappersMap } from '../parseTypeMappers';
3
+ export declare type GraphQLObjectTypeResolversToGenerate = Record<string, Record<string, {
4
+ resolverName: string;
5
+ resolverDeclaration: string;
6
+ }>>;
7
+ export declare const getGraphQLObjectTypeResolversToGenerate: ({ typesSourceFile, userDefinedSchemaTypeMap, typeMappersMap, }: {
8
+ typesSourceFile: SourceFile;
9
+ typeMappersMap: TypeMappersMap;
10
+ userDefinedSchemaTypeMap: Record<string, true>;
11
+ }) => GraphQLObjectTypeResolversToGenerate;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getGraphQLObjectTypeResolversToGenerate = void 0;
4
+ const ts_morph_1 = require("ts-morph");
5
+ const utils_1 = require("../utils");
6
+ const getGraphQLObjectTypeResolversToGenerate = ({ typesSourceFile, userDefinedSchemaTypeMap, typeMappersMap, }) => {
7
+ const typeMappersEntries = Object.entries(typeMappersMap);
8
+ if (typeMappersEntries.length === 0) {
9
+ return {};
10
+ }
11
+ // 1. Get property map of all schema types
12
+ const schemaTypePropertyMap = {};
13
+ const populateSchemaTypePropertyMap = (node) => {
14
+ const identifier = node.getNameNode();
15
+ const identifierName = identifier.getText();
16
+ if (userDefinedSchemaTypeMap[identifierName]) {
17
+ schemaTypePropertyMap[identifierName] = (0, utils_1.getNodePropertyMap)(node);
18
+ }
19
+ };
20
+ typesSourceFile
21
+ .getDescendantsOfKind(ts_morph_1.SyntaxKind.TypeAliasDeclaration)
22
+ .forEach(populateSchemaTypePropertyMap);
23
+ typesSourceFile
24
+ .getDescendantsOfKind(ts_morph_1.SyntaxKind.InterfaceDeclaration)
25
+ .forEach(populateSchemaTypePropertyMap);
26
+ // 3. Find resolvers to generate and add reason
27
+ const result = {};
28
+ typeMappersEntries.forEach(([_, { schemaType, typeMapperName, typeMapperPropertyMap }]) => {
29
+ const matchedSchemaTypePropertyMap = schemaTypePropertyMap[schemaType];
30
+ if (matchedSchemaTypePropertyMap) {
31
+ Object.values(matchedSchemaTypePropertyMap).forEach((schemaTypeProperty) => {
32
+ const typeMapperProperty = typeMapperPropertyMap[schemaTypeProperty.name];
33
+ const typeMapperPropertyIdentifier = `${typeMapperName}.${schemaTypeProperty.name}`;
34
+ const schemaTypePropertyIdentifier = `${schemaType}.${schemaTypeProperty.name}`;
35
+ if (schemaTypeProperty.name === '__typename') {
36
+ return;
37
+ }
38
+ result[schemaType] = result[schemaType] || {};
39
+ // If mapper does not have a field in schema type, report
40
+ if (!typeMapperProperty) {
41
+ result[schemaType][schemaTypeProperty.name] = {
42
+ resolverName: schemaTypeProperty.name,
43
+ resolverDeclaration: `() => { /* ${schemaTypePropertyIdentifier} resolver is required because ${schemaTypePropertyIdentifier} exists but ${typeMapperPropertyIdentifier} does not */ }`,
44
+ };
45
+ return;
46
+ }
47
+ /**
48
+ * FIXME: there's currently no way to check if a type is assignable to another type
49
+ * https://github.com/dsherret/ts-morph/issues/357
50
+ * https://github.com/microsoft/TypeScript/issues/9879
51
+ *
52
+ * Therefore, the workaround now is to generate all resolvers with matching names, then use TS diagnostics to see if there's error when trying to merge the two keys
53
+ *
54
+ * Note: this happens only when mappers are used
55
+ */
56
+ result[schemaType][schemaTypeProperty.name] = {
57
+ resolverName: schemaTypeProperty.name,
58
+ resolverDeclaration: `({ ${schemaTypeProperty.name} }) => {
59
+ /* ${schemaTypePropertyIdentifier} resolver is required because ${schemaTypePropertyIdentifier} and ${typeMapperPropertyIdentifier} are not compatible */
60
+ return ${schemaTypeProperty.name}
61
+ }`,
62
+ };
63
+ return;
64
+ });
65
+ }
66
+ });
67
+ return result;
68
+ };
69
+ exports.getGraphQLObjectTypeResolversToGenerate = getGraphQLObjectTypeResolversToGenerate;
70
+ //# sourceMappingURL=getGraphQLObjectTypeResolversToGenerate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getGraphQLObjectTypeResolversToGenerate.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/getGraphQLObjectTypeResolversToGenerate/getGraphQLObjectTypeResolversToGenerate.ts"],"names":[],"mappings":";;;AAAA,uCAKkB;AAElB,oCAAoE;AAO7D,MAAM,uCAAuC,GAAG,CAAC,EACtD,eAAe,EACf,wBAAwB,EACxB,cAAc,GAKf,EAAwC,EAAE;IACzC,MAAM,kBAAkB,GAAG,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;IAC1D,IAAI,kBAAkB,CAAC,MAAM,KAAK,CAAC,EAAE;QACnC,OAAO,EAAE,CAAC;KACX;IAED,0CAA0C;IAC1C,MAAM,qBAAqB,GAAoC,EAAE,CAAC;IAElE,MAAM,6BAA6B,GAAG,CACpC,IAAiD,EAC3C,EAAE;QACR,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;QACtC,MAAM,cAAc,GAAG,UAAU,CAAC,OAAO,EAAE,CAAC;QAC5C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BACrC,mBAAmB,EAAE,cAAc,4BAA4B,iCAAiC,4BAA4B,eAAe,4BAA4B,gBAAgB;qBACxL,CAAC;oBACF,OAAO;iBACR;gBAED;;;;;;;;mBAQG;gBACH,MAAM,CAAC,UAAU,CAAC,CAAC,kBAAkB,CAAC,IAAI,CAAC,GAAG;oBAC5C,YAAY,EAAE,kBAAkB,CAAC,IAAI;oBACrC,mBAAmB,EAAE,MAAM,kBAAkB,CAAC,IAAI;qBAC3C,4BAA4B,iCAAiC,4BAA4B,QAAQ,4BAA4B;yBACzH,kBAAkB,CAAC,IAAI;gBAChC;iBACH,CAAC;gBAEF,OAAO;YACT,CAAC,CACF,CAAC;SACH;IACH,CAAC,CACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAvFW,QAAA,uCAAuC,2CAuFlD"}
@@ -0,0 +1 @@
1
+ export * from './getGraphQLObjectTypeResolversToGenerate';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./getGraphQLObjectTypeResolversToGenerate"), exports);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/getGraphQLObjectTypeResolversToGenerate/index.ts"],"names":[],"mappings":";;;AAAA,oFAA0D"}
@@ -8,6 +8,12 @@ interface GetPluginsConfigParams {
8
8
  whitelistedModules: string[];
9
9
  blacklistedModules: string[];
10
10
  }
11
- declare type GetPluginsConfigResult = Record<'defaultScalarTypesMap' | 'defaultScalarExternalResolvers' | 'defaultTypeMappers', Record<string, string>>;
11
+ declare type GetPluginsConfigResult = {
12
+ userDefinedSchemaTypeMap: Record<string, true>;
13
+ pluginsConfig: Record<'defaultScalarTypesMap' | 'defaultScalarExternalResolvers' | 'defaultTypeMappers', Record<string, string>>;
14
+ };
15
+ /**
16
+ * TODO: rename to parseGraphQLSchema
17
+ */
12
18
  export declare const getPluginsConfig: ({ schemaAst, sourceMap, typeMappersMap, whitelistedModules, blacklistedModules, }: GetPluginsConfigParams) => GetPluginsConfigResult;
13
19
  export {};
@@ -4,6 +4,9 @@ exports.getPluginsConfig = void 0;
4
4
  const graphql_1 = require("graphql");
5
5
  const graphql_scalars_1 = require("graphql-scalars");
6
6
  const utils_1 = require("../utils");
7
+ /**
8
+ * TODO: rename to parseGraphQLSchema
9
+ */
7
10
  const getPluginsConfig = ({ schemaAst, sourceMap, typeMappersMap, whitelistedModules, blacklistedModules, }) => {
8
11
  return Object.entries(schemaAst.getTypeMap()).reduce((res, [schemaType, namedType]) => {
9
12
  var _a;
@@ -23,17 +26,22 @@ const getPluginsConfig = ({ schemaAst, sourceMap, typeMappersMap, whitelistedMod
23
26
  handleScalarType(schemaType, res);
24
27
  }
25
28
  if (!(0, utils_1.isRootObjectType)(schemaType) && (0, graphql_1.isObjectType)(namedType)) {
29
+ res.userDefinedSchemaTypeMap[schemaType] = true;
30
+ // Wire up `mappers` config
26
31
  const typeMapperDetails = typeMappersMap[schemaType];
27
32
  if (typeMapperDetails) {
28
- res.defaultTypeMappers[typeMapperDetails.schemaType] =
33
+ res.pluginsConfig.defaultTypeMappers[typeMapperDetails.schemaType] =
29
34
  typeMapperDetails.configImportPath;
30
35
  }
31
36
  }
32
37
  return res;
33
38
  }, {
34
- defaultScalarTypesMap: {},
35
- defaultScalarExternalResolvers: {},
36
- defaultTypeMappers: {},
39
+ userDefinedSchemaTypeMap: {},
40
+ pluginsConfig: {
41
+ defaultScalarTypesMap: {},
42
+ defaultScalarExternalResolvers: {},
43
+ defaultTypeMappers: {},
44
+ },
37
45
  });
38
46
  };
39
47
  exports.getPluginsConfig = getPluginsConfig;
@@ -44,9 +52,9 @@ const handleScalarType = (schemaType, result) => {
44
52
  }
45
53
  if (scalarResolver.extensions.codegenScalarType &&
46
54
  typeof scalarResolver.extensions.codegenScalarType === 'string') {
47
- result.defaultScalarTypesMap[schemaType] =
55
+ result.pluginsConfig.defaultScalarTypesMap[schemaType] =
48
56
  scalarResolver.extensions.codegenScalarType;
49
57
  }
50
- result.defaultScalarExternalResolvers[schemaType] = `~graphql-scalars#${scalarResolver.name}Resolver`;
58
+ result.pluginsConfig.defaultScalarExternalResolvers[schemaType] = `~graphql-scalars#${scalarResolver.name}Resolver`;
51
59
  };
52
60
  //# sourceMappingURL=getPluginsConfig.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"getPluginsConfig.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/getPluginsConfig/getPluginsConfig.ts"],"names":[],"mappings":";;;AAAA,qCAAoE;AACpE,qDAA+D;AAG/D,oCAIkB;AAiBX,MAAM,gBAAgB,GAAG,CAAC,EAC/B,SAAS,EACT,SAAS,EACT,cAAc,EACd,kBAAkB,EAClB,kBAAkB,GACK,EAA0B,EAAE;IACnD,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC,MAAM,CAClD,CAAC,GAAG,EAAE,CAAC,UAAU,EAAE,SAAS,CAAC,EAAE,EAAE;;QAC/B,IAAI,IAAA,yBAAiB,EAAC,SAAS,CAAC,EAAE;YAChC,OAAO,GAAG,CAAC;SACZ;QAED,MAAM,YAAY,GAAG,IAAA,yCAAiC,EAAC;YACrD,QAAQ,EAAE,MAAA,SAAS,CAAC,OAAO,0CAAE,GAAG;YAChC,SAAS;YACT,kBAAkB;YAClB,kBAAkB;SACnB,CAAC,CAAC;QACH,IAAI,CAAC,YAAY,EAAE;YACjB,OAAO,GAAG,CAAC;SACZ;QAED,IAAI,IAAA,sBAAY,EAAC,SAAS,CAAC,EAAE;YAC3B,gBAAgB,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;SACnC;QAED,IAAI,CAAC,IAAA,wBAAgB,EAAC,UAAU,CAAC,IAAI,IAAA,sBAAY,EAAC,SAAS,CAAC,EAAE;YAC5D,MAAM,iBAAiB,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;YACrD,IAAI,iBAAiB,EAAE;gBACrB,GAAG,CAAC,kBAAkB,CAAC,iBAAiB,CAAC,UAAU,CAAC;oBAClD,iBAAiB,CAAC,gBAAgB,CAAC;aACtC;SACF;QAED,OAAO,GAAG,CAAC;IACb,CAAC,EACD;QACE,qBAAqB,EAAE,EAAE;QACzB,8BAA8B,EAAE,EAAE;QAClC,kBAAkB,EAAE,EAAE;KACvB,CACF,CAAC;AACJ,CAAC,CAAC;AA3CW,QAAA,gBAAgB,oBA2C3B;AAEF,MAAM,gBAAgB,GAAG,CACvB,UAAkB,EAClB,MAA8B,EACxB,EAAE;IACR,MAAM,cAAc,GAAG,2BAAe,CAAC,UAAU,CAAC,CAAC;IACnD,IAAI,CAAC,cAAc,EAAE;QACnB,OAAO;KACR;IAED,IACE,cAAc,CAAC,UAAU,CAAC,iBAAiB;QAC3C,OAAO,cAAc,CAAC,UAAU,CAAC,iBAAiB,KAAK,QAAQ,EAC/D;QACA,MAAM,CAAC,qBAAqB,CAAC,UAAU,CAAC;YACtC,cAAc,CAAC,UAAU,CAAC,iBAAiB,CAAC;KAC/C;IAED,MAAM,CAAC,8BAA8B,CACnC,UAAU,CACX,GAAG,oBAAoB,cAAc,CAAC,IAAI,UAAU,CAAC;AACxD,CAAC,CAAC"}
1
+ {"version":3,"file":"getPluginsConfig.js","sourceRoot":"","sources":["../../../../../packages/typescript-resolver-files/src/getPluginsConfig/getPluginsConfig.ts"],"names":[],"mappings":";;;AAAA,qCAAoE;AACpE,qDAA+D;AAG/D,oCAIkB;AAoBlB;;GAEG;AACI,MAAM,gBAAgB,GAAG,CAAC,EAC/B,SAAS,EACT,SAAS,EACT,cAAc,EACd,kBAAkB,EAClB,kBAAkB,GACK,EAA0B,EAAE;IACnD,OAAO,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC,MAAM,CAClD,CAAC,GAAG,EAAE,CAAC,UAAU,EAAE,SAAS,CAAC,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;QAC3C,OAAO,cAAc,CAAC,UAAU,CAAC,iBAAiB,KAAK,QAAQ,EAC/D;QACA,MAAM,CAAC,aAAa,CAAC,qBAAqB,CAAC,UAAU,CAAC;YACpD,cAAc,CAAC,UAAU,CAAC,iBAAiB,CAAC;KAC/C;IAED,MAAM,CAAC,aAAa,CAAC,8BAA8B,CACjD,UAAU,CACX,GAAG,oBAAoB,cAAc,CAAC,IAAI,UAAU,CAAC;AACxD,CAAC,CAAC"}
@@ -1,7 +1,8 @@
1
- import type { SourceFile } from 'ts-morph';
1
+ import { type SourceFile } from 'ts-morph';
2
2
  import type { TypeMappersMap } from './parseTypeMappers';
3
- export declare const collectTypeMappersFromSourceFile: ({ typeMappersSourceFile, typeMappersSuffix, resolverTypesPath, }: {
3
+ export declare const collectTypeMappersFromSourceFile: ({ typeMappersSourceFile, typeMappersSuffix, resolverTypesPath, shouldCollectPropertyMap, }: {
4
4
  typeMappersSourceFile: SourceFile;
5
5
  typeMappersSuffix: string;
6
6
  resolverTypesPath: string;
7
+ shouldCollectPropertyMap: boolean;
7
8
  }, result: TypeMappersMap) => void;