Ad

Apollographql - Changes To Entries In Objects In Local State & Re-rendering?

Let's say I have an object in my local state and 2 types of query on that object: -

anObject {

__typename: "AnObject"

thisThing: ""

thatThing: ""}

Query 1

{

anObject @client

}

Query 2

{

anObject @client {

thisThing}

}

2 scenarios:-

A component was running Query 1 but does not use anObject.thatThing in it's render function.

A component was running Query 2 (which does not query thatThing).

Now, let's say another component mutated anObject.thatThing. I would assume scenario 1 would cause the component to re-render as it is subscribed to that entire object in the local state (via Query 1) whereas in scenario 2 the component would no re-render if anObject.thatThing mutated as it is only subscribed to a single (different) fragment of that object. Is that correct?

Ad

Answer

You have wrong assumptions.

Standard Query is not a Subscription or an ObservableQuery - it's an one time query, it won't force automatic update/refetch/render on external changes.

You can use the options listed above or [the simplest] pollInterval option.

For deeper object updates you should use shouldComponentUpdate method.


UPDATE

@client directive changes default querying behaviour. From docs:

Once you call client.writeData, the query result on the render prop function will automatically update.

It may look like a subscription but it isn't. Query is refetched in both cases. Rerendering of child component depends on its props changes. It won't be rerendered if props passed won't change - for the same query result or passing/using only unchanged result fragments (object properties).

Ad
source: stackoverflow.com
Ad