Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an array of input type inside the resolver in GraphQL

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

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

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

Any idea why this happens?

like image 498
Eranga Heshan Avatar asked Jun 22 '20 10:06

Eranga Heshan


People also ask

Is it possible to use inheritance with GraphQL input types?

No, the spec does not allow input types to implement interfaces. And GraphQL type system in general does not define any form of inheritance (the extends keyword adds fields to an existing type, and isn't for inheritance).


1 Answers

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

Credits go to @vinujan.s

like image 129
Eranga Heshan Avatar answered Sep 21 '22 07:09

Eranga Heshan