Apollo-client: new InMemoryCache throws an error

Created on 2 Dec 2019  ·  6Comments  ·  Source: apollographql/apollo-client


Trying to create a new InMemoryCache throws an error. I am trying this in React Native, not web.

Intended outcome:

A new InMemoryCache

Actual outcome:

TypeError: optimism.dep is not a function
TypeError: optimism.dep is not a function
    at new Root (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:103974:35)
    at new InMemoryCache (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:104690:20)
    at App.componentDidMount$ (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:98694:25)
    at tryCatch (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:29753:19)
    at Generator.invoke [as _invoke] (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:29928:24)
    at Generator.prototype.<computed> [as next] (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:29796:23)
    at tryCatch (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:29753:19)
    at invoke (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:29829:22)
    at http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:29859:13
    at tryCallTwo (http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false:6091:7)

How to reproduce the issue:

import {InMemoryCache} from '@apollo/client'

const cache = new InMemoryCache()

Versions

System:
OS: macOS 10.15.1
Binaries:
Node: 13.1.0 - /usr/local/bin/node
npm: 6.13.1 - /usr/local/bin/npm
npmPackages:
@apollo/client: ^3.0.0-beta.14 => 3.0.0-beta.14
apollo-cache-persist: ^0.1.1 => 0.1.1

All 6 comments

What version do you see in node_modules/optimism/package.json, and is that package installed anywhere else (such as node_modules/@apollo/client/node_modules/optimism)?

In any case, I suspect that running

npm i optimism@latest

(and committing the changes to package-lock.json) should fix this issue. 🤞

node_modules/optimism/package.json shows 0.11.4 and there is no node_modules/@apollo/client/node_modules/optimism.

I tried npm i optimism@latest but it seems 0.11.4 is the latest version so that didn't do anything unfortunately.

This seems to have fixed itself with 3.0.0-beta.15. 🏝

I'm getting this error too.

Error:

Failed to load apollo_example/core.cljs TypeError: "optimism.dep is not a function"
    InMemoryCache http://172.18.0.5:5678/cljs/js/cljs-runtime/module$node_modules$$apollo$client$apollo_client_cjs.js:231
    <anonymous> apollo_example/core.cljs:15
   ...

Setup, versions, etc:

npm ls optimism
[email protected] /workspace
├─┬ @apollo/[email protected]
│ └── [email protected]  deduped
└── [email protected] 

node --version
v13.9.0

cat package-lock.json | grep optimism
        "optimism": "^0.12.1",
    "optimism": {
      "resolved": "https://registry.npmjs.org/optimism/-/optimism-0.12.1.tgz",
user@2db2086b9237:~$ cat package-lock.json | grep apollo
  "name": "apollo-example",
    "@apollo/client": {
      "resolved": "https://registry.npmjs.org/@apollo/client/-/client-3.0.0-beta.49.tgz",

The only crazy thing about my setup is I'm trying to run this via ClojureScript/Helix. Which is a little crazy, I admit.

Everything worked fine with older version of Apollo, etc. Trying to upgrade to the 3.0-beta using @apollo/client and I hit this issue.

Fixed it.

Apparently the issue was something in the shadow-cljs compilation cache. I fixed it by changing the compiler options flag (https://shadow-cljs.github.io/docs/UsersGuide.html#_output_language_options) but reverting no options to update this issue did nothing (no crash, everything working fine)

Guessing, but most likely an older version of optimism was hanging around in the shadow-cljs compilation cache after upgrading apollo and dependencies.

Sorry for the noise.

Facing the same issue.
TypeError: optimism.dep is not a function. (In 'optimism.dep()', 'optimism.dep' is undefined)

I am migrating from react-apollo to @apollo/client, have tried a lot of things but was unable to fix this issue.

Error

Was this page helpful?
0 / 5 - 0 ratings