I have an issue with my typegraphql code. I couldn't find what is causes this error message. The code that I shared below is my resolver and mutation which should return a 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 }
}
}
Below is the ProductResponse class for standardizing the response of all mutations and queries:
ObjectType()
export class ProductResponse {
@Field(returns => String, { nullable: true })
errorMessage?: string
@Field(returns => Product, { nullable: true })
product?: Product
}
And this is my service which is returns saved data from DB or throws an error:
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
}
But when I run this code I am encountering an error message below. What am I doing wrong?
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?