简体   繁体   中英

vue-apollo execute graphql query in router guard

I have an application where a user can sign up for an event. As soon as the user has signed up, he can visit routes he couldn't before.

I'm using graphql for my api requests. The code in my route guard looks like this:

router.beforeEach(async (to, from, next) => {
  if (to.meta.signUpRequired && from.name) {
    const { data: { getCurrentUser } } 
      = await apolloProvider.defaultClient.query({
      query: USER_INFO_QUERY
    });
    if (getCurrentUser && getCurrentUser.isCollectionAttendee) {
      return next();
    }
    return next('/');
  }
  return next();
});

This works fine and the request is not executed every time the route changes because vue-apollo caches the result. My problem is, that if the user signs up and then tries to switch the route, he can't because the cached result of the query is still used.

I don't want to use a network-only fetchPolicy before every route change.

How can I solve this?

也许在注册后尝试使用this.$apolloProvider.defaultClient.resetStore()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM