morrys / react-relay-offline

TypeScript library files for Relay Modern Offline
https://morrys.github.io/react-relay-offline/docs/react-relay-offline.html
MIT License
224 stars 15 forks source link

What is the best way to avoid performing mutations when it's offline? #140

Closed bglgwyng closed 1 year ago

bglgwyng commented 1 year ago

As far as I know, react-relay-offline's default behavior is keeping mutations in the queue until the network is back online. I read some code of react-relay-offline and I couldn't find any way to change this behavior. But I want to send mutations only when the network is online. At least for some mutations. For example, it seems quite weird that the sign-in mutation is kept in the queue and resent when the network is back online. It'd be better just to throw an error when the network is offline in this case.

I found one way to do that, but it's a bit ugly.

if (environment.isOnline()) {
  // do mutation
} else {
  // throw error
}

Is there any better way to do that?

morrys commented 1 year ago

Hi @bglgwyng, have you tried onPublish function https://github.com/morrys/react-relay-offline#environment-with-offline-options?

bglgwyng commented 1 year ago

@morrys From my observation, onPublish is called whether it's online or not and also whether it's succeeded or failed. So I think that I need to filter operations to be queued by looking at the value of offlinePayload.request.payload.operation. Then, this indicates that I'm going to write the logic that decides which mutations are queued in the one place, that onPublish. And I'm not fully satisfied with this solution. Do I understand correctly? Or am I missing something?

morrys commented 1 year ago

yes, you understood correctly, onPublish is called only when an offline mutation is performed, you can use the metadata field of cacheConfig, adding for example skipOffline: true, to change the behavior of onPublish

bglgwyng commented 1 year ago

Thank you for your kind reply!