微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

动态更新 Apollo 客户端使用的端点

如何解决动态更新 Apollo 客户端使用的端点

有没有办法动态更改用 getTenantEndpoint 检索到的客户端使用的 URI?

const httpLink = new HttpLink({
  uri: `${getTenantEndpoint()}/graphql`,fetch
});

const authMiddleware = new ApolloLink(
  (operation: Operation,forward: NextLink) => {
    operation.setContext((_: any) => {
      return {
        headers: {
          ...operation.getContext().headers,Authorization: token && `Bearer ${token}`
        }
      };
    });

    return forward(operation);
  }
);


let apolloClient: ApolloClient<normalizedCacheObject> | null = null;
const create = (initialState = {}): ApolloClient<normalizedCacheObject> => {
  const httpLinkConfig: HttpLink.Options = {
    uri: `${getTenantEndpoint()}/graphql`,credentials: "same-origin"
  };
  if (!IS_SERVER) {
    httpLinkConfig.fetch = fetch;
  }
  return new ApolloClient({
    connectToDevTools: !IS_SERVER,ssrMode: IS_SERVER,link: from([
      authMiddleware,// @ts-ignore
      httpLink
    ]),cache: new InMemoryCache().restore(initialState)
  });
};

...


目前,我的(粗略)方法是在 localeStorage 中设置一个新的 URI(通过 getTenantEndpoint() 返回)并重新加载页面,但不需要完全重新加载页面解决方案将是理想的。

>

有没有办法用 authMiddleware 覆盖 URI?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。