MichalLytek 给了我以下对我有帮助的链接:
https://typegraphql.com/docs/custom-decorators.html
我在此处将该链接与 typegraphql-prisma 说明结合使用:
https://prisma.typegraphql.com/docs/advanced/additional-decorators
在buildSchema 之前拨打applyResolversEnhanceMap(resolversEnhanceMap); 之类的电话就成功了。然后我将连接的PrismaClient 传递给装饰器函数,以检查 ORM 模型中的内容,例如我的“问题”模型:
import { validate, ValidationError } from "class-validator";
import {
ClassType,
ArgumentValidationError,
createMethodDecorator,
} from "type-graphql";
import { PrismaClient } from "@prisma/client";
// sample implementation of custom validation decorator
// this example use `class-validator` however you can plug-in `joi` or any other lib
export function ValidateDeleteIssueOwnership<T extends object>(
Type: ClassType<T>,
prisma: PrismaClient
) {
return createMethodDecorator(async ({ args, context, info }, next) => {
const instance = Object.assign(new Type(), args);
console.log(" instance: ", instance);
// console.log(' context: ', context);
// console.log(' info: ', info);
// const validationErrors = await validate(instance);
// if (validationErrors.length > 0) {
// throw new ArgumentValidationError(validationErrors);
// }
const numberIssues = await prisma.issue.count();
if (numberIssues === 0) {
throw new Error(
"CUSTOM DECORATOR - Oh Nos!!! There were no issues to delete! "
);
}
return next();
});
}