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

Intercepting network errors on apollo-module using Nuxt

我正在使用 nuxtapollo-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'
    },
  }
}