question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript error when compiling a module using apollo-client and apollo-cache-inmemory

See original GitHub issue

Intended outcome:

Successful compilation of the project.

Actual outcome:

ERROR in [at-loader] ./node_modules/apollo-cache-inmemory/lib/inMemoryCache.d.ts:5:22
    TS2415: Class 'InMemoryCache' incorrectly extends base class 'ApolloCache<NormalizedCacheObject>'.
  Types of property 'read' are incompatible.
    Type '<T>(query: ReadOptions) => T | null' is not assignable to type '<T>(query: ReadOptions) => T'.
      Type 'T | null' is not assignable to type 'T'.
        Type 'null' is not assignable to type 'T'.

How to reproduce the issue: tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "jsx": "preserve",
    "target": "es2015",
    "lib": ["es2017", "dom", "esnext.asynciterable"],
    "module": "es2015",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strictNullChecks": true,
    "allowSyntheticDefaultImports": false,
    "baseUrl": ".",
    "paths": {
    }
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

Sample module:

import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloClient, ApolloQueryResult } from "apollo-client";
import { HttpLink } from "apollo-link-http";
import gql from "graphql-tag";

const client = new ApolloClient({
  cache: new InMemoryCache() as any,
  link: new HttpLink({ uri: process.env.GRAPHQL_ENDPOINT })
});

Version

  • apollo-client@2.0.2
  • apollo-cache-inmemory@^1.1.0
  • apollo-cache@^1.0.0

** Possible cause **

apollo-cache defines ApolloCache base class with read method which cannot miss:

abstract read<T>(query: Cache.ReadOptions): T

apollo-cache-inmemory actually has more realistic definition of the return value as T | null

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:22 (2 by maintainers)

github_iconTop GitHub Comments

25reactions
mbrochhcommented, Jan 16, 2019

Am I right to assume that this is still not fixed? I have this code:

    const client = new ApolloClient({
      cache: new InMemoryCache(),
      link: batchHttpLink,
      queryDeduplication: true,
      ssrMode: true,
    })

and am getting this TS error:

Type 'InMemoryCache' is not assignable to type 'ApolloCache<NormalizedCacheObject>'.
  Property 'data' is protected in type 'InMemoryCache' but public in type 'ApolloCache<NormalizedCacheObject>'. [2322]
11reactions
tackleycommented, Nov 7, 2017

Temporary, and unfortunate, workaround is to set "strictNullChecks": false 😞

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating to Apollo Client 3.0 - Apollo GraphQL Docs
This article walks you through migrating your application to Apollo Client 3.0 from previous versions of Apollo Client. To illustrate the migration process, ......
Read more >
Apollo - React (Typescript) Invariant Error when building ...
I'm struggling with my understanding of all the pieces here. I understand that Invariant Violations are about an issue with the wrong types ......
Read more >
Apollo Cache – Angular - GraphQL Code Generator
InMemoryCache is a normalized data store that supports all of Apollo Client 1.0's features without the dependency on Redux. In some instances, ...
Read more >
apollo-cache-inmemory - npm
Core abstract of Caching layer for Apollo Client. Latest version: 1.6.6, last published: 3 years ago. Start using apollo-cache-inmemory in ...
Read more >
Mike McMann / AngularJS Apollo Client · GitLab
Use your GraphQL server data in your Angular 1.0 app, with the Apollo Client. ... 'apollo-client'; import { InMemoryCache } from 'apollo-cache-inmemory'; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found