typescript - 如何在 GraphQL 的解析器中获取输入类型的数组
问题描述
我想从查询变量中获取字符串数组作为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
推荐阅读
- java - 如何使用 JOOQ Query Builder 使用使用常量值的 select 语句构建 Insert into
- postgresql - POSGTRESQL 9.10 - 从 JSON 数组返回最大值
- java - 通过本地 java 应用程序执行 html 按钮的任务
- reactjs - 来自道具的背景图像不适用于样式化组件
- r - Rbind:将源名称添加为新组合数据集中的列和/或变量
- dart - 使用json给类添加值并在flutter中创建listview
- postgresql - 升级到想要使用校验和的 postgres 10
- unit-testing - 在 Jest 中生成 HTML coverageReport 的命令行方式
- python - 循环时慢熊猫
- android - 读取 json 中的数组