在express-graphql中返回用户可读的模式类型验证

时间:2018-06-11 18:22:26

标签: validation error-handling graphql graphql-js express-graphql

我正在尝试使用突变发布某些内容( express-graphql )中的验证错误消息(错误消息接近使用类似rails的activerecord)。

errors: {
  email: ["can't be blank", "Should be a valid email."],
  name: ["can't be blank"],
  username:["can't be blank"],
}

这只是为了让您了解我想要获得的内容,但验证失败。应该返回上述错误的示例变异可以是:

mutation{
 AddUser(data: {
  email: "InvalidEmail"
 })
}

目前graphql只返回一般消息以简化调试,如下所示:

{"errors": [{
   "message": "Expected type Email, found \"Item 1\".",
      "locations": [{
          ...
      }]
    }
]}

我的graphql用户架构类型如下所示:

const userInputType = new GraphQLInputObjectType({
 name: 'UserInput',
 description: 'Insert User',
 fields: () => ({
   // all working well
   name: { type: new GraphQLNonNull(GraphQLString) },
   lastName: { type: GraphQLString },
   email: { type: new GraphQLNonNull(GraphQLEmail) },
   username: { type: GraphQLString },
 }),
});

我一直在寻找一种在变异中执行此操作的方法,但即使在执行突变内部的验证之前,也会抛出错误。

有可能达到这个目的吗?

0 个答案:

没有答案