Skip to content

Commit

Permalink
feat(shared-model): generate models from graphql schema
Browse files Browse the repository at this point in the history
  • Loading branch information
timonmasberg committed Feb 10, 2024
1 parent a20a6a4 commit 1cf5903
Show file tree
Hide file tree
Showing 8 changed files with 3,664 additions and 78 deletions.
19 changes: 19 additions & 0 deletions apps/api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@
}
}
},
"build-generate-schema-script": {
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"options": {
"target": "node",
"compiler": "tsc",
"outputPath": "dist/apps/api-generate-schema",
"main": "apps/api/src/generate-schema.ts",
"tsConfig": "apps/api/tsconfig.app.json",
"webpackConfig": "apps/api/webpack.config.js"
}
},
"generate-schema": {
"executor": "@nx/js:node",
"options": {
"buildTarget": "api:build-generate-schema-script",
"watch": false
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"]
Expand Down
5 changes: 1 addition & 4 deletions apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ const UTILITY_MODULES = [
imports: [ConfigModule],
driver: ApolloDriver,
useFactory: (config: ConfigService) => ({
autoSchemaFile:
config.get('NODE_ENV') !== 'production'
? path.join(process.cwd(), 'apps/api/src/schema.gql')
: true,
autoSchemaFile: true,
subscriptions: {
'graphql-ws': true,
},
Expand Down
21 changes: 21 additions & 0 deletions apps/api/src/generate-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NestFactory } from '@nestjs/core';
import { GraphQLSchemaHost } from '@nestjs/graphql';
import { writeFileSync } from 'fs';
import { printSchema } from 'graphql/utilities';
import { join } from 'path';

import { AppModule } from './app/app.module';

const generateSchema = async (): Promise<void> => {
const app = await NestFactory.create(AppModule, { logger: false });
await app.init();

const { schema } = app.get(GraphQLSchemaHost);
const outputPath = join(process.cwd(), 'dist/schema.gql');
writeFileSync(outputPath, printSchema(schema));
await app.close();
// eslint-disable-next-line no-console
console.log(`Scheme generated successfully at ${outputPath}`);
};
// eslint-disable-next-line no-console
generateSchema().catch(console.log);
6 changes: 6 additions & 0 deletions codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
overwrite: true
schema: 'dist/schema.gql'
generates:
libs/shared/model/src/lib/gql.model.ts:
plugins:
- typescript
3 changes: 1 addition & 2 deletions libs/shared/model/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './lib/shared-model';
export { default as AuthUser } from '../../model/src/lib/auth-user.model';
export { default as AuthUser } from './lib/auth-user.model';
Loading

0 comments on commit 1cf5903

Please sign in to comment.