繁体   English   中英

如何获取数组作为 GraphQL 解析器的输入

[英]How to get an array as an input for a GraphQL resolver

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

人.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);
}

将 go 归功于 @vinujan.s

更新

[我的答案是针对那些希望使用type-graphQL而不是 NextJS/graphQL 实现相同目标的人]

对于 NextJS/graphQL,这里的答案仍然正确。

在最新版本中,returnTypeFunc & name 是从 object 中提取出来的。

所以,我们上面的例子变成了——

@UseGuards(GraphqlAuthGuard)
@Mutation(() => String)
async deletePeople(@Args('ids', () => [String]) ids: String[]) : Promise<String> {
    const result = await this.peopleService.deletePeople(ids);
    return JSON.stringify(result);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM