-
Sorry for the bothersome question but trying to wrap my head around how this would work with apollo server's errors and apollo client's error link. Apollo catches errors when there is a returned Thoughts on how to unify the two? Ideally, plugin errors would be treated as apollo-server errors on the client side. Curious how you've set it up for your projects. Here's our apolloClient: const authLink = new ApolloLink((operation, forward) => {
operation.setContext(({ headers = {} as { [key: string]: $TsFixMe } }) => {
// @ts-ignore
const cookies = new Cookies();
const accessToken = cookies.get('jwt2');
return {
headers: defaults(headers, {
authorization: accessToken ? `Bearer ${accessToken}` : '',
'sentry-trace': uuidv4(),
}),
};
});
return forward(operation);
});
const sentryLink = new SentryLink({
breadcrumb: {
includeQuery: true,
includeVariables: true,
includeError: true,
includeResponse: true,
// includeCache: true,
},
});
const httpLink = new HttpLink({
uri: `${API_URL}/api/graphql`,
fetch,
});
const createLink = () => {
const errorLink = onError(({ operation, networkError, graphQLErrors, forward }) => {
if (networkError) {
const content = `[GraphQL error]: ${networkError.name} Message: ${networkError.message}, Stack: ${networkError.stack}`;
console.error(content);
if (IS_DEV) {
message.error(content);
}
}
if (graphQLErrors) {
graphQLErrors.forEach(({ message: gMessage, locations, path }) => {
const content = `[GraphQL error]: Message: ${gMessage}, Location: ${locations}, Path: ${path}`;
console.error(content);
if (IS_DEV) {
message.error(content);
}
});
}
return forward(operation);
});
return from([authLink, sentryLink, errorLink, apolloLogger, httpLink]);
};
const typePolicies: TypedTypePolicies = {
ApplicationFormFieldConfig: {
keyFields: ['name', 'label'],
},
ApplicationFormConfig: {
fields: {
fields: {
merge: false,
},
},
},
};
export const createApolloClient = (options: Options) =>
new ApolloClient({
ssrMode: typeof window === 'undefined',
link: createLink(),
cache: new InMemoryCache({
possibleTypes: possibleTypes.possibleTypes,
typePolicies,
}),
}); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
One thought is to eliminate the possibility of having errors not handled by error plugin by doing: const builder = new SchemaBuilder({
plugins: [ErrorsPlugin],
errorOptions: {
defaultTypes: [Error, ApolloError],
},
}); to catch all. That way, we will always return a union for all mutations and queries. |
Beta Was this translation helpful? Give feedback.
-
@mmahalwy I don't think that will work, generally you won't be throwing an apollo error inside you resolvers, so the errors plugin would not catch those errors. There are effectively 3 options:
Generally I would think about why you are trying to do with your error logging. I generally wouldn't want to rely on client reporting of server errors, and instead would want the server to log when things go wrong server side. For client side errors, I would probably want to report network errors, or errors that are not expected/handled by the client, which are generally not the cases you would handle with the errors plugin. Not sure if that's helpful 🤷 One feature I could see coming out of this, is a hook for logging errors handled by the errors plugin, which i don't think exists today |
Beta Was this translation helpful? Give feedback.
-
@hayes thanks for the detailed answer! I think we are going to go with option 3 as it wont force us to migrate everything over to errors plugin (option 1) and still benefit from the errors. In terms of hook - do you mean a react hook or an apollo link? |
Beta Was this translation helpful? Give feedback.
@mmahalwy I don't think that will work, generally you won't be throwing an apollo error inside you resolvers, so the errors plugin would not catch those errors.
There are effectively 3 options: