Search code examples
typescriptvue.jsapollo-clientvue-apollovue-testing-library

Vue Apollo TypeScript: ApolloClient is missing websocket properties


I'm trying to set up a component test with Vue Testing Library and Apollo as described in 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 }),
    }
  })

However, when I do this, TypeScript yells about defaultClient with the following error:

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; }'

How can the ApolloClient miss those types and how do I give it those properties? It feels like I'm missing configurations.


Solution

  • I found out I was missing a link property and I had to call 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 }),
        }
      })