Search code examples
reactjsgraphqlapollo

Apollo error handling in one point in application


I would like to add error handling in my apollo react app. I'm checking error

export enum ERROR_CODES {
  ERROR_USER_NOT_EXIST = "ERROR_USER_NOT_EXIST"
}

export const getErrorMessage = (error: string): string | null => {
  switch(error) {
    case ERROR_CODES.ERROR_USER_NOT_EXIST:
      return 'test error';
    default:
      return null;
  }
}

and I want to show snackBar for errors which I have in switch case.

I understand that i can't do it with apollo-link-error because i want to show an error like react component and I don't want to add error handling for each query request in my components. Maybe exist way to do it in one point in my app and without apollo-link-error.


Solution

  • Use apollo-link-error ;)

    Nobody forces you to only console.log() error or break a data flow. You can use it to "inject your hook" only, to be notified on errors.

    You can wrap your app in some context provider - create a 'communication channel' - use it to write errors (from error link) and to render (read and clear) them in snackBar.