在Typescript中使用TypeGraphql创建了一个项目。试图使一个字段成为具有字符串值的枚举。如果我将这些值或名称发送到突变端点,则会抛出错误:
"errors": [
{
"message": "Expected type Tipe!, found \"Contract\"; Did you mean the enum value Contract?",
"locations": [
{
DTO之后:
import { Tipe } from "../validators/enums/ContactEnums";
@ObjectType()
@Entity()
export class Contact extends BaseEntity {
@Field(() => ID)
@PrimaryColumn("uuid")
id: string;
@Field()
@Column("enum", { enum: Tipe })
tipe: Tipe;
枚举类型:
export enum Tipe {
"Contract" = "CONTRACT",
"Person" = "PERSON"
}
输入类型:
@InputType()
export class ContactInput {
@Field()
id: string;
@Field(() => Tipe)
tipe: string;
答案 0 :(得分:0)
如果要将枚举值作为字符串发送,则必须使用变量:
query SampleQuery($sampleArg: Tipe!) {
sample(sampleArg: $sampleArg) {
field
}
}
{
sampleArg: "Contract"
}
如果将查询设为静态并提供内联的枚举,则不能将其用引号引起来:
query {
sample(sampleArg: Contract) {
field
}
}