graphql error handling code example
Example: how to decode apollo client graphql error: bad request
import _ from 'lodash'
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map((error) => {
if (process.env.DEV) {
console.error(`[apollo]-9: graphQLErrors: %o`, error);
console.error(`[apollo]-10: graphQLErrors stackTrace: %o`, _.get(error, 'extensions.exception.stacktrace'));
}
const message = _.get(error, 'extensions.exception.data.message[0].messages[0].message',
_.get(error, 'extensions.exception.stacktrace[0]'));
error.code = _.get(error, 'extensions.code');
error.status = _.get(error, 'extensions.exception.code', error.code);
error.message = `[${error.path}]-(${error.status}) ${message}`;
});
if (networkError) {
if (process.env.DEV) console.error(`[apollo]-22: [Network Error]: %o`, networkError);
}
});
export default new ApolloClient({
link: authLink.concat(errorLink.concat(link)),
});