relay-tools / relay-hooks

Use Relay as React hooks
https://relay-tools.github.io/relay-hooks/docs/relay-hooks.html
MIT License
540 stars 56 forks source link

loadQuery - `network-only` Is Not Respected #163

Closed epiqueras closed 3 years ago

epiqueras commented 3 years ago

https://github.com/relay-tools/relay-hooks/blob/94de2de505a663a59052a6eb82bb95a56c20086e/src/QueryFetcher.ts#L168-L193

I think it's because you only fetch again when the fetch policy changes. This doesn't let you fetch the same query again without hacking around with the fetch key.

morrys commented 3 years ago

Hi @epiqueras, fetch policies have nothing to do with rendering logic.

The method you referenced follows the logic of react a getDerivedStateFromProps https://github.com/facebook/relay/blob/master/packages/react-relay/ReactRelayQueryRenderer.js#L136

The use of the fetchKey is not a hack but its precise purpose https://github.com/relay-tools/relay-hooks#usequery

epiqueras commented 3 years ago

Hi @morrys, let me add some context.

When you prefetch.next the same query with the same variables using network-only, this is making it so that nothing happens.

morrys commented 3 years ago

Yes, i know but when you render usePreloadedQuery , the function getValue recovers the last value resolved, is it right?

epiqueras commented 3 years ago

Yeah, but there is no new value because it's never fetched.

morrys commented 3 years ago

Yeah, but there is no new value because it's never fetched.

what do you mean? I don't understand

epiqueras commented 3 years ago
morrys commented 3 years ago

Because you are already displaying and rendering the previously fetched data as if you were using useQuery and call a React setState

epiqueras commented 3 years ago

prefetch.next is imperative; it's not a hook. If you call it again, you expect the server's data to have changed, or you are refreshing the page without an actual window unload.

morrys commented 3 years ago

@epiqueras, fetch-query is imperative https://relay.dev/docs/en/fetch-query and not the loadQuery.

And anyway, as you said (with fetchKey), there is an easy way to make the loadQuery behave as you wish.

epiqueras commented 3 years ago

"network-only": will not reuse locally cached data, and will always send a network request to fetch the query, ignoring any data that might be locally cached in Relay.

Doesn't that contradict this?

morrys commented 3 years ago

Fetch policies are used when fetching is performed. When you do the next twice with the same parameters the behavior of the loadQuery is the same as when the useQuery is re-rendered (if the parameters do not change, the fetch is not re-executed)

From this issue it seems to be necessary to add a paragraph Behavior like this https://relay.dev/docs/en/api-reference#behavior-2 in the documentation

epiqueras commented 3 years ago

That makes sense. The documentation should be more precise.

What would you recommend as the canonical way of re-fetching the same preloaded query then?

morrys commented 3 years ago

As suggested in the comment above:

And anyway, as you said (with fetchKey), there is an easy way to make the loadQuery behave as you wish.

It is sufficient to use the fetchKey as a counter and increment it before executing a new next.

epiqueras commented 3 years ago

Ok, I was just wondering if there was another way because it felt hacky.

matepaiva commented 3 years ago

I think I am having the same issue or something similar. Even changing the fetchKey I am not able to force a re-fetch. It only re-fetches when the cache expires.