Graphql React Apollo Link-如何在使用<;查询/>;及<;突变/>;组件?

Graphql React Apollo Link-如何在使用<;查询/>;及<;突变/>;组件?,graphql,apollo,react-apollo,Graphql,Apollo,React Apollo,在阅读了许多关于如何处理Apollo React的重新身份验证的文章后,我决定需要编写自己的逻辑来处理检查过期JWT、刷新令牌,然后继续执行操作。我不想重定向到任何类型的登录页面,从一个错误或等待,直到一个错误发生刷新我的令牌 所以我遵循了Apollo文档,在每个请求的头中添加了一个令牌。这很好: const authLink = new ApolloLink((operation, forward) => { operation.setContext(({ headers = {}

在阅读了许多关于如何处理Apollo React的重新身份验证的文章后,我决定需要编写自己的逻辑来处理检查过期JWT、刷新令牌,然后继续执行操作。我不想重定向到任何类型的登录页面,从一个错误或等待,直到一个错误发生刷新我的令牌

所以我遵循了Apollo文档,在每个请求的头中添加了一个令牌。这很好:

const authLink = new ApolloLink((operation, forward) => {
  operation.setContext(({ headers = {} }) => {
    const token = localStorage.getItem('token');

    // TODO: Check if token is expired. If so, fetch a new one from GraphQL server and THEN
    // move forward with operation

    // If token exists, add to headers
    if (token) {
      headers = { ...headers,authorization: token ? `Bearer ${token}` : "", };
    }

    return { headers };
  });

  return forward(operation);
});


const httpTopicsServiceLink = createHttpLink({
  uri: 'http://localhost:4000/topics',
});

export const TopicsClient = new ApolloClient({
  link: authLink.concat(httpTopicsServiceLink),
  cache: new InMemoryCache(),
});

但是,在
authLink
函数中,我想检查令牌是否过期,以便能够向服务器发送GraphQL变异以刷新我的令牌,然后转发我的操作。由于我一直在Apollo
组件内部执行所有GraphQL查询和转换,因此我不确定如何向GraphQL服务器发出独立请求以完成此类功能。我能做什么?

您可以使用TopicsClient将采石场和变异发送到graphql服务器

const client = new ApolloClient({
  link: new ApolloLink((operation, forward) => {...}),
  cache: new InMemoryCache()
});
客户应可在ApolloLink建筑内访问