milos
10/06/2020, 1:42 PMRyan
10/06/2020, 2:43 PMschema.validate()
. This will automatically throw the error which will be sent in the GraphQL response.
I don’t currently have any example but I could create one.Philip
10/06/2020, 3:37 PMmilos
10/06/2020, 4:00 PMRyan
10/08/2020, 7:25 AMmodel User {
id Int @id @default(autoincrement())
name String?
email String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
And this is my yup vaildator:
const validators = {
createUser: yup.object().shape({
name: yup.string(),
email: yup.string().email(),
}),
}
Finally my resolver to create a user:
t.field('createUser', {
type: 'User',
args: {
name: ns.stringArg(),
email: ns.stringArg({ required: true }),
},
async resolve(_root, args, ctx) {
await validators.createUser.validate(args)
return ctx.prisma.user.create({ data: args })
},
})
This will validate and send the error as follow if I type an incorrect email: