InMemoryCache is deep freezing results
See original GitHub issueIntended 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:
- Created 4 years ago
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@dylanwulf Yea but it seems the
freezeResults
flag was removed from theInMemoryCache
ctor?From the article: