Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apollo graphql UI is not updating after refetch

Tags:

reactjs

apollo

In a react login component, I'd like to refetch and update the navbar component once login is successful.

const loginUser = () => {
    props.mutate({
      variables: {
        input: { email, password },
      },
      refetchQueries: [{ query: GET_ME }],
    });
  };

I can see the login and re-fetch in network tab, and both return 200 with proper status. But the navbar still displays the old data.

Here is my navbar component.

export const Header = props => {
  return <div>Header {JSON.stringify(props.data)}</div>;
};

export default graphql(GET_ME)(Header);

And apolloClient:

export const client = new ApolloClient({
  link: createHttpLink({
    credentials: 'include',
    uri: 'http://localhost:8080/api/graphql',
  }),
  cache: new InMemoryCache(),
});
like image 898
leogoesger Avatar asked Nov 26 '18 04:11

leogoesger


People also ask

How do you update the Apollo Client cache after a mutation?

If a mutation updates a single existing entity, Apollo Client can automatically update that entity's value in its cache when the mutation returns. To do so, the mutation must return the id of the modified entity, along with the values of the fields that were modified.

Is redux still necessary with Apollo GraphQL?

Apollo GraphQL no longer requires Redux. Apollo Client automatically catches the data and normalizes new data in query responses and after mutation.


1 Answers

Try adding

 options: {
      awaitRefetchQueries: true
 },

to your props.mutate next to refetchQueries and variables.

Queries refetched using options.refetchQueries are handled asynchronously, which means by default they are not necessarily completed before the mutation has completed. Setting options.awaitRefetchQueries to true will make sure refetched queries are completed before the mutation is considered done (or resolved). options.awaitRefetchQueries is false by default.

like image 109
John Franke Avatar answered Oct 10 '22 12:10

John Franke