<p>apollo-link-error: 类型“ApolloLink”不可分配给类型“ApolloLink”。 存在具有此名称的两种不同类型,但它们无关。</p>

创建于 2018-03-07  ·  27评论  ·  资料来源: apollographql/apollo-link

预期行为

由 apollo-link-error onError ApolloLink创建的ApolloLink匹配。

实际行为

结果 apollo-link-* 包中似乎有两个不兼容的ApolloLink类型。

下面的示例在标记的位置返回 TypeScript 错误,但在标记的位置添加as any时按预期工作。

_简单_复制

import { InMemoryCache } from 'apollo-cache-inmemory'
import ApolloClient from 'apollo-client'
import { ApolloLink } from 'apollo-link'
import { onError } from 'apollo-link-error'
import { createHttpLink } from 'apollo-link-http'

const httpLink = createHttpLink({ uri: apiUrl })

const authLink = new ApolloLink((operation, forward) => {
  const token = getToken()
  if (token) {
    operation.setContext({
      headers: {
        Authorization: `Bearer ${token}`,
      },
    })
  }
  return forward(operation)
})

const logLink = onError(error => console.error('Apollo Error', error))  // as any

const link = logLink.concat(authLink.concat(httpLink))  // as any

/*
TS2345: Argument of type 'ApolloLink' is not assignable to parameter of type 'ApolloLink | RequestHandler'.
  Type 'ApolloLink' is not assignable to type 'RequestHandler'.
    Type 'ApolloLink' provides no match for the signature '(operation: Operation, forward?: NextLink): Observable<FetchResult<Record<string, any>, Record<string, any>>>'.
*/

export const graphql = new ApolloClient({
  cache: new InMemoryCache(),
  link,
})

/*
TS2345: Argument of type '{ cache: InMemoryCache; link: ApolloLink; }' is not assignable to parameter of type 'ApolloClientOptions<NormalizedCacheObject>'.
  Types of property 'link' are incompatible.
    Type 'ApolloLink' is not assignable to type 'ApolloLink'. Two different types with this name exist, but they are unrelated.
      Types of property 'split' are incompatible.
        Type '(test: (op: Operation) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | Reques...' is not assignable to type '(test: (op: Operation) => boolean, left: ApolloLink | RequestHandler, right?: ApolloLink | Reques...'. Two different types with this name exist, but they are unrelated.
          Types of parameters 'left' and 'left' are incompatible.
            Type 'ApolloLink | RequestHandler' is not assignable to type 'ApolloLink | RequestHandler'. Two different types with this name exist, but they are unrelated.
              Type 'ApolloLink' is not assignable to type 'ApolloLink | RequestHandler'.
                Type 'ApolloLink' is not assignable to type 'RequestHandler'.

*/

问题标签

  • [x] 有复制
  • [ ] 特征
  • [ ] 阻塞
  • [ ] 好的第一期
has-reproduction

最有用的评论

我也看到这个问题。

所有27条评论

我也看到这个问题。

同样在这里...

@iki有趣,感谢您打开问题! 您是否将 apollo-link-error 和 apolo-link-http 更新到最新版本? 如果您在https://github.com/evans/apollo-link-reproductions/tree/538 中运行“npm run compile”,我看不到任何类型错误。 类型应该可以工作,因为ApolloLink的鸭子类型应该是相同的。 你有我可以克隆的复制库吗? 我们可能必须声明最低版本的 TS?

我遇到了这个问题,似乎已经通过修复package.jsonapollo-link版本来解决它,如下所示:

{
    "dependencies": {
       "dependencies": {
        "apollo-cache-inmemory": "^1.1.12",
        "apollo-client": "^2.2.8",
        "apollo-codegen": "^0.19.0",
        "apollo-link": "^1.2.1",
        "apollo-link-batch-http": "^1.2.1",
        "apollo-link-http": "^1.5.3",
        "apollo-link-retry": "^2.2.2"
    },
    "resolutions": {
        "apollo-link": "1.2.1"
    }
}

我不得不对graphql做类似的事情,以确保它只存在一个副本。

这应该在最新版本中修复。 见#599,被#600关闭,发布于[email protected]

@evans我还在等待[email protected]发布,所以我们得到了这个问题的修复。 它阻止我升级其他阿波罗软件包。

同样在这里,等待 1.0.10 发布

@ravishivt @sccdGyan抱歉! 我不确定那里发生了什么。 事情应该整理在[email protected]

我有一个类似的问题:

相关依赖:

"dependencies": {
    "@apollo/client": "^3.0.0-beta.19",
    "apollo-link-ws": "^1.0.19",
    "graphql": "^14.5.8",
    "subscriptions-transport-ws": "^0.9.16",
    ...
  },

代码:

import {
    ApolloClient,
    InMemoryCache,
    HttpLink,
    ApolloLink,
    Operation,
    NextLink,
    split,
    getMainDefinition
} from '@apollo/client'
import { WebSocketLink } from 'apollo-link-ws';

const wsLink = new WebSocketLink({
    uri: config.backendWSApiUrl,
    options: {
        reconnect: true
    }
})

const httpLink = new HttpLink({
    uri: config.backendHttpApiUrl
})

const link = split(
    // split based on operation type
    ({ query }) => {
        const definition = getMainDefinition(query);
        return (
            definition.kind === 'OperationDefinition' &&
            definition.operation === 'subscription'
        );
    },
    wsLink,
    httpLink,
);


打字稿错误:

Argument of type 'WebSocketLink' is not assignable to parameter of type 'ApolloLink | RequestHandler'.
  Type 'WebSocketLink' is missing the following properties from type 'ApolloLink': onError, setOnErrorts(2345)

与带有 3.0 测试版的@mnesarco相同

我遇到了同样的
"@apollo/client": "~3.0.0-beta.29"@apollo/link-error": "^2.0.0-beta.3",
使用ApolloLink.from([errorLink, httpLink])

@smeevil你找到任何解决

这应该重新开放吗?

更新最新的 Apollo 客户端 3.0.0 beta 32 版本解决了我的问题

从 1.1.12 更新到[email protected]对我

现在都对我有好处

"@apollo/client": "^3.0.0-beta.37",
"@apollo/link-context": "^2.0.0-beta.3",
"@apollo/link-error": "^2.0.0-beta.3",
"@apollo/link-retry": "^2.0.0-beta.3",
"@apollo/link-ws": "^2.0.0-beta.3",`

现在都对我有好处

"@apollo/client": "^3.0.0-beta.37",
"@apollo/link-context": "^2.0.0-beta.3",
"@apollo/link-error": "^2.0.0-beta.3",
"@apollo/link-retry": "^2.0.0-beta.3",
"@apollo/link-ws": "^2.0.0-beta.3",`

谢谢! 将@apollo/client 更新到 3.0.0-beta.39 将导致此错误。

使用@apollo/link-error而不是apollo-link-error对我有用。

我是 Apollo 的新手,我按照 Apollo Server 指南得到了这个错误,该指南指示您从apollo-boost导入ApolloClient apollo-boost而不直接使用 Apollo 链接库。 我的代码看起来像:

import ApolloClient from 'apollo-boost';

const client = new ApolloClient({
  uri: "/api/graphql",
});

const App = ({ Component, pageProps }: AppProps) => {
  return (
    <ApolloProvider client={client}>
      <Component {...pageProps} />
    </ApolloProvider>
  )
}

我能够通过安装@apollo/client并重构为这样的东西来修复它:

import { ApolloClient, HttpLink, InMemoryStore } from '@apollo/client';

const client = new ApolloClient({
  cache: new InMemoryStore(),
  link: new HttpLink({ uri: "/api/graphql" })
});

const App = ({ Component, pageProps }: AppProps) => {
  return (
    <ApolloProvider client={client}>
      <Component {...pageProps} />
    </ApolloProvider>
  )
}

谢谢! 将@apollo/client 更新到 3.0.0-beta.39 将导致此错误。

谢天谢地,这似乎已被版本@apollo/client 修复到 3.0.0-beta.41!

我今天在尝试新的 3.0.0-beta.43 时遇到了这个问题。
能够解决并分享如何帮助其他人。

对我来说,要么将 package.json “resolutions” 字段指定为 beta.43 要么,或者,删除yarn.lock并重新安装解决了这个问题。

在 3.0.0-beta.44 上。 尝试在 package.json 中添加分辨率字段并删除 yarn.lock 文件,对我来说都不起作用。

对我来说,升级客户端和链接错误解决了这个问题:
"@apollo/client": "^3.0.0-beta.49", "@apollo/link-error": "^2.0.0-beta.3"

我上线了

    "@apollo/client": "^3.0.0-beta.50",
    "@apollo/link-context": "^2.0.0-beta.3",

并且仍然有这个问题。

我上线了

    "@apollo/client": "^3.0.0-beta.50",

    "@apollo/link-context": "^2.0.0-beta.3",

并且仍然有这个问题。

对于我的项目,仔细检查 package.json 并确保 Apollo 的所有内容都是 3.0,然后删除 yarn.lock 并再次安装 yarn。

谢谢@onpaws 。 我想我的问题是我确实有其他(开发)依赖项,它们本身具有“旧”Apollo 作为依赖项(例如graphql-codegen ),我不准备删除它们。 但是我没有在我的代码中 _import_ 旧的,所以我不明白为什么 Typescript 会对它们感到困惑。 不过,我是 Typescript 的新手,所以这可能只是“我”的问题。 如果我只是通过as any投射,一切都运行良好,即使它看起来很荒谬:

export const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: authenLink.concat(httpLink as any) as any
});
此页面是否有帮助?
0 / 5 - 0 等级