Vue Apollo TypeScript:ApolloClient 缺少 websocket 属性

Vue Apollo TypeScript: ApolloClient is missing websocket properties

我正在尝试使用 Vue 测试库和 Apollo 设置组件测试,如 their example 中所述。

import { ApolloClient, InMemoryCache } from '@apollo/client'
import { render } from '@testing-library/vue'
import VueApollo from 'vue-apollo'

const apolloClient = new ApolloClient({
  uri: 'http://localhost:4001/graphql',
  cache: new InMemoryCache({
    addTypename: false,
  }),
})

type ComponentType = Parameters<typeof render>[0]
const renderWithApollo = (Component: ComponentType) =>
  render(Component, undefined, (localVue) => {
    localVue.use(VueApollo)

    return {
      apolloProvider: new VueApollo({ defaultClient: apolloClient }),
    }
  })

但是,当我这样做时,TypeScript 会大喊 defaultClient 并出现以下错误:

Type 'ApolloClient<NormalizedCacheObject>' is missing the following properties from type 'ApolloClient<any>': store, writeData, initQueryManager, wsClientts(2739)
apollo-provider.d.ts(16, 5): The expected type comes from property 'defaultClient' which is declared here on type '{ defaultClient: ApolloClient<any>; defaultOptions?: VueApolloComponentOptions<Vue> | undefined; clients?: { [key: string]: ApolloClient<any>; } | undefined; watchLoading?: WatchLoading | undefined; errorHandler?: ErrorHandler | undefined; prefetch?: boolean | undefined; }'

ApolloClient 怎么会错过这些类型,我该如何给它这些属性?感觉好像少了配置

我发现我少了一个 link 属性,我不得不打电话给 provideApolloClient:

import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client'
import { render } from '@testing-library/vue'
import fetch from 'cross-fetch'
import { provideApolloClient } from '@vue/apollo-composable'

const apolloClient = new ApolloClient({
  uri: 'http://localhost:4001',
  cache: new InMemoryCache({
    addTypename: false,
  }),
  link: new HttpLink({ uri: '/graphql', fetch }),
})

type ComponentType = Parameters<typeof render>[0]
const renderWithApollo = (Component: ComponentType) =>
  render(Component, undefined, (localVue) => {
    localVue.use(VueApollo)

    provideApolloClient(apolloClient)

    return {
      // @ts-ignore
      apolloProvider: new VueApollo({ defaultClient: apolloClient }),
    }
  })