Search code examples
react-routerapollo-clientgraphcool

Apollo query runs on page refresh but not with react router navigation?


Im using React, Apollo and Graphcool. I have an account page where I need to query the logged in users details:

const loggedInUser = gql`
    query {
        loggedInUser {
            id
        }
    }
`;

Logging in is done on a separate page. If the user then navigates or is automatically redirected (both with React Router) to the account page then the query comes back as null. If I give the page a hard refresh then the logged in users ID comes back from the query successfully.

Im not sure why this is happening. I don't think it could be a race condition as you can navigate to the account page after 10 seconds and it still wont return the user id unless you refresh.

Do I need to user resetStore? This could be expensive for me as a lot of data which wont have changed will need to be fetched again.


Solution

  • I needed to refetch the query after the user logged in as Apollo had cached the previous result of null. I added this to the login function and it now works.

    // I added this: 
    await this.props.loggedInUser.refetch();
    
    // redirect user 
    this.setState({
        redirect: true,
    });