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

Store doesn't persist data after rebuilding it when logging out #111

Open high-performance-hery opened 2 years ago

high-performance-hery commented 2 years ago

When logging out, we rebuild a new environment like this:

    const store = new Store(recordSource, persistOptionsStore, relayStoreOptions)
    const env = new Environment(
        {
            network,
            store,
        },
        {}
    )

but the issue is that mutations aren’t updating the store until we hard restart the app.

Is there anythign specific we need to do when logging out?

morrys commented 2 years ago

Hi @high-performance-hery, It appears that the restore of the new environment is not performed

high-performance-hery commented 2 years ago

We do call useRestore with the newly built environment, is there anything else we need to do?

morrys commented 2 years ago

Add the log to understand if the new environment has been restored and if the mutation is performed in the new enrivonment

morrys commented 2 years ago

Otherwise, create a small sample project where the problem occurs so that I can help you

high-performance-hery commented 2 years ago

So the new environment has been restored and the mutation is performed in the new environment.

However it looks like the first mutation updates the store correctly, but the UI doesn't get updated. So subsequent mutations just send the same mutation as the initial one and no more updates occur.

After restarting the app, mutations behave normally.

So it looks like we set up the Relay store and environment correctly, but there's an issue with the way we hook up the UI?

morrys commented 2 years ago

I tried to modify the example project by adding a button that makes a new "login" and everything seems to work correctly. https://github.com/morrys/offline-examples/blob/master/relay/todo-updater/js/app.js#L106

high-performance-hery commented 2 years ago

Looks good, thanks! It looks like a setup issue on our end: queries go to the old store and mutations go to the new one, which is why the UI doesn't update when the store updates.

high-performance-hery commented 2 years ago

Is there a store property we can watch in the logs to differentiate them? I'm currently using relay-devtools to watch which store events go througgh but it isn't the most convenient

high-performance-hery commented 2 years ago

Do I need to update the RelayEnvironmentProvider on logout? Doing this via a state update is tricky, since there is a place where I need to use our logout function imperatively (i.e. outside a component render function). Previously I had just been clearing async storage/access token and running buildEnvironment. Is there a workaround in this case?

morrys commented 2 years ago

Yes you have to update it, otherwise the application does not log out completely, in fact it uses the enviroment associated with the old user.

It is difficult for me to give you advice on how to do this in your case.

high-performance-hery commented 2 years ago

Yea sounds quite specific to our setup.

Would there be a problem with updating the env when logging in or signing up rather than logging out?

morrys commented 2 years ago

No problem, but you must avoid that the application uses the old environment in the meantime