Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'setLink' is missing in type 'ApolloClient<NormalizedCacheObject>' but required in type 'ApolloClient<any>

I am setting up a new typescript project with react and apollo client. I am attempting to wire in the client like so:

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: new HttpLink({
    uri: 'http://localhost:3000/graphql',
    headers: {
      authorization: localStorage.getItem('token'),
    },
  }),
});

function App() {
  return (
    <ApolloProvider client={client}>
      <div className="App">
       ...content goes here
      </div>
    </ApolloProvider>
    );
}

However, this throws an error during runtime:

TypeScript error in /src/App.tsx(60,21):
Property 'setLink' is missing in type 'ApolloClient<NormalizedCacheObject>' but required in type 'ApolloClient<any>'.  TS2741

    58 | function App() {
    59 |   return (
  > 60 |     <ApolloProvider client={client}>
       |                     ^
    61 |       <div className="App">

As this appears to be a type based problem, I attempted to be explicit when creating the Apollo client per the example here: https://github.com/apollographql/apollo-client/issues/2503

const client = new ApolloClient<NormalizedCacheObject>{ ...

But no dice. I'm not sure why I have dueling types compared to working examples. Help?

like image 651
Eric H Avatar asked Jul 21 '20 00:07

Eric H


People also ask

Is it possible to assign type'inmemorycache'to apollocache?

Type 'InMemoryCache' is not assignable to type 'ApolloCache<NormalizedCacheObject>'. Property 'data' is protected in type 'InMemoryCache' but public in type 'ApolloCache<NormalizedCacheObject>'. [2322]

What are the dependencies of the apolloclient?

The ApolloClient is created as stated in the docs. Below are the current set of dependencies: "apollo-cache-inmemory": "^1.1.0", "apollo-client-preset": "^1.0.2", "apollo-client": "^2.0.2", "apollo-link-context": "^1.0.0", "apollo-link-http": "^1.1.0", "react-apollo": "^2.0.0"

How to fix property is missing in type but required in type?

The React.js error "Property is missing in type but required in type" occurs when we don't pass all of the required props to a component. To solve the error, make sure to pass all of the props the component requires, e.g. <MyComponent name="Tom" age= {30} /> or mark its props as optional.

Is it possible to assign type'normalizedcacheobject'to type'cache'?

Type 'NormalizedCacheObject' is not assignable to type 'Cache'. Property 'add' is missing in type 'NormalizedCacheObject'.


1 Answers

The @apollo/client package contains the imports from previously decoupled libraries like apollo-client and apollo-cache-inmemory.

If your imports look like this:

import { ApolloClient } from 'apollo-client'
import { InMemoryCache, NormalizedCacheObject } from 'apollo-cache-inmemory'

Switch to this:

import {
  ApolloClient,
  InMemoryCache,
  NormalizedCacheObject,
} from '@apollo/client'
like image 65
pfcodes Avatar answered Sep 21 '22 19:09

pfcodes