My query variables contain an array of strings that I need to use as the ids
parameter inside my resolver. Below is the relevant code snippet.
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, when I try to execute the code, I encounter the following error message,
[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 have tried various alternatives like,
@Args('ids', () => string[]) ids: string[]
@Args('ids', () => String[]) ids: String[]
@Args('ids', () => [String]) ids: String[]
@Args('ids', { type: () => String[] }) ids: String[]
Interestingly, changing the mutation to accept a single string input works as expected.
@Mutation(() => String)
async deletePeople(@Args('id') id: string) : Promise<String> {
const result = await this.peopleService.deletePeople([id]);
return JSON.stringify(result);
}
Does anyone understand why this problem occurs?