import * as yup from "yup" ; import { ResolverMap } from "../../../types/graphql-utils" ; import { User } from "../../../entity/User" ; import { formatYupError } from "../../../utils/formatYupError" ; import { duplicateEmail , emailNotLongEnough , invalidEmail } from "./errorMessages" ; import { registerPasswordValidation } from "../../../yupSchemas" ; const schema = yup . object ( ) . shape ( { email : yup . string ( ) . min ( 3 , emailNotLongEnough ) . max ( 255 ) . email ( invalidEmail ) , password : registerPasswordValidation } ) ; export const resolvers = { Mutation : { register : async ( _ , args ) => { try { await schema . validate ( args , { abortEarly : false } ) ; } catch ( err ) { return formatYupError ( err ) ; } const { email , password } = args ; const userAlreadyExists = await User . findOne ( { where : { email } , select : [ "id" ] } ) ; if ( userAlreadyExists ) { return [ { path : "email" , message : duplicateEmail } ] ; } const user = User . create ( { email , password } ) ; await user . save ( ) ; return null ; } } } ;