Commit 8e808bc2 authored by Kushal Pandya's avatar Kushal Pandya

Add support for baseUrl in ApolloClient instance

Add support for providing baseUrl/rootUrl within `uri` while
creating ApolloClient instance.
parent d8d57f23
...@@ -3,10 +3,17 @@ import { InMemoryCache } from 'apollo-cache-inmemory'; ...@@ -3,10 +3,17 @@ import { InMemoryCache } from 'apollo-cache-inmemory';
import { createUploadLink } from 'apollo-upload-client'; import { createUploadLink } from 'apollo-upload-client';
import csrf from '~/lib/utils/csrf'; import csrf from '~/lib/utils/csrf';
export default (resolvers = {}) => export default (resolvers = {}, baseUrl = '') => {
new ApolloClient({ let uri = `${gon.relative_url_root}/api/graphql`;
if (baseUrl) {
// Prepend baseUrl and ensure that `///` are replaced with `/`
uri = `${baseUrl}${uri}`.replace(/\/{3,}/g, '/');
}
return new ApolloClient({
link: createUploadLink({ link: createUploadLink({
uri: `${gon.relative_url_root}/api/graphql`, uri,
headers: { headers: {
[csrf.headerKey]: csrf.token, [csrf.headerKey]: csrf.token,
}, },
...@@ -14,3 +21,4 @@ export default (resolvers = {}) => ...@@ -14,3 +21,4 @@ export default (resolvers = {}) =>
cache: new InMemoryCache(), cache: new InMemoryCache(),
resolvers, resolvers,
}); });
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment