簡體   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