score:1

for me it worked when i refetched the currentuser query in my login mutation. i added my code below. maybe it helps:

onsubmit(event) {
event.preventdefault();

const { email, password } = this.state;

this.props
  .mutate({
    variables: { email, password },
    update: (proxy, { data }) => {
      // get token from response and set it to the localstorage
      localstorage.setitem('token', data.login.jwt);
    },
    refetchqueries: [{ query: currentuser }]
  })
  .catch((res) => {
    const errors = res.graphqlerrors.map(error => error.message);
    this.setstate({ errors });
  });
}

score:2

i think you might be able to solve this issue by setting the query's fetchpolicy to "cache-and-network". you can read about fetch policies here: "graphql query options.fetchpolicy"

in your specific case i think you can update this line

const privateroute = graphql(mequery, { name: 'mequery' })(proute)

to this:

const privateroute = graphql(mequery, { name: 'mequery', options: {fetchpolicy: 'cache-and-network'} })(proute)

explanation

as stated in the documentation, the default policy is cache-first.

  1. currentuser is queried the first time and it updates the cache.
  2. you execute the login mutation, the cache is not updated without you updating it (read about it here: "updating the cache after a mutation").
  3. currentuser query is executed again but due to the default cache-first policy the outdated result will be retrieved only from the cache.

from the official documentation:

cache-first: this is the default value where we always try reading data from your cache first. if all the data needed to fulfill your query is in the cache then that data will be returned. apollo will only fetch from the network if a cached result is not available. this fetch policy aims to minimize the number of network requests sent when rendering your component.

cache-and-network: this fetch policy will have apollo first trying to read data from your cache. if all the data needed to fulfill your query is in the cache then that data will be returned. however, regardless of whether or not the full data is in your cache this fetchpolicy will always execute query with the network interface unlike cache-first which will only execute your query if the query data is not in your cache. this fetch policy optimizes for users getting a quick response while also trying to keep cached data consistent with your server data at the cost of extra network requests.

in addition to these two there are two more policies: 'network-only' and 'cache-only' here's the link to the documentation


Related Query

More Query from same tag