【发布时间】:2020-12-19 03:42:45
【问题描述】:
我已经阅读了多篇关于处理错误的文章,尤其是https://blog.logrocket.com/handling-graphql-errors-like-a-champ-with-unions-and-interfaces/。虽然,我无法理解为什么我无法在我的代码中使用联合,或者需要什么方法来实现它。
联合原因
我希望返回自定义错误。例如,如果用户电子邮件已经存在,则返回错误消息,例如
{ message: "Email already exists", key: "EMAIL_EXISTS" }
类型
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: GraphQLInt },
email: { type: GraphQLString }
})
});
const AuthType = new GraphQLObjectType({
name: 'Auth',
fields: () => ({
token: { type: GraphQLString },
user: { type: UserType }
})
});
const UserNotFound = new GraphQLObjectType({
name: 'UserNotFound',
fields: () => ({
message: { type: GraphQLString },
key: { type: GraphQLString }
})
});
union AuthUserType = AuthType | UserNotFound
module.exports = {
UserType,
AuthType,
AuthUserType
};
为了完成这项工作,我需要做些什么特别的事情吗?
感谢您在这方面提供的所有帮助以及如何实施联合。
【问题讨论】: