简体   繁体   English

如何在 Nestjs + Graphql Mutation Args 中定义 ID 类型?

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

I am searching one things.我正在寻找一件事。 Actually I am building a graphql api with nestjs .实际上我正在用nestjs构建一个graphql api Here I want to ID type in @Args Input.在这里,我想在@Args输入中输入 ID。

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 .在这里,我从@nestjs/graphql导入ID类型。 And add type as id .并将类型添加为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 .然后我尝试添加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.然后它解决typescript错误。 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 .但是当我给String It works perfectly.它完美地工作。 But I need to add ID .但我需要添加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.这里我需要 ID 类型 如何从这里定义 ID 类型。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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