// https://github.com/apollographql/meteor-integration/commit/9c0165bee4ede2c1a1b6a732235a1ebc0b02e20f
const {
ApolloClient,
createNetworkInterface,
createBatchingNetworkInterface
} = require('apollo-client');
require('isomorphic-fetch');
Novar.prototype.ApolloClient = {
createApolloNetworkInterface(givenConfig = {}) {
const config = { ...defaultNetworkInterfaceConfig, ...givenConfig };
// absoluteUrl adds a '/', so let's remove it first
let path = config.path;
if (path[0] === '/') {
path = path.slice(1);
}
const uri = this.absoluteUrl(
path,
{ rootUrl: this.Settings.getSetting('developmentServerIp', this.absoluteUrl()) },
);
// allow the use of a batching network interface;
// if the options.batchingInterface is not specified,
// fallback to the standard network interface
const interfaceToUse = config.batchingInterface ?
createBatchingNetworkInterface : createNetworkInterface;
// default interface options
const interfaceOptions = {
uri,
opts: {
credentials: 'same-origin', // http://dev.apollodata.com/react/auth.html#Cookie
},
};
// if a BatchingNetworkInterface is used with a correct batch interval, add it to the options
if (config.batchingInterface && config.batchInterval) {
interfaceOptions.batchInterval = config.batchInterval;
}
// if 'fetch' has been configured to be called with specific opts, add it to the options
if (Object.keys(config.opts).length > 0) {
interfaceOptions.opts = config.opts;
}
const networkInterface = interfaceToUse(interfaceOptions);
networkInterface.use([{
applyMiddleware(request, next) {
const currentUserToken = Novar.isClient() ?
global.localStorage[config.loginTokenKey] : config.loginToken;
if (!currentUserToken) {
next();
return;
}
if (!request.options.headers) {
request.options.headers = new Headers();
}
request.options.headers.Authorization = currentUserToken;
next();
},
}]);
return networkInterface;
}
}
// import { Novar } from './novar.js';
// import { Settings } from './settings.js';
// const ApolloClient = {};
// const defaultNetworkInterfaceConfig = {
// path: '/graphql', // default graphql server endpoint
// opts: {}, // additional fetch options like `credentials` or `headers`
// batchingInterface: false, // use a BatchingNetworkInterface by default instead of a NetworkInterface
// batchInterval: 10, // default batch interval
// };
// const createApolloNetworkInterface = (givenConfig = {}) => {
// const config = { ...defaultNetworkInterfaceConfig, ...givenConfig };
// // absoluteUrl adds a '/', so let's remove it first
// let path = config.path;
// if (path[0] === '/') {
// path = path.slice(1);
// }
// const uri = Novar.absoluteUrl(
// path,
// { rootUrl: Settings.getSetting('developmentServerIp', Novar.absoluteUrl()) },
// );
// // allow the use of a batching network interface; if the options.batchingInterface is not specified, fallback to the standard network interface
// const interfaceToUse = config.batchingInterface ? createBatchingNetworkInterface : createNetworkInterface;
// // default interface options
// const interfaceOptions = {
// uri,
// opts: {
// credentials: 'same-origin', // http://dev.apollodata.com/react/auth.html#Cookie
// },
// };
// // if a BatchingNetworkInterface is used with a correct batch interval, add it to the options
// if (config.batchingInterface && config.batchInterval) {
// interfaceOptions.batchInterval = config.batchInterval;
// }
// // if 'fetch' has been configured to be called with specific opts, add it to the options
// if (Object.keys(config.opts).length > 0) {
// interfaceOptions.opts = config.opts;
// }
// const networkInterface = interfaceToUse(interfaceOptions);
// networkInterface.use([{
// applyMiddleware(request, next) {
// const currentUserToken = Novar.isClient() ? global.localStorage[config.loginTokenKey] : config.loginToken;
// if (!currentUserToken) {
// next();
// return;
// }
// if (!request.options.headers) {
// request.options.headers = new Headers();
// }
// request.options.headers.Authorization = currentUserToken;
// next();
// },
// }]);
// return networkInterface;
// };
// const createApolloClientConfig = networkInterfaceConfig => ({
// ssrMode: Novar.isServer(),
// networkInterface: createApolloNetworkInterface(networkInterfaceConfig),
// queryDeduplication: true, // http://dev.apollodata.com/core/network.html#query-deduplication
// // 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;
// },
// });
// ApolloClient.createApolloClient = options => new Client(createApolloClientConfig(options));
// export { ApolloClient as default, ApolloClient };