const client = new ApolloClient({
uri: 'your graphql uri',
onError: ({ networkError, graphQLErrors }) => {
// 处理错误
},
});
const client = new ApolloClient({
uri: 'your graphql uri',
onError: ({ networkError, graphQLErrors, forward }) => {
// 处理错误
return forward(operation);
},
});
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.forEach(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
);
}
if (networkError) console.log(`[Network error]: ${networkError}`);
});
const httpLink = createHttpLink({
uri: '/graphql',
});
const client = new ApolloClient({
link: ApolloLink.from([errorLink, httpLink]),
cache: new InMemoryCache(),
});
这样可以创建一个链接,链接中间件顺序可以影响错误处理的结果。