I have a React app built using Next.js. I want to be able to login and then cause the app to re-render in order for it to attempt to fetch the current logged in user.
My _app.js component is wrapped with the apollo provider and has a get user Query as well:
class MyApp extends App {
render() {
const { Component, pageProps, apolloClient } = this.props;
return (
<>
<Container>
<ApolloProvider client={apolloClient}>
<Query query={GET_USER} fetchPolicy="network-only" errorPolicy="ignore">
{({ loading, data, error }) => console.log("rendering app", data) || (
<>
<Component {...pageProps} user={data && data.me} />
</>
)}
</Query>
</ApolloProvider>
</Container>
</>
);
}
}
I then have a login form which simply sends the email and password to my API which then returns a set-cookie token header with the access token.
<Mutation
mutation={LOGIN}
onError={error => {
this.setState({error: "Incorrect email or password"})
}}
onCompleted={() => {
window.location.reload() // temp solution to cause app to re-render
}}
>
{(login, {loading}) => (
<Login
{...this.props}
login={login}
loading={loading}
error={error}
/>
)}
</Mutation>
I can successfully log in but I was expecting the Apollo cache to get written to with the user data:
{
"data": {
"login": {
"_id": "abcd1234",
"accessToken": "abc123",
"firstName": "First",
"lastName": "Last",
"email": "abcd123@b.com",
"__typename": "User"
}
}
}
As the cache is getting written to, I was expecting the _app.js / ApolloProvider children to re-render as they receive props from the cache.
My get user Query should then attempt to run again (this time with the access token set in a cookie) and a user should be returned (indicating the user is logged in).
Why is my mutation not telling my app to re-render onCompleted?