首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用ApolloLink.split时处理Apollo客户端的错误

在使用ApolloLink.split时处理Apollo客户端的错误
EN

Stack Overflow用户
提问于 2018-10-30 18:59:23
回答 1查看 811关注 0票数 3

我有一个简单的代码:

代码语言:javascript
复制
  import { split } from 'apollo-link';
  import { WebSocketLink } from 'apollo-link-ws'
  import { HttpLink } from 'apollo-link-http'
  import ApolloClient from 'apollo-client'
  import { onError } from 'apollo-link-error'

  const wsLink = new WebSocketLink({
    uri: hasura.wsUrl,
    options: {
      reconnect: true,
      timeout: 30000,
      connectionParams: {
        headers: {
          'Authorization': `Bearer ${this.token}`
        }
      }
    }
  })

  const httpLink = new HttpLink({
    uri: hasura.httpUrl,
    headers: {
      'Authorization': `Bearer ${this.token}`
    }
  })

  const link = split(
    ({ query }) => {
      const { kind, operation } = getMainDefinition(query);
      return kind === 'OperationDefinition' && operation === 'subscription';
    },
    wsLink,
    httpLink
  )

  const errorLink = onError(({graphQLErrors, networkError}) => {
    // this callback is never called
    console.log('graphQLErrors', graphQLErrors)
    console.log('networkError', networkError)
  })

  this.client = new ApolloClient({
    link: errorLink.concat(link),
    cache: new InMemoryCache()
  })

如何处理“拆分”链接的错误?在这个例子中,捕获错误是不起作用的。如果我使用不带“拆分”功能的链接,错误捕获就会起作用。

EN

回答 1

Stack Overflow用户

发布于 2019-10-28 22:11:13

代码语言:javascript
复制
let link = ApolloLink.from([
  onError(({ graphQLErrors, networkError }) => {
    if (graphQLErrors) {
      graphQLErrors.map(({ message, locations, path }) =>
        console.log(
          `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
        )
      );
    }
    if (networkError) console.error(`[Network error]: ${networkError}`, networkError.stack);
  }),
  ApolloLink.split(
    operation => operation.getContext().important === true,
    httpLink, // don't batch important
    batchHttpLink
  ),
]);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53062839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档