首页 > 解决方案 > 枚举 $EnumName 不能表示非枚举值:“$EnumValue”。您的意思是枚举值“$EnumValue”吗

问题描述

我正在尝试在我的玩具 GraphQL API 的解析器上创建一个可选过滤器,它使用type-graphql@1.0.0-rc.3class-validator@0.12.0. 当我运行看似矛盾的查询时,我收到此错误消息。我非常密切地遵循文档中的枚举示例,但我确定我遗漏了一些东西。

这是查询:

{
  authors(
    field: "name",
    op: "CONTAINS",
    value: "Jones"
  ) {
    id,
    name
  }
}

结果:

{
  "error": {
    "errors": [
      {
        "message": "Enum \"FilterType\" cannot represent non-enum value: \"CONTAINS\". Did you mean the enum value \"CONTAINS\"?"
      }
    ]
  }
}

和枚举

enum FilterType {
    EQ = "=",
    // . . .
    CONTAINS = "CONTAINS",
    IN = "in"
}

registerEnumType(FilterType, {
    name: "FilterType",
    description: "Type of comparison operation to perform during the filter",
});

export {
    FilterType
}

还有我的解析器/ArgsType。


@ArgsType()
class FilterAuthorArgs {
    @Field(() => String, {nullable: true})
    field?: string;

    @Field(() => FilterType, {nullable: true})
    op?: FilterType;

    @Field(() => String, {nullable: true})
    value?: string;

    // . . .

    get _field(): string|undefined {
        this.validate();
        return this.field
    }

    get _op(): FilterType|undefined {
        this.validate();
        return this.op
    }

    get _value(): string|undefined {
        this.validate();
        return this.value
    }
}

@Resolver()
export class AuthorResolver {
    @Query(()=> [Author])
    authors(@Args() {_field, _op, _value}: FilterAuthorArgs) {
        // use filters
        return authors;
    }
}

标签: typescriptenumsgraphqltypegraphql

解决方案


GraphQL 中的双引号表示字符串文字。如果您的查询采用枚举值,则应省略引号:

{
  authors(
    field: "name",
    op: CONTAINS,
    value: "Jones"
  ) {
    id,
    name
  }
}

推荐阅读