Skip to content

Commit

Permalink
feat: move types generation back to chimp itself, cleaner project
Browse files Browse the repository at this point in the history
  • Loading branch information
lgandecki committed Aug 18, 2023
1 parent fd4d5f0 commit bf785e6
Show file tree
Hide file tree
Showing 8 changed files with 1,750 additions and 91 deletions.
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
},
"bugs": "https://github.com/xolvio/chimp/issues",
"dependencies": {
"@graphql-tools/graphql-file-loader": "^7.5.5",
"@graphql-tools/load": "^7.7.7",
"@graphql-tools/merge": "^8.3.6",
"@graphql-codegen/add": "5.0.0",
"@graphql-codegen/cli": "5.0.0",
"@graphql-codegen/typescript": "4.0.1",
"@graphql-codegen/typescript-mongodb": "2.4.6",
"@graphql-codegen/typescript-operations": "4.0.1",
"@graphql-codegen/typescript-resolvers": "4.0.1",
"@graphql-tools/graphql-file-loader": "8.0.0",
"@graphql-tools/load": "8.0.0",
"@graphql-tools/merge": "9.0.0",
"@oclif/core": "^2",
"@oclif/plugin-help": "^5",
"chimp-graphql-codegen-plugin": "latest",
"debug": "4.3.4",
"enquirer": "^2.3.6",
"find-package-json": "^1.2.0",
Expand Down
9 changes: 6 additions & 3 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ const runTypeGen = async (projectMainPath: string, appPrefix: string) => {
const codegenConfigPath = fs.existsSync(customCodegenConfig)
? customCodegenConfig
: path.join(__dirname, '../generate/runtime-config-helpers/codegen.js');
await execQuietly(`APP_PREFIX=${appPrefix} npx graphql-codegen --config ${codegenConfigPath}`, {
cwd: projectMainPath,
});
await execQuietly(
`PROJECT_PATH=${projectMainPath} APP_PREFIX=${appPrefix} npx graphql-codegen --config ${codegenConfigPath}`,
{
cwd: path.join(__dirname, '../../'),
},
);
};

const fixGenerated = async (projectMainPath: string) => {
Expand Down
28 changes: 4 additions & 24 deletions src/generate/generate-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import getUnions from './parse-graphql/getUnions';
const debug = configureDebug('generate-module');

const generateSchema = async (projectMainPath: string) => {
await execQuietly(`npx ts-node -r tsconfig-paths/register ./generated/graphql/printSchema.ts`, {
cwd: projectMainPath,
});
await execQuietly(
`PROJECT_PATH=${projectMainPath} npx tsx -r tsconfig-paths/register ${__dirname}/templates/printSchema.ts`,
{},
);
};

export const executeGeneration = async (appPrefix = '~app', generatedPrefix = '~generated', modulesPath = 'src/') => {
Expand All @@ -30,27 +31,6 @@ export const executeGeneration = async (appPrefix = '~app', generatedPrefix = '~
shelljs.mkdir('-p', `${projectMainPath}/generated/graphql/helpers`);

// "Framework" "generated" files - initial generation
const createCombineSchemas = () => {
const templateName = './templates/combineSchemas.ts';
const filePath = `${projectMainPath}/generated/graphql/`;
const fileName = 'combineSchemas.ts';

saveRenderedTemplate(templateName, {}, filePath, fileName);
};

debug('createCombineSchemas');
createCombineSchemas();

const createPrintSchema = () => {
const templateName = './templates/printSchema.ts';
const filePath = `${projectMainPath}/generated/graphql/`;
const fileName = 'printSchema.ts';

saveRenderedTemplate(templateName, {}, filePath, fileName);
};

debug('createPrintSchema');
createPrintSchema();

const createGenericDataModelSchema = () => {
const templateName = './templates/genericDataModelSchema.graphql';
Expand Down
5 changes: 4 additions & 1 deletion src/generate/helpers/execQuietly.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import shelljs from 'shelljs';
import debugConfigurator from 'debug';

const debug = debugConfigurator('execQuietly');

export async function execQuietly(command: string, options: Record<string, unknown>, errorMessage = '') {
return new Promise((resolve, reject) => {
const child = shelljs.exec(command, {
...options,
silent: true,
silent: !debug.enabled,
async: true,
});

Expand Down
8 changes: 4 additions & 4 deletions src/generate/runtime-config-helpers/getCodegenConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { pascalCase } = require('pascal-case');
const { isObjectType, Source, buildSchema } = require('graphql');

let schemaString = fs
.readFileSync('./schema.graphql')
.readFileSync(`${process.env.PROJECT_PATH}/schema.graphql`)
.toString()
.replace(/extend type/g, `type`);

Expand Down Expand Up @@ -52,7 +52,7 @@ module.exports = function ({ contextType } = {}) {
overwrite: true,
schema: schemaString,
generates: {
'generated/graphql/types.ts': {
[`${process.env.PROJECT_PATH}/generated/graphql/types.ts`]: {
config: {
contextType: contextType || `${process.env.APP_PREFIX}/context#GqlContext`,
idFieldName: 'id',
Expand All @@ -69,7 +69,7 @@ module.exports = function ({ contextType } = {}) {
'typescript-resolvers',
'typescript-operations',
'chimp-graphql-codegen-plugin',
{ add: { content: 'export {GqlContext};'} },
{ add: { content: 'export {GqlContext};' } },
{
add: {
content: `
Expand All @@ -79,7 +79,7 @@ module.exports = function ({ contextType } = {}) {
mimetype: string;
encoding: string;
createReadStream(options?:{encoding?: string, highWaterMark?: number}): ReadStream;
}`
}`,
},
},
],
Expand Down
9 changes: 8 additions & 1 deletion src/generate/templates/combineSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import { GraphQLFileLoader, GraphQLFileLoaderOptions } from '@graphql-tools/grap
import { mergeTypeDefs } from '@graphql-tools/merge';
// @ts-ignore
import shelljs from 'shelljs';
import debugConfigurator from 'debug';

const graphqlPaths = shelljs.ls(`${__dirname}/../../src/**/*.graphql`);
const debug = debugConfigurator('combineSchemas.ts');

const graphqlPaths = shelljs.ls(`${process.env.PROJECT_PATH || ''}/src/**/*.graphql`);
debug(
'path to GraphQL Schemas',
graphqlPaths.filter((p) => p),
);

class ExtendedGraphQLFileLoader extends GraphQLFileLoader {
handleFileContent(rawSDL: string, pointer: string, options: GraphQLFileLoaderOptions) {
Expand Down
2 changes: 1 addition & 1 deletion src/generate/templates/printSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import schema from './combineSchemas';

const printed = print(schema);

const outputPath = path.join(process.cwd(), './schema.graphql');
const outputPath = path.join(process.env.PROJECT_PATH || '', './schema.graphql');
fs.writeFileSync(outputPath, printed);
Loading

0 comments on commit bf785e6

Please sign in to comment.