我的 typegraphql 代码有问题。我找不到导致此错误消息的原因。我在下面分享的代码是我的解析器和突变,它应该返回一个ProductResponse:
@Mutation(returns => ProductResponse)
async addProduct (@Args() { model, title, description, price, images, productCode }: productData):
Promise<ProductResponse> {
try {
const productInstance = new ProductService()
const resp = await productInstance.add(model, title, description, price, images, productCode)
return { product: resp }
} catch (err: any) {
return { errorMessage: err.message }
}
}
下面是用于标准化所有突变和查询响应的ProductResponse类:
ObjectType()
export class ProductResponse {
@Field(returns => String, { nullable: true })
errorMessage?: string
@Field(returns => Product, { nullable: true })
product?: Product
}
这是我的服务,它从数据库返回保存的数据或引发错误:
async add (model: string, title: string, description: string, price: number, images: string, productCode: string) {
const product = await Product.findOne({ where: { productCode } })
if (product) {
throw new Error("Duplicate product")
}
const newProduct = await Product.create({ model, title, description, price, images, productCode }).save()
return newProduct
}
但是当我运行此代码时,我在下面遇到错误消息。我究竟做错了什么?
UnhandledPromiseRejectionWarning: Error: Cannot determine GraphQL output type for 'addProduct' of 'ProductResolver' class. Is the value, that is used as its TS type or explicit type, decorated with a proper decorator, or is it a proper output value?