Reset store after logout with Apollo client

前端 未结 4 2001
深忆病人
深忆病人 2021-02-19 07:46

I\'m trying to reset the store after logout in my react-apollo application.

So I\'ve created a method called \"logout\" which is called when I click on a button (and pas

4条回答
  •  灰色年华
    2021-02-19 08:05

    client.resetStore() doesn't actually reset the store. It refetches all active queries

    Above statement is very correct.

    I was also having the logout related problem. After using client.resetStore() It refetched all pending queries, so if you logout the user and remove session token after logout you will get authentication errors.

    I used below approach to solve this problem -

     {
                      sessionStorage.clear()
                      client.clearStore().then(() => {
                        client.resetStore();
                        history.push('/login')
                      });
                    }}
                  >
                    {logout => (
                      
                    )}
                  
    

    Important part is this function -

    onCompleted={()=> {
                      sessionStorage.clear(); // or localStorage
                      client.clearStore().then(() => {
                        client.resetStore();
                        history.push('/login') . // redirect user to login page
                      });
                    }}
    

提交回复
热议问题