2016-10-28 18:09:41 +02:00
|
|
|
// note(oct. 28, 2016)
|
|
|
|
// by-pass the meteor integration to use the features of apollo-client 0.5.x / graphql-server 0.4.x
|
|
|
|
|
|
|
|
// -------
|
|
|
|
// start of main-client from apollostack/meteor-integration
|
|
|
|
|
2016-11-16 16:17:12 +01:00
|
|
|
import ApolloClient, { createNetworkInterface } from 'apollo-client';
|
2016-10-28 18:09:41 +02:00
|
|
|
import { Accounts } from 'meteor/accounts-base';
|
|
|
|
import { _ } from 'meteor/underscore';
|
2016-11-16 16:17:12 +01:00
|
|
|
import 'isomorphic-fetch';
|
|
|
|
import Cookie from 'react-cookie';
|
2016-10-28 18:09:41 +02:00
|
|
|
|
2016-11-03 17:16:12 +09:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
|
|
|
|
2016-10-28 18:09:41 +02:00
|
|
|
const defaultNetworkInterfaceConfig = {
|
|
|
|
path: '/graphql',
|
|
|
|
options: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
export 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);
|
2016-11-16 16:17:12 +01:00
|
|
|
const networkInterface = createNetworkInterface({
|
2016-10-28 18:09:41 +02:00
|
|
|
uri: url,
|
2016-11-16 16:17:12 +01:00
|
|
|
opts: {
|
|
|
|
credentials: 'same-origin',
|
|
|
|
}
|
2016-10-28 18:09:41 +02:00
|
|
|
});
|
|
|
|
|
2016-11-16 16:17:12 +01:00
|
|
|
networkInterface.use([{
|
|
|
|
applyMiddleware(request, next) {
|
2016-11-16 16:26:53 +01:00
|
|
|
// console.log('from router token', config.cookieLoginToken);
|
|
|
|
// console.log('from accounts token', Meteor.isClient && Accounts._storedLoginToken());
|
2016-11-16 16:17:12 +01:00
|
|
|
const currentUserToken = config.cookieLoginToken ? config.cookieLoginToken : Meteor.isClient ? Accounts._storedLoginToken() : null;
|
2016-10-28 18:09:41 +02:00
|
|
|
|
2016-11-16 16:17:12 +01:00
|
|
|
if (!currentUserToken) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
2016-10-28 18:09:41 +02:00
|
|
|
|
2016-11-16 16:17:12 +01:00
|
|
|
if (!request.options.headers) {
|
|
|
|
request.options.headers = new Headers();
|
|
|
|
}
|
2016-10-28 18:09:41 +02:00
|
|
|
|
2016-11-16 16:17:12 +01:00
|
|
|
request.options.headers.Authorization = currentUserToken;
|
2016-10-28 18:09:41 +02:00
|
|
|
|
2016-11-16 16:17:12 +01:00
|
|
|
next();
|
|
|
|
},
|
|
|
|
}]);
|
2016-10-28 18:09:41 +02:00
|
|
|
|
|
|
|
return networkInterface;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const meteorClientConfig = (networkInterfaceConfig) => {
|
|
|
|
return {
|
2016-11-16 16:17:12 +01:00
|
|
|
ssrMode: Meteor.isServer,
|
2016-10-28 18:09:41 +02:00
|
|
|
networkInterface: createMeteorNetworkInterface(networkInterfaceConfig),
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// end of main-client from apollostack/meteor-integration
|
|
|
|
// --------
|
|
|
|
|
2016-11-16 16:34:37 +01:00
|
|
|
// export const client = new ApolloClient(meteorClientConfig());
|