gpt4 book ai didi

typescript - 如何在 GraphQL 中的解析器中获取输入类型的数组

转载 作者:行者123 更新时间:2023-12-03 14:14:25 28 4
gpt4 key购买 nike

我想从查询变量中获取一个字符串数组作为 ids 参数并在我的解析器中使用。下面是我的代码。
People.resolver.ts

import {
Resolver, Query, Mutation, Args,
} from '@nestjs/graphql';
import { People } from './People.entity';
import { PeopleService } from './People.service';

@Resolver(() => People)
export class PeopleResolver {
constructor(private readonly peopleService: PeopleService) { }

@Mutation(() => String)
async deletePeople(@Args('ids') ids: string[]) : Promise<String> {
const result = await this.peopleService.deletePeople(ids);
return JSON.stringify(result);
}
}
但是,我收到以下错误,
[Nest] 8247   - 06/22/2020, 6:32:53 PM   [RouterExplorer] Mapped {/run-migrations, POST} route +1ms
(node:8247) UnhandledPromiseRejectionWarning: Error: You need to provide explicit type for PeopleResolver#deletePeople parameter #0 !
at Object.findType (/Users/eranga/Documents/Project/node_modules/type-graphql/dist/helpers/findType.js:17:15)
at Object.getParamInfo (/Users/eranga/Documents/Project/node_modules/type-graphql/dist/helpers/params.js:9:49)
at /Users/eranga/Documents/Project/node_modules/type-graphql/dist/decorators/Arg.js:9:159
at /Users/eranga/Documents/Project/node_modules/@nestjs/graphql/dist/decorators/args.decorator.js:34:113
at /Users/eranga/Documents/Project/node_modules/@nestjs/graphql/dist/storages/lazy-metadata.storage.js:11:36
at Array.forEach (<anonymous>)
at LazyMetadataStorageHost.load (/Users/eranga/Documents/Project/node_modules/@nestjs/graphql/dist/storages/lazy-metadata.storage.js:11:22)
at GraphQLSchemaBuilder.<anonymous> (/Users/eranga/Documents/Project/node_modules/@nestjs/graphql/dist/graphql-schema-builder.js:31:57)
at Generator.next (<anonymous>)
at /Users/eranga/Documents/Project/node_modules/@nestjs/graphql/dist/graphql-schema-builder.js:17:71
(node:8247) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:8247) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我还尝试了以下变体,
@Args('ids', () => string[]) ids: string[]
@Args('ids', () => String[]) ids: String[]
@Args('ids', () => [String]) ids: String[]
@Args('ids', { type: () => String[] }) ids: String[]
但是,如果我要像下面这样更改我的突变以获取单个字符串,它就可以工作。
@Mutation(() => String)
async deletePeople(@Args('id') id: string) : Promise<String> {
const result = await this.peopleService.deletePeople([id]);
return JSON.stringify(result);
}
知道为什么会这样吗?

最佳答案

当输入类型参数定义为时,这有效,

@Args({ name: 'ids', type: () => [String] }) ids: String[]
下面是突变解析器的样子:
@UseGuards(GraphqlAuthGuard)
@Mutation(() => String)
async deletePeople(@Args({ name: 'ids', type: () => [String] }) ids: String[]) : Promise<String> {
const result = await this.peopleService.deletePeople(ids);
return JSON.stringify(result);
}
学分转到@vinujan.s

关于typescript - 如何在 GraphQL 中的解析器中获取输入类型的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62512778/

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