I have a webapp made with Next.js and Apollo as show in example with-apollo. I want to serve multiple domains with my webapp (name-based virtual hosting). Unfortunately HttpLink
of ApolloClient
requires absolute server URL with domain but this makes backend app unable to recognize domain which user really visited. Is there a way to configure HttpLink
with a dynamic URL based on real request or use relative URL or anything else?
Asked
Active
Viewed 512 times
0

igo
- 6,359
- 6
- 42
- 51
-
I don't understand why " absolute server URL with domain but this makes backend app unable to recognize domain which user really visited" – Akhil Surapuram Dec 06 '19 at 09:31
-
absolule url doesn't indicate that url has to be immutable string – Akhil Surapuram Dec 06 '19 at 09:32
-
It means url should contain protocol , domain and path – Akhil Surapuram Dec 06 '19 at 09:36
-
what backend you are using? – Akhil Surapuram Dec 06 '19 at 09:37
2 Answers
0
Either use an Apollo Link to intercept the query and set uri property on the context
const authMiddleware = setContext((operation, { uri }) => {
return refreshToken().then(res => ({
uri: this.getURI()
})
}))
Or intercept the request with Angular's HttpClient interceptor and change the endpoint.

Mykybo
- 1,429
- 1
- 12
- 24
0
The NextPageContext
object passed to getInitialProps
includes the req
object when called on the server-side. So you can do something like:
WithApollo.getInitialProps = async ctx => {
const { AppTree, req } = ctx
const linkBaseUrl = req ? req.protocol + '://' + req.get('host') : ''
...
}
You can then pass this base url down to createApolloClient
along with the initial state and prepend your HttpLink's url with it. On the client side, this will prepend an empty string (you only need the full URL on the server).

Daniel Rearden
- 80,636
- 11
- 185
- 183