When I call my resolver to return a union type (either a User
or an object with a message
key and value of type String
, such as my UserNotFoundError
type), it always comes back with
"__typename": "User"
. Why is this happening and how can I fix it?
Resolver:
async validateUser(
_: undefined,
{ email, password }: { email: string; password: string }
) {
const user = await prisma.user.findUnique({
where: {
email,
},
});
console.log('user before ', user);
if (!user || user.password !== password) {
const err = { message: 'Invalid user or password' };
return err;
}
return user;
},
typeDefs
type User {
email: String!
password: String
id: ID!
}
type UserNotFoundError {
message: String!
}
union UserOrError = User | UserNotFoundError
type Query {
allUsers: [User!]!
userByEmail(email: String!): User!
validateUser(email: String!, password: String!): User
}
When testing in Apollo Studio/Explorer, regardless of the actual return from the resolver:
{
"data": {
"validateUser": {
"__typename": "User"
}
}
}