Merge pull request 'feat: remove graphql due to complexity of stack' (#2) from remove/graphql into feature/auth

Reviewed-on: #2
This commit is contained in:
m1000 2024-11-28 14:33:19 +00:00
commit 38105c3d43
11 changed files with 20 additions and 1112 deletions

View File

@ -14,30 +14,20 @@
"preinstall": "node -e \"if(process.env.npm_execpath.indexOf('yarn') === -1) throw new Error('You must use Yarn to install, not NPM')\"" "preinstall": "node -e \"if(process.env.npm_execpath.indexOf('yarn') === -1) throw new Error('You must use Yarn to install, not NPM')\""
}, },
"dependencies": { "dependencies": {
"@fastify/passport": "^3.0.1",
"@nestjs/common": "^10.0.0", "@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.3.0", "@nestjs/config": "^3.3.0",
"@nestjs/core": "^10.0.0", "@nestjs/core": "^10.0.0",
"@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.4.11", "@nestjs/platform-express": "^10.4.11",
"@nestjs/platform-fastify": "^10.4.9",
"@prisma/client": "5.22.0", "@prisma/client": "5.22.0",
"axios": "^1.7.7", "axios": "^1.7.7",
"graphql": "^16.9.0",
"graphql-subscriptions": "^3.0.0",
"graphql-tools": "^9.0.4",
"graphql-ws": "^5.16.0",
"mercurius": "14",
"passport": "^0.7.0", "passport": "^0.7.0",
"passport-discord": "^0.1.4", "passport-discord": "^0.1.4",
"passport-jwt": "^4.0.1", "passport-jwt": "^4.0.1",
"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"
"subscriptions-transport-ws": "^0.11.0"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^10.0.0", "@nestjs/cli": "^10.0.0",

View File

@ -1,13 +1,10 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config"; import { ConfigModule } from "@nestjs/config";
import { JwtModule } from "@nestjs/jwt"; import { JwtModule } from "@nestjs/jwt";
import { GraphQLModule } from "@nestjs/graphql";
import { MercuriusDriver, MercuriusDriverConfig } from "@nestjs/mercurius";
import { AppService } from "./app.service"; import { AppService } from "./app.service";
import { UserModule } from "./user/user.module"; import { UserModule } from "./user/user.module";
import { GraphqlOptions } from "./graphql/graphqlOptions";
import { AuthModule } from './auth/auth.module'; import { AuthModule } from './auth/auth.module';
@Module({ @Module({
@ -19,10 +16,6 @@ import { AuthModule } from './auth/auth.module';
global: true, global: true,
secret: process.env.JWT_SECRET, secret: process.env.JWT_SECRET,
}), }),
// GraphQLModule.forRootAsync<MercuriusDriverConfig>({
// driver: MercuriusDriver,
// useClass: GraphqlOptions,
// }),
UserModule, UserModule,
AuthModule, AuthModule,
], ],

View File

@ -1,46 +0,0 @@
import { Injectable, UnauthorizedException } from "@nestjs/common";
import { GqlOptionsFactory } from "@nestjs/graphql";
import { JwtService } from "@nestjs/jwt";
import { MercuriusDriverConfig } from "@nestjs/mercurius";
@Injectable()
export class GraphqlOptions implements GqlOptionsFactory {
constructor(private readonly jwtService: JwtService) {}
createGqlOptions(): Promise<MercuriusDriverConfig> | MercuriusDriverConfig {
return {
autoSchemaFile: "src/graphql/schema.gql",
subscription: {
context: (_connection, request) => {
const authorization = request.headers.authorization;
if (!authorization) throw new UnauthorizedException();
const token = authorization.split(" ")[1];
if (!token) throw new UnauthorizedException();
try {
const user = this.jwtService.verify(token);
return { user };
} catch (error) {
throw new UnauthorizedException();
}
},
},
graphiql: false,
context: (req) => {
const authorization = req.headers.authorization;
if (!authorization) throw new UnauthorizedException();
const token = authorization.split(" ")[1];
if (!token) throw new UnauthorizedException();
try {
const user = this.jwtService.verify(token);
return { user };
} catch (error) {
throw new UnauthorizedException();
}
},
};
}
}

View File

@ -1,39 +0,0 @@
# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------
type User {
id: String!
username: String!
isAdmin: Boolean!
}
type Query {
users: [User!]!
}
type Mutation {
CreateUser(createUserInput: CreateUserInput!): User!
updateUser(updateUserInput: UpdateUserInput!): User!
setPassword(setUserPasswordInput: SetUserPasswordInput!): User!
}
input CreateUserInput {
username: String!
isAdmin: Boolean!
}
input UpdateUserInput {
username: String
isAdmin: Boolean
id: String!
}
input SetUserPasswordInput {
id: String!
password: String!
}
type Subscription {
userAdded: User!
}

View File

@ -1,10 +1,4 @@
import { Field, InputType } from "@nestjs/graphql";
@InputType()
export class CreateUserInput { export class CreateUserInput {
@Field()
username: string; username: string;
@Field()
isAdmin: boolean; isAdmin: boolean;
} }

View File

@ -1,10 +1,5 @@
import { Field, InputType } from "@nestjs/graphql";
@InputType()
export class SetUserPasswordInput { export class SetUserPasswordInput {
@Field()
id: string; id: string;
@Field()
password: string; password: string;
} }

View File

@ -1,11 +1,7 @@
import { Field, InputType, PartialType } from "@nestjs/graphql";
import { CreateUserInput } from "./create-user.input"; import { CreateUserInput } from "./create-user.input";
@InputType() export class UpdateUserInput extends CreateUserInput {
export class UpdateUserInput extends PartialType(CreateUserInput) {
@Field()
id: string; id: string;
@Field({ nullable: true })
username: string; username: string;
} }

View File

@ -1,13 +1,8 @@
import { Field, ObjectType } from "@nestjs/graphql";
@ObjectType()
export class User { export class User {
@Field(() => String)
id: string; id: string;
@Field(() => String)
username: string; username: string;
@Field(() => Boolean)
isAdmin: boolean; isAdmin: boolean;
} }

View File

@ -1,11 +1,10 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { UserResolver } from "./user.resolver";
import { UserService } from "./user.service"; import { UserService } from "./user.service";
import { PrismaService } from "src/prisma/prisma.service"; import { PrismaService } from "src/prisma/prisma.service";
@Module({ @Module({
providers: [UserResolver, UserService, PrismaService] providers: [UserService, PrismaService]
}) })
export class UserModule {} export class UserModule {}

View File

@ -1,56 +0,0 @@
import {
Args,
Mutation,
Query,
Resolver,
Subscription
} from "@nestjs/graphql";
import { CreateUserInput } from "./dto/create-user.input";
import { SetUserPasswordInput } from "./dto/setpassword-user.input";
import { UpdateUserInput } from "./dto/update-user.input";
import { User } from "./user.entity";
import { UserService } from "./user.service";
import { PubSub } from "graphql-subscriptions";
const pubSub = new PubSub();
@Resolver(() => User)
export class UserResolver {
constructor(private readonly userService: UserService) {}
@Subscription(() => User)
userAdded() {
return pubSub.asyncIterableIterator<User>("userAdded");
}
@Mutation(() => User)
async CreateUser(
@Args("createUserInput") createUserInput: CreateUserInput,
): Promise<User> {
const user = await this.userService.create(createUserInput);
pubSub.publish("userAdded", { userAdded: user });
return user;
}
@Mutation(() => User)
async updateUser(
@Args("updateUserInput") updateUserInput: UpdateUserInput,
) {
return await this.userService.update(updateUserInput);
}
@Mutation(() => User)
async setPassword(
@Args("setUserPasswordInput")
setUserPasswordInput: SetUserPasswordInput,
) {
return await this.userService.setPassword(setUserPasswordInput);
}
@Query(() => [User])
async users() {
return await this.userService.findAll();
}
}

947
yarn.lock

File diff suppressed because it is too large Load Diff