4

Is there a way to dynamically change the URI used by the client retrieved with 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)
  });
};

...


Currently, my (crude) approach is to set a new URI in the localeStorage (returned with getTenantEndpoint()) and reload the page, but a solution that doesn't require to reload entirely the page would be ideal.

Is there way to override the URI with authMiddleware ?

dbrrt
  • 2,275
  • 4
  • 18
  • 35

0 Answers0