feat: graphql + typeorm + config + postgresql
This commit is contained in:
@ -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
10
src/app.resolver.ts
Normal 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!' };
|
||||
}
|
||||
}
|
9
src/commons/config/configuration.ts
Normal file
9
src/commons/config/configuration.ts
Normal 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
7
src/hello.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { Field, ObjectType } from '@nestjs/graphql';
|
||||
|
||||
@ObjectType()
|
||||
export class Hello {
|
||||
@Field()
|
||||
message: string;
|
||||
}
|
@ -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();
|
||||
|
Reference in New Issue
Block a user