gpt4 book ai didi

graphql - 为突变 GraphQL 提供显式类型

转载 作者:行者123 更新时间:2023-12-04 07:34:34 27 4
gpt4 key购买 nike

我正在尝试创建一个接受产品列表的突变。但是这样做 GraphQL 会为 抛出错误createMultipleProducts 方法。不知道这里有什么错误。

import { Inject } from "@nestjs/common";
import { Args, Mutation, Query, Resolver } from "@nestjs/graphql";
import { ClientProxy } from "@nestjs/microservices";
import { ProductRequest } from "src/types/ms-product/product.request.type";
import { ProductResponse } from "src/types/ms-product/product.response.type";
@Resolver(of => ProductResponse)
export class ProductResolver {

constructor(
@Inject('SERVICE__PRODUCT') private readonly clientServiceProduct: ClientProxy
) {}

@Mutation(returns => ProductResponse)
async createProduct(@Args('data') product: ProductRequest): Promise<ProductResponse> {
const PATTERN = {cmd: 'ms-product-create'};
const PAYLOAD = product;
return this.clientServiceProduct.send(PATTERN, PAYLOAD)
.toPromise()
.then((response: ProductResponse) => {
return response;
})
.catch((error) => {
return error;
})
}

@Mutation(returns => [ProductResponse])
async createMultipleProducts(@Args('data') products: [ProductRequest]): Promise<Array<ProductResponse>> {
try {
const PROMISES = products.map(async (product: ProductRequest) => {
const PATTERN = {cmd: 'ms-product-create'};
const PAYLOAD = product;
return await this.clientServiceProduct.send(PATTERN, PAYLOAD).toPromise();
});

return await Promise.all(PROMISES);
} catch (error) {
throw new Error(error);
}
}

@Query(returns => ProductResponse)
async readProduct(@Args('data') id: string) {
return {}
}
}
我收到此错误:
UnhandledPromiseRejectionWarning: Error: Undefined type error. Make sure you are providing an explicit type for the "createMultipleProducts" (parameter at index [0]) of the "ProductResolver" class.

最佳答案

如果它是一个复杂的对象数组,则需要明确告知 GraphQL 参数的类型。

@Mutation(returns => [ProductResponse])
async createMultipleProducts(@Args({ name: 'data', type: () => [ProductRequest] }) products: ProductRequest[]): Promise<Array<ProductResponse>> {
...
}

关于graphql - 为突变 GraphQL 提供显式类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67798146/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com