动态更新 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 (将#修改为@)