0

I am trying to update the users' password that I have retrieved previously when an event is published to the PasswordUpdated Channel. This is what I have tried so far,

this.apollo.subscribe({
  query: this.passwordUpdatedSubscription
}).subscribe(({passwordUpdated}:any) => {
  this.userDataQuery.updateQuery((previousResult) => {
    previousResult.getUsers.filter(user => user.name === passwordUpdated.name).forEach(user => user.password = passwordUpdated.password);
  });

});

However, I just found out from the error in the console that previousResult is immutable. When I tried to update the password in previousResult, the property has writable: false. How can I resolve this?

Anthony C
  • 2,117
  • 2
  • 15
  • 26

1 Answers1

0

You have to return the changed previous result. Check out subscription docu for apollo client. A bit below is the point for the update query.

And you might want to check reacts immutability helper which explains a little bit what the problem is.

Locco0_0
  • 3,420
  • 5
  • 30
  • 42
  • The `SubscribeToMore` function is available to the apollo-react client only. Unfortunately it is not available for the angular client. I checked the updateQuery from that page, it looks like they are re-creating the `newComments` array and push the new comments to it, which is what I am trying to avoid. I am looking for a way to mutate the previous result, rather than re-creating a new result from previous result. – Anthony C Mar 18 '17 at 19:41
  • You are right. I did not find `SubscribeToMore`. One thing that you could check if it is possible that the apollo client for angular does it for you in the background with `dataIdFromObject` [docu](http://dev.apollodata.com/angular2/cache-updates.html#dataIdFromObject). It should update the apollo store when youre response contains the id of the user. – Locco0_0 Mar 18 '17 at 23:00