【问题标题】:typeGraphql how to change message for Field validationtypeGraphql 如何更改字段验证的消息
【发布时间】:2021-10-03 12:03:35
【问题描述】:

我正在使用带字段的 InputType:

@InputType('RegisterInput')
export default class RegisterInput {
    @Field({nullable: true})
    username: string
    @Field({nullable: true})
    password: string
    @Field({nullable: true})
    age?: number
}

当年龄不是数字时,我会收到如下错误:

UserInputError {
  message: 'Variable "$registerInput" got invalid value "text" at "registerInput.age"; Int cannot represent non-integer value: "text"',
  locations: [ { line: 1, column: 19 } ],
  path: undefined,
  extensions: { code: 'BAD_USER_INPUT' }
}

如何将消息更改为更加用户友好?我尝试的所有类型的中间件和类验证仍然首先产生此错误,然后产生任何其他错误。

如果我可以禁用它并在我的中间件中进行验证,那就更好了。

我尝试使用 graphql-middleware,但仍然只有在此验证之后才调用中间件。

【问题讨论】:

    标签: node.js graphql apollo-server typegraphql


    【解决方案1】:

    您无法轻易更改,因为它是来自 graphql-js 的标准 GraphQL 查询验证。它静态分析您的查询,并查看您何时不提供某些输入或提供错误类型的变量。

    它发生在整个 graphql 执行管道之前,中间件和其他东西开始发挥作用。

    【讨论】:

      猜你喜欢
      • 2016-03-30
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 2011-02-03
      • 2020-08-22
      • 1970-01-01
      相关资源
      最近更新 更多