如何捕捉Nuxt-Apollo错误 [英] How do I catch nuxt-apollo errors

查看:13
本文介绍了如何捕捉Nuxt-Apollo错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加入了我的阿波罗核项目。我构建了一个login,它在登录成功时设置JWT,但是当它执行时,我得到一个nuxt错误。我尝试使用Apollo错误处理程序捕获nuxt错误,但处理程序不起作用。 如何捕获错误以设置新令牌?

nuxt.config.js
....

  apollo: {
    authenticationType: 'Bearer',
    clientConfigs: {
      default: '~/graphql/config/config.ts'
    },
    errorHandler: '~/plugins/apollo-error-handler.ts',
    tokenName: 'apollo-token'
  }
...

我的错误处理程序如下所示:

import { Context } from '@nuxt/types'

export default ({ redirect, app, $apolloHelpers, ...rest }: Context) => {
  console.log(1, rest)
}

推荐答案

我在我的nuxt.config.js>;apollo.clientConfigs.default: '@/plugins/nuxt-apollo-config.js'(不是errorHandlerSO)中设置了以下内容,它有点适合我的用例。
不是专家,关于the documentation可能有10种不同的处理方式。
我按照Apollo Link overview设置了以下内容。

import { onError } from '@apollo/client/link/error'
import { setContext } from 'apollo-link-context'
import { from } from 'apollo-link'
import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'
import { createHttpLink } from '@apollo/client/core'
import schema from '../apollo/schema.json'
const fragmentMatcher = new IntrospectionFragmentMatcher({
  introspectionQueryResultData: schema,
})

export default ({ app, $config: { baseUrlGraphql } }) => {
  const headersConfig = setContext(() => ({
    credentials: 'same-origin',
    headers: {
      // ...
    },
  }))

  const link = onError(({ graphQLErrors, networkError, operation, response }) => {
      if (graphQLErrors) {
        graphQLErrors.map(() => console.log('error my dude'))
      }

      if (networkError) {
        console.log('and another one!')
      }
  })

  const cache = new InMemoryCache({ fragmentMatcher, resultCaching: false })

  return {
    defaultHttpLink: false,
    link: from([
      headersConfig,
      link,
      createHttpLink({
        credentials: 'include',
        uri: baseUrlGraphql,
        fetch: (uri, options) => {
          return fetch(uri, options)
        },
      }),
    ]),
    cache,
  }
}

我不确定它是否会有任何帮助,但这就是我的全部信息。

这篇关于如何捕捉Nuxt-Apollo错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆