如何在 GraphQL 中的解析器中获取输入类型的数组 [英] How to get an array of input type inside the resolver in GraphQL

查看:13
本文介绍了如何在 GraphQL 中的解析器中获取输入类型的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get an array of strings as ids parameter from query variables and use inside my resolver. Below here is my code.

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

但是,我收到以下错误,

However, I am getting the following error,

[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.


我也尝试了以下变体,


I also tried the following variations,

@Args('ids', () => string[]) ids: string[]

@Args('ids', () => String[]) ids: String[]

@Args('ids', () => [String]) ids: String[]

@Args('ids', { type: () => String[] }) ids: String[]


但是如果我要像下面那样更改我的突变以采用单个字符串,它就可以工作.


But if I am to change my mutation like below to take a single string it works.

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

知道为什么会这样吗?

推荐答案

当输入类型参数定义为,

This works when the input type argument is defined as,

@Args({ name: 'ids', type: () => [String] }) ids: String[]

下面是突变解析器的样子:

Below is how the mutation resolver looks like:

@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

Credits go to @vinujan.s

这篇关于如何在 GraphQL 中的解析器中获取输入类型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆