有没有办法动态更改检索到的客户端使用的 URI getTenantEndpoint
?
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() 返回)并重新加载页面,但是不需要完全重新加载页面的解决方案是理想的。
有没有办法用 URI 覆盖authMiddleware
?