【问题标题】:Enum $EnumName cannot represent non-enum value: "$EnumValue". Did you mean the enum value "$EnumValue"枚举 $EnumName 不能表示非枚举值:“$EnumValue”。您的意思是枚举值“$EnumValue”吗
【发布时间】:2020-11-11 09:17:57
【问题描述】:

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

这里是查询:

{
  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;
    }
}

【问题讨论】:

    标签: typescript enums graphql typegraphql


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 2011-10-27
      相关资源
      最近更新 更多