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?
Type 'InMemoryCache' is not assignable to type 'ApolloCache<NormalizedCacheObject>'. Property 'data' is protected in type 'InMemoryCache' but public in type 'ApolloCache<NormalizedCacheObject>'. [2322]
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"
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.
Type 'NormalizedCacheObject' is not assignable to type 'Cache'. Property 'add' is missing in type 'NormalizedCacheObject'.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With