I'm trying to return Errors from my resolvers when something is going wrong. An example below. But I'm getting errors from the Typescript compiler.
0] Types of property 'me' are incompatible.
[0] Type '(_: {}, __: {}, context: ExtendedContext) => Promise<AuthenticationError | User>' is not assignable to type 'ResolverFn<Maybe<ResolverTypeWrapper<User>>, {}, ExtendedContext, {}> | ResolverWithResolve<Maybe<ResolverTypeWrapper<User>>, {}, ExtendedContext, {}> | undefined'.
[0] Type '(_: {}, __: {}, context: ExtendedContext) => Promise<AuthenticationError | User>' is not assignable to type 'ResolverFn<Maybe<ResolverTypeWrapper<User>>, {}, ExtendedContext, {}>'.
[0] Type 'Promise<AuthenticationError | User>' is not assignable to type 'User | Promise<User> | Promise<Maybe<ResolverTypeWrapper<User>>> | null'.
[0] Type 'Promise<AuthenticationError | User>' is not assignable to type 'Promise<User>'.
[0] Type 'AuthenticationError | User' is not assignable to type 'User'.
[0] Type 'AuthenticationError' has no properties in common with type 'User'.
What is the correct way to extend these generated resolver types from graphql-codegen in order to be able return errors that I'm recieving.
export const resolvers: Resolvers<ExtendedContext> = {
Query: {
me: async (_, __, context) => {
if (!tokenIsvalid(context.userData)) {
return new AuthenticationError("Invalid token"); <---- This is causing errors
}
const user = await getRepository(User).findOneOrFail(
context.userData.userId
);
return user;
},
},
}