From 217c352f180ca954d265e7afbe78b715deed893e Mon Sep 17 00:00:00 2001 From: Michal Baranowski Date: Fri, 31 Dec 2021 16:58:23 +0100 Subject: [PATCH 1/2] detects if there's a module called shared.graphql - if yes, it's merging shared module schema with other modules while buildingSchema and prevents generating duplicate resolvers --- README.md | 2 +- src/generate/generate-module.ts | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a11580d..230f942 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ $ npm install -g chimp $ chimp COMMAND running command... $ chimp (-v|--version|version) -chimp/0.0.0-development darwin-x64 node-v12.16.2 +chimp/0.0.0-development darwin-x64 node-v14.15.4 $ chimp --help [COMMAND] USAGE $ chimp COMMAND diff --git a/src/generate/generate-module.ts b/src/generate/generate-module.ts index c34f4d7..cb9d7f3 100755 --- a/src/generate/generate-module.ts +++ b/src/generate/generate-module.ts @@ -82,13 +82,16 @@ export const executeGeneration = async (appPrefix = '~app', generatedPrefix = '~ saveRenderedTemplate(templateName, { appPrefix }, filePath, fileName); }; + const isSharedModule = (module: {name: string}) => module.name.toLowerCase() === 'shared'; + debug('createGetCodegenConfig'); createGetCodegenConfig(); const modulesResolvedPath = path.join(projectMainPath, modulesPath); const graphqlPaths = shelljs.ls(path.join(modulesResolvedPath, '**/*.graphql')); const moduleNames = getModuleNames(graphqlPaths, projectMainPath); - const modules = getModuleInfos(moduleNames); + const modules = getModuleInfos(moduleNames).sort((a) => (isSharedModule(a) ? -1 : 1)); + const hasSharedModule = modules.length && isSharedModule(modules[0]); const createGlobalResolvers = () => { const templateName = './templates/resolvers.handlebars'; @@ -239,9 +242,17 @@ export const executeGeneration = async (appPrefix = '~app', generatedPrefix = '~ }); const createTypeResolvers = () => { - modules.forEach(({ name, typeDefinitions, types, schemaString, queries, mutations, graphqlFileRootPath }) => { + const sharedInterfaces: string[] = []; + const sharedTypeDefinitions: {name: string}[] = []; + modules.forEach(({ name, typeDefinitions, types, schemaString: schemaSDL, queries, mutations, graphqlFileRootPath }, moduleIndex) => { const typeResolvers: { typeName: string; fieldName: { name: string; capitalizedName: string }[] }[] = []; + let schemaString = schemaSDL; + if (hasSharedModule && !isSharedModule({name})) { + schemaString = `${modules[0].schemaString} +${schemaString}` + } + if (types) { debug(`create type resolvers for module ${name}`); @@ -251,6 +262,7 @@ export const executeGeneration = async (appPrefix = '~app', generatedPrefix = '~ // eslint-disable-next-line no-param-reassign schemaString = schemaString.replace(/extend type/g, 'type'); const source = new Source(schemaString); + console.log("MICHAL: schemaString", schemaString); const schema = buildSchema(source, { assumeValidSDL: true }); shelljs.mkdir('-p', `${projectMainPath}/src/${graphqlFileRootPath}/types/`); @@ -311,7 +323,7 @@ export const executeGeneration = async (appPrefix = '~app', generatedPrefix = '~ saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists); }; - interfaces.forEach((interfaceName) => { + interfaces.filter(i => !sharedInterfaces.includes(i)).forEach((interfaceName) => { createInterfaceType(interfaceName); createInterfaceSpec(interfaceName); createInterfaceSpecWrapper(interfaceName); @@ -320,8 +332,13 @@ export const executeGeneration = async (appPrefix = '~app', generatedPrefix = '~ fieldName: [{ name: '__resolveType', capitalizedName: capitalize('__resolveType') }], }); }); + + if(isSharedModule({name})) { + sharedInterfaces.push(...interfaces) + sharedTypeDefinitions.push(...typeDefinitions) + } type FilteredType = { name: { value: string }; resolveReferenceType: boolean; arguments?: string[] }; - typeDefinitions.forEach((typeDef) => { + typeDefinitions.filter(t => !sharedTypeDefinitions.includes(t)).forEach((typeDef) => { let filtered: FilteredType[] = []; let type = schema.getType(typeDef.name); if (!type) { From 3e4e50a7de52e1e3cc352ccfd35fc9acafb65d19 Mon Sep 17 00:00:00 2001 From: Michal Baranowski Date: Fri, 31 Dec 2021 17:05:55 +0100 Subject: [PATCH 2/2] removes console logs --- src/generate/generate-module.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/generate/generate-module.ts b/src/generate/generate-module.ts index cb9d7f3..5599391 100755 --- a/src/generate/generate-module.ts +++ b/src/generate/generate-module.ts @@ -262,7 +262,6 @@ ${schemaString}` // eslint-disable-next-line no-param-reassign schemaString = schemaString.replace(/extend type/g, 'type'); const source = new Source(schemaString); - console.log("MICHAL: schemaString", schemaString); const schema = buildSchema(source, { assumeValidSDL: true }); shelljs.mkdir('-p', `${projectMainPath}/src/${graphqlFileRootPath}/types/`);