Apollo-client: Reopen: React-Native apps throws an error when calling `refetch`

Created on 22 Jan 2017  路  5Comments  路  Source: apollographql/apollo-client

This bug has occurred with previous versions of apollo (apollo-client #261, react-apollo #78)

To reproduce it just call reftech on a react-native app.
The error being thrown is:

One of the sources for assign has an enumerable key on the prototype chain. Are you trying to assign a prototype property? We don't allow it, as this is an edge case that we do not support. This error is a performance optimization and not spec compliant.

From what I gathered this bug has returned after moving back to object spreading thus probably being caused by the polyfil.

This is still occurring on the version 0.8.0 of apollo-client and version 0.8.1 of react-apollo.

馃悶 bug

Most helpful comment

This red screen is unfortunately as much as I could have gathered.

image

I might have found the cause though and this should be a warning maybe for any react-native user.
refetch function accept variables object as it's parameter, while react-native's onPress property passes a Proxy object to the listener callback passed to it.

meaning this will throw:

render() {
  const {data: {refetch}} = this.props;
  return (
    <TouchableOpacity onPress={refetch}>
      <Text>Refetch</Text>
    </TouchableOpacity>
  );
}

And this will not:

render() {
  const {data: {refetch}} = this.props;
  return (
    <TouchableOpacity onPress={() => refetch()}>
      <Text>Refetch</Text>
    </TouchableOpacity>
  );
}

The object assign being done inside of apollo's refetch causes the error to be thrown but it a tricky error because it does not point you to the object being changed in any way.

All 5 comments

@davidyaha interesting. Do you have a stack trace so we know where this is happening? I don鈥檛 think the solution is to stop using object spreading in _all_ cases; but instead, understand the underlying issue and fix that.

This red screen is unfortunately as much as I could have gathered.

image

I might have found the cause though and this should be a warning maybe for any react-native user.
refetch function accept variables object as it's parameter, while react-native's onPress property passes a Proxy object to the listener callback passed to it.

meaning this will throw:

render() {
  const {data: {refetch}} = this.props;
  return (
    <TouchableOpacity onPress={refetch}>
      <Text>Refetch</Text>
    </TouchableOpacity>
  );
}

And this will not:

render() {
  const {data: {refetch}} = this.props;
  return (
    <TouchableOpacity onPress={() => refetch()}>
      <Text>Refetch</Text>
    </TouchableOpacity>
  );
}

The object assign being done inside of apollo's refetch causes the error to be thrown but it a tricky error because it does not point you to the object being changed in any way.

Yep, that is definitely the cause. This isn鈥檛 a bug in Apollo Client, but we may be able to throw a more helpful error here if we added the following:

if (Object.getPrototypeOf(variables) !== Object.prototype) {
  throw new Error('Only plain objects may be used as variables.');
}

Although this case is rare enough that I don鈥檛 think we really need the check. It would also disallow some interesting use cases.

I鈥檓 going to close. Let us know if you run into any other problems 馃憤

davidyaha thx a lot and god bless you

Thank you @davidyaha. Your solution worked for me!

Was this page helpful?
0 / 5 - 0 ratings