gpt4 book ai didi

typescript - Prisma with GraphQL - 如何为指定的字符串数组提供显式类型 - 唯一命名问题

转载 作者:行者123 更新时间:2023-12-05 05:39:45 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何为一个字段指定显式类型,该字段在 Prisma 架构中定义为具有枚举值。

在我的模式中我有:

enum Category {
CHANGE
OPTION
OTHER
}

和一个使用 Category 的模型如下:

model Issue {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
title String
description String
category Category
Template Template[]
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @default(now()) @updatedAt @db.Timestamptz(6)
User User[]
}

然后在后端,我有一个模型:

import * as Prisma from "@prisma/client"

import { Field, ObjectType } from 'type-graphql'
import { BaseModel } from "../shared/base.model"
// - I tried this but it didn't help: import { Category } from "@generated"



@ObjectType()
export class Issue extends BaseModel implements Prisma.Issue {

@Field()
title: string

@Field()
description: string

@Field(() => Category)
category: Prisma.Category


}

和一个都定义“类别”的输入文件,我认为我通过引用 Prisma 枚举提供了正确的类型(不确定我是否应该引用生成的类型)。

import { IsNotEmpty } from "class-validator"
import { Field, InputType } from "type-graphql"
import { Issue } from '../issue.model'
import * as Prisma from "@prisma/client"


@InputType()
export class IssueInput implements Partial<Issue> {
@Field()
@IsNotEmpty()
title: string

@Field()
@IsNotEmpty()
description: string

@Field()
@IsNotEmpty()
category: Prisma.Category

@Field()
@IsNotEmpty()
userId: string



}

当我尝试用它运行 yarn dev 时,我收到一条错误消息:

NoExplicitTypeError: Unable to infer GraphQL type from TypeScriptreflection system. You need to provide explicit type for 'category' of'IssueInput' class

如何确定如何为 IssueInput 类中的类别指定类型?我找不到如何执行此操作的示例。

我尝试添加:registerEnumType 到 IssueInput 定义和 Issue 模型,使用下面列出的想法:

import { IsNotEmpty } from "class-validator"
import { Field, InputType, registerEnumType } from "type-graphql"
import { Issue } from '../issue.model'
import * as Prisma from "@prisma/client"


registerEnumType(Prisma.Category, {
name: "Category", // this one is mandatory
description: "Issue Category", // this one is optional
});


@InputType()
export class IssueInput implements Partial<Issue> {
@Field()
@IsNotEmpty()
title: string

@Field()
@IsNotEmpty()
description: string

@Field()
@IsNotEmpty()
category: Prisma.Category

@Field()
@IsNotEmpty()
userId: string



}

我仍然收到一条错误消息:

NoExplicitTypeError: Unable to infer GraphQL type from TypeScriptreflection system. You need to provide explicit type for 'category' of'IssueInput' class.at Object.findType

当我像下面这样尝试时,我收到一个控制台错误,显示错误:架构必须包含唯一命名的类型,但包含多个名为“类别”的类型。我在架构文件中只有一个类别。

在我的终端中,错误的表达方式不同,它说:

Property 'category' in type 'IssueInput' is not assignable to the sameproperty in base type 'Partial'. Type 'Category' is notassignable to type 'Category | undefined'.Type '"CHANGE"' is not assignable to type 'Category | undefined'.

import { IsNotEmpty } from "class-validator"
import { Field, InputType, registerEnumType } from "type-graphql"
import { Issue } from '../issue.model'
import * as Prisma from "@prisma/client"


registerEnumType(Prisma.Category, {
name: "Category", // this one is mandatory
description: "Issue Category", // this one is optional
});


@InputType()
export class IssueInput implements Partial<Issue> {
@Field()
@IsNotEmpty()
title: string

@Field()
@IsNotEmpty()
description: string

@Field(type => Prisma.Category)
@IsNotEmpty()
category: Prisma.Category
// category: Prisma.Category | undefined -- this also does not solve the problem


@Field()
@IsNotEmpty()
userId: string



}

最佳答案

您需要使用 type-graphql 注册枚举。

import { registerEnumType } from "type-graphql";

registerEnumType(Prisma.Category, {
name: "Category", // this one is mandatory
description: "Issue Category", // this one is optional
});

关于typescript - Prisma with GraphQL - 如何为指定的字符串数组提供显式类型 - 唯一命名问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72596799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com