React-apollo: TypeError: Cannot add property x, object is not extensible

Created on 18 Oct 2017  路  6Comments  路  Source: apollographql/react-apollo

this.subscription = [subscribeToMore({
document: postInserted,
updateQuery: (previousResult, {
subscriptionData
}) => {
Object.preventExtensions(previousResult);
/* ! error line --> */ previousResult.posts.push(subscriptionData.data.postInserted);
return previousResult
},
}),

I get this error

              TypeError: Cannot add property 2, object is not extensible

how can I solve this error?

what is the solution to this problem?
@jbaxleyiii

Most helpful comment

when use graphql subscription.

instead

this.comments.push(addComment);

Here is a working demo:

this.comments = this.comments.concat(addComment);

All 6 comments

This issue has been automatically labled because it has not had recent activity. If you have not received a response from anyone, please mention the repository maintainer (most likely @jbaxleyiii). It will be closed if no further activity occurs. Thank you for your contributions to React Apollo!

This issue has been automatically labled because it has not had recent activity. If you have not received a response from anyone, please mention the repository maintainer (most likely @jbaxleyiii). It will be closed if no further activity occurs. Thank you for your contributions to React Apollo!

Hi! This happens because you are passing a non extensible object to options on react-apollo config. Maybe because you are using Inmmutable.js or similar.

So instead of this

options: (ownProps) => ownProps

Do this

options: (ownProps) => {
      return {
        ...ownProps
      }
  }

Then your object will be extensible.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isExtensible

when use graphql subscription.

instead

this.comments.push(addComment);

Here is a working demo:

this.comments = this.comments.concat(addComment);

Closing - housekeeping. Please ask questions on slack or stack overflow.

@mrdulin: I ran into a similar error when trying to push to a member of the cache object. Assigning to it didn't work either: Cannot assign to read only property 'signedUp' of object.

Can't believe how tricky this is to get right. And there's nothing in the docs about this.

Meteor was amazingly easy to use for keeping data in sync.

Was this page helpful?
0 / 5 - 0 ratings