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.

InMemoryCache is deep freezing results

See original GitHub issue

Intended outcome:

Data can be mutated (not ideal, but some 3rd party libs rely on being able to mutate objects).

It would nice if there was a flag to disable object freezing.

Actual outcome:

TypeError: Cannot add property X, object is not extensible

How to reproduce the issue:

new ApolloClient({
  cache: new InMemoryCache(),
  link: new HttpLink({ uri: process.env.REACT_APP_GRAPHQL_URL }),
})
const { loading, data, error } = useQuery<Query>(QUERY);
data.result.X = '';

Versions

  System:
    OS: macOS 10.15.3
  Binaries:
    Node: 12.14.0
    Yarn: 1.22.0
    npm: 6.13.4
  Browsers:
    Chrome: 80.0.3987.116
    Safari: 13.0.5
  npmPackages:
    @apollo/client: ^3.0.0-beta.37 => 3.0.0-beta.37 
    @apollo/link-context: ^2.0.0-beta.3 => 2.0.0-beta.3 
    apollo: ^2.23.0 => 2.23.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
benjamncommented, Feb 25, 2020

See https://github.com/apollographql/apollo-client/pull/5153.

If you have to provide mutable data to external libs, you should deep-clone it first. Even just JSON.parse(JSON.stringify(data)) will work here, since GraphQL response data is inherently JSON-serializable.

The freezing is necessary because cache results can be shared between multiple consumers, so letting one of them modify the result is unacceptable.

Also, the freezing happens only in development, so it has no production performance penalty, in case you were concerned about that.

3reactions
narcierocommented, Feb 25, 2020

@dylanwulf Yea but it seems the freezeResults flag was removed from the InMemoryCache ctor?

From the article:

In Apollo Client 3.0, we intend to make both freezeResults and assumeImmutableResults true by default

const client = new ApolloClient({
  link: ...,
  cache: new InMemoryCache({
    freezeResults: true,
  }),
  assumeImmutableResults: true,
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

What's new in Apollo Client 2.6
Passing freezeResults: true to the InMemoryCache constructor causes all cache results to be frozen in development, so you can more easily detect ...
Read more >
How to remove the `__typename` field from the graphql ...
js:28 DEPRECATION WARNING: using fragments without __typename is unsupported behavior and will be removed in future versions of Apollo client.
Read more >
When a disk cache performs better than an in-memory cache ...
It caches many of the results of these queries for ten or fifteen minutes, which takes a lot of load away from the...
Read more >
Apollo Cache in a Nutshell - E.Y. - Medium
This is a 8th of the series blogs on deep dive into Apollo GraphQL ... the results of its GraphQL queries in a...
Read more >
Accelerating Deep Learning Inference via Freezing
Accelerating Deep Learning Inference via Freezing. Authors: Adarsh Kumar, Arjun Balasubramanian, Shivaram Venkataraman, and Aditya Akella, University of ...
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