使用 Nuxt 拦截 apollo-module 上的网络错误

Edu*_*oso 4 apollo vue.js apollo-client nuxt.js vue-apollo

我正在使用nuxtwithapollo-module并且我需要拦截可能的网络错误(401/403 更具体),以便我可以显示一些错误模式并注销我的用户。在文档中,我看到在里面nuxt.config.js你可以这样做:

  apollo: {
    tokenName: 'Authorization',
    authenticationType: 'Bearer',
    errorHandler(error) { do something }
  }
...
Run Code Online (Sandbox Code Playgroud)

但是在该配置文件中,我无法访问我需要的应用程序功能(例如错误模式或我的路由器)。有什么办法可以存档吗?

Ald*_*und 7

您可以使用 apollo-error-link

  apollo: {
    clientConfigs: {
      default: '~/apollox/client-configs/default.js'
    }
  },
Run Code Online (Sandbox Code Playgroud)

这里配置

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'
    },
  }
}
Run Code Online (Sandbox Code Playgroud)