chore: fastify & mercurius without auth

This commit is contained in:
M1000fr 2024-11-25 22:55:25 +01:00
parent 3446f5bee1
commit eb3f3a8de9
6 changed files with 679 additions and 501 deletions

View File

@ -20,10 +20,9 @@
"@nestjs/core": "^10.0.0", "@nestjs/core": "^10.0.0",
"@nestjs/graphql": "^12.2.1", "@nestjs/graphql": "^12.2.1",
"@nestjs/jwt": "^10.2.0", "@nestjs/jwt": "^10.2.0",
"@nestjs/mercurius": "^12.2.1",
"@nestjs/passport": "^10.0.3", "@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-fastify": "^10.4.9",
"@nestjs/platform-socket.io": "^10.4.9",
"@nestjs/websockets": "^10.4.9",
"@prisma/client": "5.22.0", "@prisma/client": "5.22.0",
"apollo-server-express": "^3.13.0", "apollo-server-express": "^3.13.0",
"axios": "^1.7.7", "axios": "^1.7.7",
@ -31,9 +30,7 @@
"graphql-subscriptions": "^3.0.0", "graphql-subscriptions": "^3.0.0",
"graphql-tools": "^9.0.4", "graphql-tools": "^9.0.4",
"graphql-ws": "^5.16.0", "graphql-ws": "^5.16.0",
"passport": "^0.7.0", "mercurius": "14",
"passport-jwt": "^4.0.1",
"passport-oauth2": "^1.8.0",
"prisma": "^5.22.0", "prisma": "^5.22.0",
"reflect-metadata": "^0.2.0", "reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1", "rxjs": "^7.8.1",
@ -42,11 +39,8 @@
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^10.0.0", "@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0", "@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^5.0.0", "@types/express": "^5.0.0",
"@types/node": "^20.3.1", "@types/node": "^20.3.1",
"@types/passport-oauth2": "^1.4.17",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^8.0.0", "@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0", "@typescript-eslint/parser": "^8.0.0",
"eslint": "^9.0.0", "eslint": "^9.0.0",
@ -54,7 +48,6 @@
"eslint-plugin-prettier": "^5.0.0", "eslint-plugin-prettier": "^5.0.0",
"prettier": "^3.0.0", "prettier": "^3.0.0",
"source-map-support": "^0.5.21", "source-map-support": "^0.5.21",
"supertest": "^7.0.0",
"ts-loader": "^9.4.3", "ts-loader": "^9.4.3",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0", "tsconfig-paths": "^4.2.0",

View File

@ -1,20 +1,9 @@
import { Controller, Get, UseGuards } from "@nestjs/common"; import { Controller, Get } from '@nestjs/common';
import { AppService } from "./app.service";
import { AuthGuard } from "./auth/auth.guard";
@Controller() @Controller()
export class AppController { export class AppController {
constructor(private readonly appService: AppService) {}
@Get() @Get()
getHello() { getHello(): string {
return this.appService.getHello(); return 'Hello World!';
}
@Get("protected")
@UseGuards(AuthGuard)
getProtectedHello() {
return this.appService.getHello();
} }
} }

View File

@ -1,15 +1,15 @@
import { ApolloDriver, ApolloDriverConfig } from "@nestjs/apollo";
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config"; import { ConfigModule } from "@nestjs/config";
import { GraphQLModule } from "@nestjs/graphql";
import { JwtModule } from "@nestjs/jwt"; import { JwtModule } from "@nestjs/jwt";
import { GraphQLModule } from "@nestjs/graphql";
import { MercuriusDriver, MercuriusDriverConfig } from "@nestjs/mercurius";
import { AppController } from "./app.controller";
import { AppService } from "./app.service"; import { AppService } from "./app.service";
import { AuthModule } from "./auth/auth.module"; import { AuthModule } from "./auth/auth.module";
import { UserModule } from "./user/user.module"; import { UserModule } from "./user/user.module";
import { AppController } from "./app.controller";
import { join } from "path"; import { GraphqlOptions } from "./graphqlOptions";
@Module({ @Module({
imports: [ imports: [
@ -20,19 +20,14 @@ import { join } from "path";
global: true, global: true,
secret: process.env.JWT_SECRET, secret: process.env.JWT_SECRET,
}), }),
GraphQLModule.forRoot<ApolloDriverConfig>({ GraphQLModule.forRootAsync<MercuriusDriverConfig>({
driver: ApolloDriver, driver: MercuriusDriver,
playground: true, useClass: GraphqlOptions,
autoSchemaFile: join(process.cwd(), "src/schema.gql"),
installSubscriptionHandlers: true,
subscriptions: {
"graphql-ws": true,
},
}), }),
AuthModule, AuthModule,
UserModule, UserModule,
], ],
controllers: [AppController],
providers: [AppService], providers: [AppService],
controllers: [AppController],
}) })
export class AppModule {} export class AppModule {}

14
src/graphqlOptions.ts Normal file
View File

@ -0,0 +1,14 @@
import { GqlOptionsFactory } from "@nestjs/graphql";
import { Injectable } from "@nestjs/common";
import { MercuriusDriverConfig } from "@nestjs/mercurius";
@Injectable()
export class GraphqlOptions implements GqlOptionsFactory {
createGqlOptions(): Promise<MercuriusDriverConfig> | MercuriusDriverConfig {
return {
autoSchemaFile: "src/schema.gql",
subscription: true,
graphiql: true,
};
}
}

View File

@ -1,11 +1,17 @@
import { NestFactory } from "@nestjs/core"; import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module"; import { AppModule } from "./app.module";
import {
FastifyAdapter,
NestFastifyApplication,
} from "@nestjs/platform-fastify";
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule, new FastifyAdapter());
app.enableCors({ app.enableCors({
origin: "*" origin: "*",
}); });
await app.listen(process.env.PORT ?? 3000);
await app.listen(process.env.PORT ?? 3000, "0.0.0.0");
} }
bootstrap(); bootstrap();

1099
yarn.lock

File diff suppressed because it is too large Load Diff