typescript - 枚举 $EnumName 不能表示非枚举值:“$EnumValue”。您的意思是枚举值“$EnumValue”吗
问题描述
我正在尝试在我的玩具 GraphQL API 的解析器上创建一个可选过滤器,它使用type-graphql@1.0.0-rc.3
和class-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;
}
}
解决方案
GraphQL 中的双引号表示字符串文字。如果您的查询采用枚举值,则应省略引号:
{
authors(
field: "name",
op: CONTAINS,
value: "Jones"
) {
id,
name
}
}
推荐阅读
- php - Laravel Sai 无法从主机连接到数据库
- javascript - React Native flatlist 多次渲染
- html - Flex 容器可单击,左侧为图像,右侧为文本居中
- nginx - 更改 Nginx 根目录时出现 403 GIven
- django - 使用 Docker 将 Django 代码部署到没有 ECS 的 AWS EC2 实例
- django - 在 Django Api 上部署
- c++ - 将函数参数更改为全局变量会导致意外结果
- javascript - 在反应js中显示当月所有剩余天数
- optimization - 计算 [1..N] 中前导 1 以下 K 个零位的整数?(没有 HW POPCNT 的连续范围的 popcount)
- vue.js - 从 Vue Router 导航栏生成一个完全空白的页面