thanks for looking into this. So I am trying to migrate my Apollo Server from V3 to V4, I have a resolver that type checks for an Access Token and returns it like so
export class LoginResolver {
@Mutation(() => AccessToken)
async login(
@Arg("email") email: string,
@Arg("password") password: string,
@Ctx() { prisma, res }: ProjectContext
): Promise<AccessToken> {
// check if user exists
const user = await prisma.user.findFirst({
where: {
email,
},
});
if (!user) {
throw new GraphQLError("No user found");
}
const valid = await verify(user.password, password);
if (!valid) {
throw new GraphQLError("Invalid Password");
}
// the user logged in successully
res.cookie("*****", createRefreshToken(user), { httpOnly: true });
const accessToken = createAccessToken(user);
return {
accessToken,
};
}
}
It returns the following error if the validation fails.
{
"data": {},
"error": {
"message": "JSON.parse: unexpected character at line 1 column 1 of the JSON data"
}
}
But however, if the right credentials were provided the API works just fine returning me a access token
{
"data": {
"login": {
"accessToken": "..."
}
}
}
Correct Input:
email : [email protected]
password : password
Initially in Apollo V3 I had standard throw statements that worked just fine
const user = await prisma.user.findFirst({
where: {
email,
},
});
if (!user) {
throw new Error("No user found");
}
const valid = await verify(user.password, password);
if (!valid) {
throw new Error("Invalid Password");
}
digging into Apollo server’s docs I found that in V4 Apollo Error was removed and GraphQLError from graphql took it’s place, I gave that a shot but that does not seem to fix it.
Error Message from the console :
Unexpected error processing request: TypeError: graphqlError.toJSON is not a function
TypeError: graphqlError.toJSON is not a function
at enrichError (D:ProjectsPlacementHubbackendnode_modules@apolloserversrcerrorNormalize.ts:84:30)
at D:ProjectsPlacementHubbackendnode_modules@apolloserversrcerrorNormalize.ts:46:18
at Array.map (<anonymous>)
at normalizeAndFormatErrors (D:ProjectsPlacementHubbackendnode_modules@apolloserversrcerrorNormalize.ts:39:29)
at ApolloServer.errorResponse (D:ProjectsPlacementHubbackendnode_modules@apolloserversrcApolloServer.ts:1028:73)
at ApolloServer.executeHTTPGraphQLRequest (D:ProjectsPlacementHubbackendnode_modules@apolloserversrcApolloServer.ts:1020:19)
3 Answers
I suspect that GraphQLError has a mandatory 2nd argument and Apollo isn’t handling it well if it’s omitted.
try
throw new GraphQLError('"No user found', {
extensions: {
code: 'UNKNOWN_USER',
},
});
I experienced similar problems when I switched from v3 to v4. If you want to show custom errors in the resolver, using GraphQLError is enough.
In the past, it tolerated throw new Error
usage. This behavior no longer works like it used to. You have to use GraphQLError instead. If you throw new Error like the past, your server will stop working.
Package versions that I have tested and are compatible with:
"graphql": "16.8.1",
"@apollo/server": "4.9.4",
"express": "4.18.2",
GraphQL has three different error types:
-
Syntax errors (e.g., a query was malformed)
-
Validation errors (e.g., a query included a schema field that doesn’t exist)
-
Resolver errors (e.g., an error occurred while attempting to populate
a query field)
I hope I helped you. You can review the documentation for more information. By the way, it would be beneficial for you to consider the NodeJS version separately.
throw new GraphQLError('Invalid argument value', {
extensions: {
code: 'BAD_USER_INPUT',
},
});
https://www.apollographql.com/docs/react/data/error-handling
You should use ApolloError for this.
import { ApolloError } from 'apollo-server-express';
throw new ApolloError("there is a big problem");
I did the one for Express in the above line of code. You can download other packages according to your project.
2
-
Yeahh but in V4 they have removed Apollo Error?
– Vishvaa KesavOct 28, 2022 at 13:39
-
Probably yes, apolloError removed
– Furkan GulsenOct 28, 2022 at 16:35