apollo-angular: Cannot edit response object - Object frozen
Hi, I have the following code:
const GetChatsQuery = gql query($withBlocked: Boolean) { userChats(withBlocked: $withBlocked) { ${ GetChatGraphQL(10) } } };
` return this.apollo.query({
query: GetChatsQuery,
variables: {
withBlocked: withBlocked
}
}).map(res => {
let response = <GetChatsResponse>res.data;
// I get here the error
response.userChats[0].messages.push(null);
return response.userChats;
})`
The thing is that I’m getting an error on the push method:
Cannot add property 2, object is not extensible
As I understood from the internet it means that the object is frozen (Object.freeze).
Have anyone had that problem before? how can I solve it?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 5
- Comments: 15 (3 by maintainers)
It’s by design. You don’t want to touch store directly so Apollo Client freezes the result. It’s the same behaviour as in Redux, Ngrx and so on. They don’t freeze the results but they say to not mutate it. This won’t change anytime soon.
Messes things up, I’ll just deep-copy the response like this:
Curious of any news as well? Surprised to run into this - wasted several hours spinning on this. Frustrating beyond belief.
We definitely need some sort of option to disable the freezing. Took me a while to figure that the frozen response was preventing
ngModelfrom working on my forms.Aye, you can use object spreading, but be aware that its only a shallow copy and other objects will maintain the same frozen reference since Apollo deeply freezes objects. Similar q: https://github.com/apollographql/apollo-client/issues/1909
@ranbuch By “edit fields” you probably mean Form and Inputs that user can modify. If so, then use Reactive Forms. You don’t need to touch an object directly.
Yeah I came across this using nested proxies for fine grained change notification. Even if I have no intention of changing the original and seek to do referential equality the invariant for frozen object prevents the getter property returning a proxy. Resorting to JSON stringify or cloning removes any referential checking ability.
In general frozen objects while useful for enforcing immutability, get in the way of performance optimizations that can be used in libraries that work off mutable state even if the consumer has no intention of ever mutating that specific data.