mirror of
https://github.com/vale981/Vulcan
synced 2025-03-10 04:26:41 -04:00
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
![]() |
import ApolloClient, { createNetworkInterface } from 'apollo-client';
|
||
|
import 'isomorphic-fetch';
|
||
|
|
||
|
const defaultNetworkInterfaceConfig = {
|
||
|
path: '/graphql',
|
||
|
options: {},
|
||
|
};
|
||
|
|
||
|
const createMeteorNetworkInterface = (givenConfig = {}) => {
|
||
|
const config = _.extend(defaultNetworkInterfaceConfig, givenConfig);
|
||
|
|
||
|
// absoluteUrl adds a '/', so let's remove it first
|
||
|
let path = config.path;
|
||
|
if (path[0] === '/') {
|
||
|
path = path.slice(1);
|
||
|
}
|
||
|
|
||
|
// For SSR
|
||
|
const url = Meteor.absoluteUrl(path);
|
||
|
const networkInterface = createNetworkInterface({
|
||
|
uri: url,
|
||
|
opts: {
|
||
|
credentials: 'same-origin',
|
||
|
}
|
||
|
});
|
||
|
|
||
|
networkInterface.use([{
|
||
|
applyMiddleware(request, next) {
|
||
|
const currentUserToken = global.localStorage['Meteor.loginToken'];
|
||
|
|
||
|
if (!currentUserToken) {
|
||
|
next();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!request.options.headers) {
|
||
|
request.options.headers = new Headers();
|
||
|
}
|
||
|
|
||
|
request.options.headers.Authorization = currentUserToken;
|
||
|
|
||
|
next();
|
||
|
},
|
||
|
}]);
|
||
|
|
||
|
return networkInterface;
|
||
|
};
|
||
|
|
||
|
const meteorClientConfig = networkInterfaceConfig => ({
|
||
|
networkInterface: createMeteorNetworkInterface(networkInterfaceConfig),
|
||
|
queryDeduplication: true,
|
||
|
|
||
|
// Default to using Mongo _id, must use _id for queries.
|
||
|
dataIdFromObject(result) {
|
||
|
if (result._id && result.__typename) {
|
||
|
const dataId = result.__typename + result._id;
|
||
|
return dataId;
|
||
|
}
|
||
|
return null;
|
||
|
},
|
||
|
});
|
||
|
|
||
|
export const createApolloClient = options => new ApolloClient(meteorClientConfig(options));
|