简体   繁体   中英

How to define ID type in Nestjs + Graphql Mutation Args?

I am searching one things. Actually I am building a graphql api with nestjs . Here I want to ID type in @Args Input.

Here is example-

import { Resolver, Mutation, Query, Args, Context, ID } from "@nestjs/graphql";

@Mutation(() => SuccessInfo, { name: "addSubCategory" })
addSub(
        @Args("id") id: ID  // Here is type ID
    ) {
    console.log(id);
}

Here I import ID type from @nestjs/graphql . And add type as id . But I getting one error like-

'ID' refers to a value, but is being used as a type here. Did you mean 'typeof ID'?

Parameter 'id' of public method from exported class has or is using private name 'ID'

Then I try to add typeof ID .

import { Resolver, Mutation, Query, Args, Context, ID } from "@nestjs/graphql";

@Mutation(() => SuccessInfo, { name: "addSubCategory" })
addSub(
        @Args("id") id: typeof ID  // Here is type ID
    ) {
    console.log(id);
}

Then It solve typescript error. But I also get error in console when starting the server-

在此处输入图像描述

Undefined type error. Make sure you are providing an explicit type for the "addSub"

But When I give String . It works perfectly. But I need to add ID .

import { Resolver, Mutation, Query, Args, Context, ID } from "@nestjs/graphql";

@Mutation(() => SuccessInfo, { name: "addSubCategory" })
addSub(
        @Args("id") id: String  // Here is type ID
    ) {
    console.log(id);
}

Here I need ID type How can I define ID type from here.

Here is the correct way to do it:

import { Resolver, Mutation, Query, Args, Context, ID } from "@nestjs/graphql";

@Mutation(() => SuccessInfo, { name: "addSubCategory" })
addSub(
    @Args("id", { type: () => ID } ) id: string  // Notice it's string not String
) {
    console.log(id);
    return { status: true } // return anything here which matches the SuccessInfo type
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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