繁体   English   中英

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

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

我正在寻找一件事。 实际上我正在用nestjs构建一个graphql api 在这里,我想在@Args输入中输入 ID。

这是示例-

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

在这里,我从@nestjs/graphql导入ID类型。 并将类型添加为id 但是我遇到了一个错误,例如-

'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'

然后我尝试添加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);
}

然后它解决typescript错误。 但是在启动服务器时我也会在控制台中遇到错误-

在此处输入图像描述

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

但是当我给String 它完美地工作。 但我需要添加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);
}

这里我需要 ID 类型 如何从这里定义 ID 类型。

这是正确的方法:

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