Added Swagger and TypeORM, added Hello World
This commit is contained in:
704
package-lock.json
generated
704
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -25,12 +25,15 @@
|
|||||||
"@nestjs/graphql": "^13.2.3",
|
"@nestjs/graphql": "^13.2.3",
|
||||||
"@nestjs/mercurius": "^13.2.3",
|
"@nestjs/mercurius": "^13.2.3",
|
||||||
"@nestjs/platform-fastify": "^11.1.12",
|
"@nestjs/platform-fastify": "^11.1.12",
|
||||||
|
"@nestjs/swagger": "^11.2.5",
|
||||||
"fastify": "^5.7.2",
|
"fastify": "^5.7.2",
|
||||||
"graphiql": "^5.2.2",
|
"graphiql": "^5.2.2",
|
||||||
"graphql": "^16.12.0",
|
"graphql": "^16.12.0",
|
||||||
"mercurius": "^16.7.0",
|
"mercurius": "^16.7.0",
|
||||||
|
"mysql2": "^3.16.2",
|
||||||
"reflect-metadata": "^0.2.2",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.1"
|
"rxjs": "^7.8.1",
|
||||||
|
"typeorm": "^0.3.28"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/eslintrc": "^3.2.0",
|
"@eslint/eslintrc": "^3.2.0",
|
||||||
@@ -56,7 +59,6 @@
|
|||||||
"typescript": "^5.7.3",
|
"typescript": "^5.7.3",
|
||||||
"typescript-eslint": "^8.20.0"
|
"typescript-eslint": "^8.20.0"
|
||||||
},
|
},
|
||||||
|
|
||||||
"jest": {
|
"jest": {
|
||||||
"moduleFileExtensions": [
|
"moduleFileExtensions": [
|
||||||
"js",
|
"js",
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import { Controller, Get } from '@nestjs/common';
|
import { Controller, Get } from '@nestjs/common';
|
||||||
import { AppService } from './app.service';
|
import { AppService } from './app.service';
|
||||||
|
import { Query, Resolver } from '@nestjs/graphql';
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
|
@Resolver()
|
||||||
export class AppController {
|
export class AppController {
|
||||||
constructor(private readonly appService: AppService) {}
|
constructor(private readonly appService: AppService) {}
|
||||||
|
|
||||||
@Get()
|
@Query(()=>String)
|
||||||
getHello(): string {
|
@Get("/")
|
||||||
|
hello(): string {
|
||||||
return this.appService.getHello();
|
return this.appService.getHello();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,19 +3,18 @@ import { AppController } from './app.controller';
|
|||||||
import { AppService } from './app.service';
|
import { AppService } from './app.service';
|
||||||
import { GraphQLModule } from '@nestjs/graphql';
|
import { GraphQLModule } from '@nestjs/graphql';
|
||||||
import { MercuriusDriver, MercuriusDriverConfig } from '@nestjs/mercurius';
|
import { MercuriusDriver, MercuriusDriverConfig } from '@nestjs/mercurius';
|
||||||
import { HelloResolver } from './hello.resolver';
|
import { databaseProviders } from './database.providers';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
GraphQLModule.forRoot<MercuriusDriverConfig>({
|
GraphQLModule.forRoot<MercuriusDriverConfig>({
|
||||||
driver: MercuriusDriver,
|
driver: MercuriusDriver,
|
||||||
path: '/api/graphql',
|
|
||||||
graphiql: true,
|
graphiql: true,
|
||||||
autoSchemaFile: true
|
autoSchemaFile: true
|
||||||
} as MercuriusDriverConfig),
|
} as MercuriusDriverConfig),
|
||||||
],
|
],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: [AppService,HelloResolver],
|
providers: [AppService, AppController, ...databaseProviders],
|
||||||
|
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
|||||||
23
src/database.providers.ts
Normal file
23
src/database.providers.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { DataSource } from 'typeorm';
|
||||||
|
|
||||||
|
export const databaseProviders = [
|
||||||
|
{
|
||||||
|
provide: 'DATA_SOURCE',
|
||||||
|
useFactory: async () => {
|
||||||
|
const dataSource = new DataSource({
|
||||||
|
type: 'mysql',
|
||||||
|
host: process.env.DB_HOST || 'database',
|
||||||
|
port: parseInt(process.env.DB_PORT || "3006"),
|
||||||
|
username: process.env.DB_USER || 'root',
|
||||||
|
password: process.env.DB_PASSWORD || 'root',
|
||||||
|
database: process.env.DB_NAME || 'ritzenbergen',
|
||||||
|
entities: [
|
||||||
|
__dirname + '/**/*.entity{.ts,.js}',
|
||||||
|
],
|
||||||
|
synchronize: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
return dataSource.initialize();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
import { Resolver, Query } from '@nestjs/graphql';
|
|
||||||
|
|
||||||
@Resolver()
|
|
||||||
export class HelloResolver {
|
|
||||||
|
|
||||||
@Query(() => String)
|
|
||||||
hello(): string {
|
|
||||||
return 'hello world';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
18
src/main.ts
18
src/main.ts
@@ -1,9 +1,23 @@
|
|||||||
import { NestFactory } from '@nestjs/core';
|
import { NestFactory } from '@nestjs/core';
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
import { NestFastifyApplication, FastifyAdapter } from '@nestjs/platform-fastify';
|
import { NestFastifyApplication, FastifyAdapter } from '@nestjs/platform-fastify';
|
||||||
|
import { FastifyContextConfig } from 'fastify';
|
||||||
|
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
|
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter({
|
||||||
await app.listen(process.env.PORT ?? 3000);
|
listeningOrigin: "0.0.0.0"
|
||||||
|
} as FastifyContextConfig));
|
||||||
|
|
||||||
|
const config = new DocumentBuilder()
|
||||||
|
.setTitle('Ritzenbergen API')
|
||||||
|
.setDescription('The Ritzenbergen API description')
|
||||||
|
.setVersion('1.0')
|
||||||
|
.addTag('ritzenbergen')
|
||||||
|
.build();
|
||||||
|
const document = SwaggerModule.createDocument(app, config);
|
||||||
|
SwaggerModule.setup('/api/docs', app, document);
|
||||||
|
|
||||||
|
await app.listen(process.env.PORT ?? 3000, "0.0.0.0");
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user