'how to create register mutation Graphql typescript
import { Arg, Field, ID, InputType, Mutation, ObjectType, Query, Resolver } from "type-graphql";
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
import * as bcrypt from 'bcryptjs';
@ObjectType()
class User{
@Field(() => ID)
id: number;
@Field()
username: String;
@Field()
password: String;
}
@InputType()
class UserInput {
@Field()
username: string;
@Field()
password: string;
}
@Resolver()
export class UserResolver {
@Query(() => [User])
async getAllUsers() {
return await prisma.user.findMany();
}
@Mutation(() => User)
async register(@Arg("options", () => UserInput) options: UserInput) {
let user = await prisma.user.findFirst(User.username); //error
if(user) return;
const hashedPassword = await bcrypt.hash(options.password, 10);
user = await prisma.user.create({
data: {
username: options.username, /error
password: hashedPassword
}
});
return user;
}
}
error: Type '{ username: string; password: any; }' is not assignable to type '(Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput)'. Object literal may only specify known properties, and 'username' does not exist in type '(Without<UserCreateInput, UserUncheckedCreateInput> & UserUncheckedCreateInput) | (Without<...> & UserCreateInput)'.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
