【问题标题】:How to get an array of input type inside the resolver in GraphQL如何在 GraphQL 的解析器中获取输入类型的数组
【发布时间】:2020-10-12 05:12:56
【问题描述】:

我想从查询变量中获取一个字符串数组作为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);
}

知道为什么会这样吗?

【问题讨论】:

    标签: typescript rest graphql nestjs graphql-js


    【解决方案1】:

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

    @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

    【讨论】:

    • 我很想用一组对象来实现模拟。您知道如何实现这一目标吗?
    • 你应该改变你的解析器来获得像@Args({ name: &lt;NAME_OF_YOUR_OBJECT_VARIABLE&gt;, type: () =&gt; [&lt;YOUR_OBJECT_INPUT_TYPE_AS_A_GRAPHQL_TYPE] }这样的参数
    猜你喜欢
    • 2018-06-16
    • 2020-07-30
    • 1970-01-01
    • 2017-08-13
    • 2017-12-18
    • 1970-01-01
    • 2018-06-08
    • 2021-01-23
    相关资源
    最近更新 更多