简体   繁体   English

vue-apollo 在路由器防护中执行 graphql 查询

[英]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.我正在为我的 api 请求使用 graphql。 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.这工作正常,并且不会在每次路由更改时执行请求,因为 vue-apollo 缓存了结果。 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.我不想在每次路由更改之前使用仅限网络的 fetchPolicy。

How can I solve this?我该如何解决这个问题?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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