我正在使用nuxt
和apollo-module
我需要拦截可能的网络错误(401/403更具体)所以我可以显示一些错误模式并注销我的用户。在文档中,我看到在nuxt.config.js
内你可以这样做:
apollo: {
tokenName: 'Authorization',
authenticationType: 'Bearer',
errorHandler(error) { do something }
}
...
但在该配置文件中,我无法访问我需要的应用程序功能(例如,错误模式或我的路由器)。有没有办法存档?
您可以使用apollo-error-link
apollo: {
clientConfigs: {
default: '~/apollox/client-configs/default.js'
}
},
在这里配置
import { onError } from 'apollo-link-error'
export default function(ctx) {
const errorLink = onError(({ graphQLErrors, networkError }) => {
})
return {
link: errorLink,
// required
httpEndpoint: ctx.app.$env.GRAPHQL_URL,
httpLinkOptions: {
credentials: 'same-origin'
},
}
}