2019-07-07 11:18:12 +05:30
|
|
|
import { ApolloClient } from 'apollo-client';
|
|
|
|
import { InMemoryCache } from 'apollo-cache-inmemory';
|
|
|
|
import { createUploadLink } from 'apollo-upload-client';
|
2019-09-04 21:01:54 +05:30
|
|
|
import { ApolloLink } from 'apollo-link';
|
|
|
|
import { BatchHttpLink } from 'apollo-link-batch-http';
|
2019-02-15 15:39:39 +05:30
|
|
|
import csrf from '~/lib/utils/csrf';
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
export default (resolvers = {}, config = {}) => {
|
2019-07-31 22:56:46 +05:30
|
|
|
let uri = `${gon.relative_url_root}/api/graphql`;
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
if (config.baseUrl) {
|
2019-07-31 22:56:46 +05:30
|
|
|
// Prepend baseUrl and ensure that `///` are replaced with `/`
|
2019-09-04 21:01:54 +05:30
|
|
|
uri = `${config.baseUrl}${uri}`.replace(/\/{3,}/g, '/');
|
2019-07-31 22:56:46 +05:30
|
|
|
}
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
const httpOptions = {
|
|
|
|
uri,
|
|
|
|
headers: {
|
|
|
|
[csrf.headerKey]: csrf.token,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
return new ApolloClient({
|
2019-09-04 21:01:54 +05:30
|
|
|
link: ApolloLink.split(
|
|
|
|
operation => operation.getContext().hasUpload,
|
|
|
|
createUploadLink(httpOptions),
|
|
|
|
new BatchHttpLink(httpOptions),
|
|
|
|
),
|
|
|
|
cache: new InMemoryCache(config.cacheConfig),
|
2019-07-07 11:18:12 +05:30
|
|
|
resolvers,
|
|
|
|
});
|
2019-07-31 22:56:46 +05:30
|
|
|
};
|