feat: graphql + typeorm + config + postgresql

This commit is contained in:
Ivan Li
2021-01-31 13:32:15 +08:00
parent a77e22edd0
commit 7ba5e220d9
8 changed files with 4666 additions and 231 deletions

View File

@ -1,10 +1,42 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppResolver } from './app.resolver';
import { AppService } from './app.service';
import configuration from './commons/config/configuration';
@Module({
imports: [],
imports: [
ConfigModule.forRoot({
load: [configuration],
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (cnfigService: ConfigService) => ({
type: 'postgres',
host: cnfigService.get<string>('db.postgres.host'),
port: cnfigService.get<number>('db.postgres.port'),
username: cnfigService.get<string>('db.postgres.username'),
password: cnfigService.get<string>('db.postgres.password'),
database: cnfigService.get<string>('db.postgres.database'),
synchronize: true,
autoLoadEntities: true,
}),
inject: [ConfigService],
}),
GraphQLModule.forRootAsync({
imports: [ConfigModule],
useFactory: (cnfigService: ConfigService) => ({
debug: cnfigService.get<string>('env') !== 'prod',
playground: true,
autoSchemaFile: true,
}),
inject: [ConfigService],
}),
],
controllers: [AppController],
providers: [AppService],
providers: [AppService, AppResolver],
})
export class AppModule {}

10
src/app.resolver.ts Normal file
View File

@ -0,0 +1,10 @@
import { Query, Resolver } from '@nestjs/graphql';
import { Hello } from './hello';
@Resolver(() => Hello)
export class AppResolver {
@Query(() => Hello)
async hello() {
return { message: 'Hello, World!' };
}
}

View File

@ -0,0 +1,9 @@
import { readFileSync } from 'fs';
import * as yaml from 'js-yaml';
import { join } from 'path';
export default () => {
return yaml.load(
readFileSync(join(__dirname, '../../../config.yml'), 'utf8'),
) as unknown;
};

7
src/hello.ts Normal file
View File

@ -0,0 +1,7 @@
import { Field, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class Hello {
@Field()
message: string;
}

View File

@ -1,8 +1,10 @@
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(7122);
const configService = app.get(ConfigService);
await app.listen(configService.get<number>('http.port'));
}
bootstrap();