Apollo-link: TypeError: forward 不是函数@apollo/[email protected], @apollo/[email protected]

创建于 2020-02-11  ·  3评论  ·  资料来源: apollographql/apollo-link

预期行为
当使用 $# @apollo/link-error中的onError()时,我们不会在 GraphQL 请求时收到错误消息。

实际行为

部门:

"dependencies": {
    "@apollo/client": "^3.0.0-beta.34",
    "@apollo/link-error": "2.0.0-beta.3",

设置:

import {
  ApolloClient,
  ApolloProvider,
  InMemoryCache,
} from '@apollo/client';
import { onError } from '@apollo/link-error';

const apolloClient = new ApolloClient({
  uri: '/graphql',
  // rehydration using the initial state
  cache: new InMemoryCache().restore(apolloState),

  link: onError(({ graphQLErrors, networkError }) => {
    console.log(graphQLErrors, networkError);
  }),
});

在与发出 GraphQL 请求的组件交互时,不幸的是,这就是我所得到的。
Screen Shot 2020-02-11 at 10 28 41 AM
打破异常,我在我的捆绑包中得到了这个:
Screen Shot 2020-02-11 at 10 36 34 AM
Screen Shot 2020-02-11 at 10 38 48 AM

一个简单的再现
我试图设置一个 Codesanbox,但是对于 AC 3.0 来说它失败了,并且存在一个神秘的依赖问题:
https://codesandbox.io/s/apolloclient-3-local-state-management-before-reactive-variables-nr1kg

最有用的评论

networkErrorapollo-link-contextapollo-link-errorapollo-link-http $ 一起发生时,我收到此错误:

import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
import { onError } from 'apollo-link-error'
import { InMemoryCache } from 'apollo-cache-inmemory'

const httpLink = new HttpLink({
  uri: // my api URI
})

const authLink = setContext((_, { headers }) => {
  // my authorization stuff
})

const errorLink = onError(({ graphQLErrors, networkError }) => {
  // my error handling logic
})

const apolloClient = new ApolloClient({
  link: authLink.concat(errorLink, httpLink),
  cache: new InMemoryCache()
})

所有3条评论

我在升级到 beta 时遇到了同样的问题。

如果包含 HttpLink 而不是直接使用uri ,似乎它就会消失。
所以也许 lib 错误地期望链中总是有后续项目并且存在forward函数?

networkErrorapollo-link-contextapollo-link-errorapollo-link-http $ 一起发生时,我收到此错误:

import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
import { onError } from 'apollo-link-error'
import { InMemoryCache } from 'apollo-cache-inmemory'

const httpLink = new HttpLink({
  uri: // my api URI
})

const authLink = setContext((_, { headers }) => {
  // my authorization stuff
})

const errorLink = onError(({ graphQLErrors, networkError }) => {
  // my error handling logic
})

const apolloClient = new ApolloClient({
  link: authLink.concat(errorLink, httpLink),
  cache: new InMemoryCache()
})

[已编辑] 对此有何更新? 我使用的是 RC 版本,但没有 HTTPLINK 仍然会出现相同的错误

        "@apollo/client": "^3.0.0-rc.10",
        "@apollo/link-batch-http": "^2.0.0-beta.3",
        "@apollo/link-context": "^2.0.0-beta.3",
        "@apollo/link-error": "^2.0.0-beta.3",
        "@apollo/link-retry": "^2.0.0-beta.3",

但是要解锁,这对我有用

    const link = from([ authLink, logoutLink,  new HttpLink({ uri }) ]);
    return new ApolloClient({
        name: APOLLO_CLIENT_NAME,
        version: config.appVersion,
        connectToDevTools: true,
        link,
        cache
    });
此页面是否有帮助?
0 / 5 - 0 等级