-
Notifications
You must be signed in to change notification settings - Fork 575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom error #167
Comments
Can you describe your desired behaviour a bit more in detail by (1) showing the client code you'd like to use and (2) the GraphQL error response you'd like to see from a client's perspective? |
@schickling sure! // really basic custom error and auth resolver
type ErrorValues = { email?: string; password?: string };
class AuthError extends Error {
public errorValues: ErrorValues;
constructor(values: ErrorValues) {
super();
Object.setPrototypeOf(this, AuthError.prototype);
this.errorValues = values;
}
}
export const authResolvers = {
async login(parent, { email, password }, ctx: Context, info) {
const user = await ctx.db.query.user({ where: { email } });
if (!user) {
throw new AuthError({ email: Errors.LOGIN_EMAIL });
}
const valid = await compare(password, user.password);
if (!valid) {
throw new AuthError({ password: Errors.LOGIN_PASSWORD });
}
return {
token: sign({ userId: user.id }, process.env.APP_SECRET),
user
};
}
}; then on the client-side: // using apollo-client
const LOGIN_MUTATION = gql`
mutation Login($email: String!, $password: String!) {
login(email: $email, password: $password) {
token
}
}
`;
// ... standard sorta login component
private onSubmit = async (variables: FormValues, { setErrors }: FormikProps<FormValues>) => {
const { history, mutate } = this.props;
try {
const result = await mutate({
variables
});
const { data: { login: { token } } } = result;
login(token);
history.replace('/');
} catch (error) {
console.log(error); // <-- right here it would be nice to be able to access custom AuthError values
}
}; I know there are a few ways for that error to show up and apollo-client |
@stephenlaughton can you setup a little launchpad with your example? So it's easier to test, try etc 😉 (not really have the time to setup an env today ^^) |
I'm looking for the same functionality. Specifically:
Let me know if i can help! |
Looking for the same functionality. For instance only the message passed by the Error constructor is send to the client. For example a server function would want to send :
|
How can I use ApolloError from graphql-yoga? From above link I can easily create custom error like this - But how can I get reference to |
Looks like graphql-yoga doesn't export anything related with apollo-graphql. |
Installing the https://unpkg.com/[email protected]/dist/exports.d.ts export {
ApolloError,
toApolloError,
SyntaxError,
ValidationError,
AuthenticationError,
ForbiddenError,
UserInputError,
gql
} from 'apollo-server-core' |
Hello, It would be convenient to expose these errors from graphql-yoga, as soon as you need internationalization or your app is in another language they're required. |
I read this article about Error Handling using |
Due to inactivity of this issue we have marked it |
Hey 👋, It seems like this issue has been inactive for some time. In need for maintaining clear overview of the issues concerning the latest version of |
Any News about custom error in graphql-yoga? |
Boink |
Is there a specific reason not to import apollo-errors in graphql-yoga? or is there alternatives to not use apollo-errors? |
Even though graphql-yoga doesnt expose error class we can manually install apollo-server-core and import error instance from it and use it like this Install apollo-server-core yarn add apollo-server-core import it const {AuthenticationError} = require("apollo-server-core"); throw error if(!user) {
throw new AuthenticationError("user not authenticated")
} if anyone wants to read more |
https://dev.to/andre/handling-errors-in-graphql--2ea3 best article to solve this. |
Is it possible to throw custom Errors? I have defined a custom Error class, but none of the additional fields are being sent with the error response.
The text was updated successfully, but these errors were encountered: