Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared module #770

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions src/generate/generate-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`);

Expand Down Expand Up @@ -311,7 +322,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);
Expand All @@ -320,8 +331,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) {
Expand Down