无法在GraphQL中实现联合
我已经阅读了多篇关于处理错误的文章,尤其是无法在GraphQL中实现联合,graphql,express-graphql,Graphql,Express Graphql,我已经阅读了多篇关于处理错误的文章,尤其是https://blog.logrocket.com/handling-graphql-errors-like-a-champ-with-unions-and-interfaces/。尽管如此,我无法理解为什么我不能在代码中使用联合,或者需要什么方法来实现它 联合的理由 我希望返回一个自定义错误。例如,如果用户电子邮件已经存在,则返回错误消息,如 {消息:“电子邮件已存在”,键:“电子邮件已存在”} 类型 const UserType = new Gra
https://blog.logrocket.com/handling-graphql-errors-like-a-champ-with-unions-and-interfaces/
。尽管如此,我无法理解为什么我不能在代码中使用联合,或者需要什么方法来实现它
联合的理由
我希望返回一个自定义错误。例如,如果用户电子邮件已经存在,则返回错误消息,如
{消息:“电子邮件已存在”,键:“电子邮件已存在”}
类型
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
};
有什么特别的事情我需要做,以使这项工作
感谢您在这方面提供的所有帮助以及如何实现联盟。这似乎是我一直试图找到的解决方案。尽管我遇到了另一个问题,但在graphiql终端中,它的工作方式与预期的一样
const AuthResultType = new GraphQLUnionType({
name: 'AuthResult',
types: [ AuthType, AuthTypeError ],
resolveType(value) {
console.log('THE VAKLUE IS', value);
if (value.token) {
return AuthType;
} else if (value.message) {
return AuthTypeError;
}
}
});