I'm having issues rehydrating a component with new data via query. To set the premise i have two components that are siblings like this:
<Container>
<Navbar />
<Login /> || <Home />
</Container
Basically in my login component i have a mutation that runs like this when i hit login:
this.props.mutate({
variables: { token },
refetchQueries: () => ['isAuthenticated']
}).then((data) => {
console.log(data);
this.props.history.push(`/`);
})
So the result ends up being that nothing rerenders, i'm console.log()ing in my navigation components to see if the props for it have changed but it hasn't, however in my network tab and cache is the data that it should be returning.
My navigation item component looks like this:
import React, { Component } from "react";
import { Link, withRouter } from "react-router-dom";
import gql from "graphql-tag";
import { graphql, compose } from "react-apollo";
import { withStyles } from "@material-ui/core/styles";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
import Button from "@material-ui/core/Button";
import headerLinksStyle from "./HeaderLinksStyle";
import { LockOpen, GetApp, Info } from "@material-ui/icons";
const isLoggedInQuery = gql`
query isLoggedInQuery {
token @client
}
`;
const loggedInMutation = gql`
mutation authToken($token: String!) {
assignAuthToken(token: $token) @client {
token
}
}
`;
const userQuery = gql`
query isAuthenticated {
currentUser {
id
firstName
lastName
gender
location
password
email
permissions {
id
name
displayOrder
description
}
roles {
id
name
description
displayOrder
}
}
}
`;
class HeaderLinks extends Component {
logout = () => {
localStorage.removeItem("auth_token");
this.props.mutate({
variables: { token: null }
});
this.props.history.push(`/`);
};
refetch = () => {
console.log("refetch")
this.props.user.refetch();
}
render() {
const { classes } = this.props;
const { token } = this.props.data;
const isLoggedIn = token;
console.log(this.props);
return (
<List className={classes.list}>
{!isLoggedIn && (
<ListItem className={classes.listItem}>
<Link to="/login" style={{ color: "inherit" }}>
<Button color="inherit" className={classes.navLink}>
<LockOpen className={classes.icon} />Login
</Button>
</Link>
</ListItem>
)}
{!isLoggedIn && (
<ListItem className={classes.listItem}>
<Link to="/signup" style={{ color: "inherit" }}>
<Button color="inherit" className={classes.navLink}>
<GetApp className={classes.icon} />Sign up
</Button>
</Link>
</ListItem>
)}
<ListItem className={classes.listItem}>
<Button color="inherit" className={classes.navLink} onClick={() => { this.refetch();}}>
<Info className={classes.icon} />About
</Button>
</ListItem>
{isLoggedIn && (
<ListItem className={classes.listItem}>
<Button
color="inherit"
className={classes.navLink}
onClick={event => {
this.logout();
}}
>
<LockOpen className={classes.icon} />Logout
</Button>
</ListItem>
)}
</List>
);
}
}
export default compose(
graphql(isLoggedInQuery),
graphql(loggedInMutation),
graphql(userQuery, { name: "user", options: {fetchPolicy: 'network-only'}}),
withStyles(headerLinksStyle),
withRouter
)(HeaderLinks);
Firing this.props.user.refetch() works fine, however my props are coming back with stale information / error or data.