[英]Prisma with GraphQL - how to provide an explicit type for a specified array of strings - unique naming issue
I am trying to figure out how to specify an explicit type for a field, that is defined in a prisma schema as having an enum value.我试图弄清楚如何为字段指定显式类型,该类型在 prisma 模式中定义为具有枚举值。
In my schema I have:在我的架构中,我有:
enum Category {
CHANGE
OPTION
OTHER
}
and a model that uses Category as follows:以及使用 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[]
}
Then in the back end, I have a model:然后在后端,我有一个模型:
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
}
and an input file that both define 'category', I think I'm providing the right type, by referencing the Prisma enum (not sure if instead I'm supposed to reference the generated types).和一个定义“类别”的输入文件,我认为我通过引用 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
}
When I try to run yarn dev with this, I get an error that says:当我尝试使用它运行 yarn dev 时,我收到一条错误消息:
NoExplicitTypeError: Unable to infer GraphQL type from TypeScript reflection system. NoExplicitTypeError:无法从 TypeScript 反射系统推断 GraphQL 类型。 You need to provide explicit type for 'category' of 'IssueInput' class您需要为“IssueInput”类的“类别”提供显式类型
How can I figure out how to give a type to category in the IssueInput class?如何确定如何在 IssueInput 类中为类别指定类型? I can't find an example of how to do this.我找不到如何执行此操作的示例。
I tried adding: registerEnumType to both the IssueInput definition, and the Issue Model, using the idea set out below:我尝试使用以下想法将:registerEnumType 添加到 IssueInput 定义和问题模型中:
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
}
I still get an error saying:我仍然收到一条错误消息:
NoExplicitTypeError: Unable to infer GraphQL type from TypeScript reflection system. NoExplicitTypeError:无法从 TypeScript 反射系统推断 GraphQL 类型。 You need to provide explicit type for 'category' of 'IssueInput' class.您需要为“IssueInput”类的“类别”提供显式类型。 at Object.findType在 Object.findType
When I try it like below, I get a console error that says Error: Schema must contain uniquely named types but contains multiple types named "Category".当我像下面这样尝试时,我收到一个控制台错误,上面写着错误:架构必须包含唯一命名的类型,但包含多个名为“类别”的类型。 I only have one Category in the schema file.我在架构文件中只有一个类别。
Within my terminal, the error is expressed differently, it says:在我的终端中,错误的表达方式不同,它说:
Property 'category' in type 'IssueInput' is not assignable to the same property in base type 'Partial'. 'IssueInput' 类型中的属性 'category' 不能分配给基本类型 'Partial' 中的相同属性。 Type 'Category' is not assignable to type 'Category |类型“类别”不可分配给类型“类别 | undefined'.不明确的'。 Type '"CHANGE"' is not assignable to type 'Category |类型 '"CHANGE"' 不可分配给类型 '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
}
You need to register the enum with type-graphql.您需要使用 type-graphql 注册枚举。
import { registerEnumType } from "type-graphql";
registerEnumType(Prisma.Category, {
name: "Category", // this one is mandatory
description: "Issue Category", // this one is optional
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.