Skip to content

Commit dc2ab98

Browse files
committed
Squashed 'app/packages/meteor-integration/' changes from 8535ff5..b1136d9
b1136d9 in progress for subscription test cases 4ecff7f updated meteor-integration git-subtree-dir: app/packages/meteor-integration git-subtree-split: b1136d93ed179db04561fc0dd4c59a3ac5fdd03f
1 parent 89f1c1d commit dc2ab98

File tree

6 files changed

+42
-57
lines changed

6 files changed

+42
-57
lines changed

.babelrc

Lines changed: 0 additions & 6 deletions
This file was deleted.

main-client.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@ import { Accounts } from 'meteor/accounts-base';
55
import { Meteor } from 'meteor/meteor';
66
import { _ } from 'meteor/underscore';
77
import { print } from 'graphql-tag/printer';
8-
import { Client } from 'subscriptions-transport-ws';
8+
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws';
99

1010
const defaultNetworkInterfaceConfig = {
1111
path: '/graphql', // default graphql server endpoint
1212
opts: {}, // additional fetch options like `credentials` or `headers`
1313
useMeteorAccounts: true, // if true, send an eventual Meteor login token to identify the current user with every request
1414
batchingInterface: true, // use a BatchingNetworkInterface by default instead of a NetworkInterface
1515
batchInterval: 10, // default batch interval
16+
useSubscription: true, // enable subscription by default
1617
};
1718

18-
const getDefaultWsClient = () => new Client('ws://localhost:8080');
19+
const getDefaultWsClient = () => new SubscriptionClient('ws://localhost:8080', {
20+
reconnect: true,
21+
// connectionParams: {
22+
// // Pass any arguments you want for initialization
23+
// },
24+
});
1925

2026
export const createMeteorNetworkInterface = (givenConfig) => {
2127
const config = _.extend(defaultNetworkInterfaceConfig, givenConfig);
22-
const wsClient = givenConfig && givenConfig.wsClient ? givenConfig.wsClient : getDefaultWsClient();
2328
// absoluteUrl adds a '/', so let's remove it first
2429
let path = config.path;
2530
if (path[0] === '/') {
@@ -30,17 +35,17 @@ export const createMeteorNetworkInterface = (givenConfig) => {
3035
const interfaceToUse = config.batchingInterface ? createBatchingNetworkInterface : createNetworkInterface;
3136

3237
// default interface options
33-
let interfaceOptions = {
38+
const interfaceOptions = {
3439
uri: Meteor.absoluteUrl(path),
3540
};
3641

3742
// if a BatchingNetworkInterface is used with a correct batch interval, add it to the options
38-
if(config.batchingInterface && config.batchInterval) {
43+
if (config.batchingInterface && config.batchInterval) {
3944
interfaceOptions.batchInterval = config.batchInterval;
4045
}
4146

4247
// if 'fetch' has been configured to be called with specific opts, add it to the options
43-
if(!_.isEmpty(config.opts)) {
48+
if (!_.isEmpty(config.opts)) {
4449
interfaceOptions.opts = config.opts;
4550
}
4651

@@ -49,7 +54,6 @@ export const createMeteorNetworkInterface = (givenConfig) => {
4954
if (config.useMeteorAccounts) {
5055
networkInterface.use([{
5156
applyMiddleware(request, next) {
52-
5357
// cookie login token created by meteorhacks:fast-render and caught during server-side rendering by rr:react-router-ssr
5458
const { loginToken: cookieLoginToken } = config;
5559
// Meteor accounts-base login token stored in local storage, only exists client-side
@@ -82,31 +86,16 @@ export const createMeteorNetworkInterface = (givenConfig) => {
8286
}
8387

8488
if (config.useSubscription) {
85-
return _.extend(networkInterface, {
86-
subscribe: (request, handler) => wsClient.subscribe({
87-
query: print(request.query),
88-
variables: request.variables,
89-
}, handler),
90-
unsubscribe: (id) => {
91-
wsClient.unsubscribe(id);
92-
},
93-
});
89+
const wsClient = givenConfig && givenConfig.wsClient ? givenConfig.wsClient : getDefaultWsClient();
90+
return addGraphQLSubscriptions(networkInterface, wsClient);
9491
}
92+
9593
return networkInterface;
9694
};
9795

9896
export const meteorClientConfig = (networkInterfaceConfig) => {
99-
const networkInterface = createMeteorNetworkInterface(networkInterfaceConfig);
100-
let { initialState } = networkInterface;
101-
if(initialState){
102-
// Temporary workaround for bug in AC@0.5.0: https://github.com/apollostack/apollo-client/issues/845
103-
delete initialState.apollo.queries;
104-
delete initialState.apollo.mutations;
105-
}
106-
10797
return {
108-
networkInterface,
109-
initialState,
98+
networkInterface: createMeteorNetworkInterface(networkInterfaceConfig),
11099
ssrMode: Meteor.isServer,
111100

112101
// Default to using Mongo _id, must use _id for queries.

main-server.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export { createMeteorNetworkInterface, meteorClientConfig } from './main-client'
1616
const defaultConfig = {
1717
path: '/graphql',
1818
maxAccountsCacheSizeInMB: 1,
19-
graphiql : Meteor.isDevelopment,
20-
graphiqlPath : '/graphiql',
21-
graphiqlOptions : {
22-
passHeader : "'Authorization': localStorage['Meteor.loginToken']"
19+
graphiql: Meteor.isDevelopment,
20+
graphiqlPath: '/graphiql',
21+
graphiqlOptions: {
22+
passHeader: "'Authorization': localStorage['Meteor.loginToken']"
2323
},
2424
useSubscription: true,
2525
subscriptionPort: 8080,
26-
configServer: (graphQLServer) => {},
26+
configServer: (graphQLServer) => { },
2727
};
2828

2929
const defaultOptions = {
@@ -51,7 +51,7 @@ export const createApolloServer = (givenOptions = {}, givenConfig = {}) => {
5151
// GraphQL endpoint
5252
graphQLServer.use(config.path, bodyParser.json(), graphqlExpress(async (req) => {
5353
let options,
54-
user = null;
54+
user = null;
5555

5656
if (_.isFunction(givenOptions))
5757
options = givenOptions(req);
@@ -75,7 +75,7 @@ export const createApolloServer = (givenOptions = {}, givenConfig = {}) => {
7575

7676
// Get the user from the database
7777
user = await Meteor.users.findOne(
78-
{"services.resume.loginTokens.hashedToken": hashedToken}
78+
{ "services.resume.loginTokens.hashedToken": hashedToken }
7979
);
8080

8181
if (user) {
@@ -95,11 +95,9 @@ export const createApolloServer = (givenOptions = {}, givenConfig = {}) => {
9595

9696
// Start GraphiQL if enabled
9797
if (config.graphiql) {
98-
graphQLServer.use(config.graphiqlPath, graphiqlExpress(_.extend(config.graphiqlOptions, {endpointURL : config.path})));
98+
graphQLServer.use(config.graphiqlPath, graphiqlExpress(_.extend(config.graphiqlOptions, { endpointURL: config.path })));
9999
}
100100

101-
// create http server for subscription
102-
const server = createServer(graphQLServer);
103101

104102
// This binds the specified paths to the Express server running Apollo + GraphiQL
105103
WebApp.connectHandlers.use(Meteor.bindEnvironment(graphQLServer));
@@ -109,13 +107,23 @@ export const createApolloServer = (givenOptions = {}, givenConfig = {}) => {
109107
if (!subscriptionManager) {
110108
throw new Meteor.Error('SubscriptionManager which is mandatory missing.');
111109
}
112-
new SubscriptionServer({
113-
subscriptionManager,
114-
}, server);
115110
try {
111+
// create http server for subscription
112+
// const server = createServer(graphQLServer);
113+
// Create WebSocket listener server
114+
const server = createServer((request, response) => {
115+
response.writeHead(404);
116+
response.end();
117+
});
116118
server.listen(config.subscriptionPort, () => {
117119
console.log('Subscription manager running ' + config.subscriptionPort);
118120
});
121+
const subscriptionServer = new SubscriptionServer({
122+
subscriptionManager,
123+
}, {
124+
server,
125+
path: '/'
126+
});
119127
} catch (e) {
120128
console.log(e);
121129
}

package.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@
2020
"homepage": "https://github.com/apollostack/meteor-integration#readme",
2121
"dependencies": {
2222
"apollo-client": "^0.8.4",
23-
"babel": "^6.5.2",
24-
"babel-core": "^6.22.1",
25-
"babel-eslint": "^7.1.1",
26-
"babel-plugin-add-module-exports": "^0.2.1",
27-
"babel-preset-es2015": "^6.22.0",
28-
"babel-preset-stage-2": "^6.22.0",
29-
"body-parser": "^1.15.2",
3023
"express": "^4.14.0",
3124
"graphql": "^0.9.1",
3225
"graphql-server-express": "^0.6.0",

tests/client.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ const personResult = {
3838
},
3939
};
4040

41-
describe('Network interface', function() {
41+
describe('Network interface without subscription enabled', function() {
4242

4343
it('should create a network interface', function() {
44-
assert.ok(createMeteorNetworkInterface({batchingInterface: false}));
44+
assert.ok(createMeteorNetworkInterface({batchingInterface: false, useSubscription: false}));
4545
});
4646

4747
});
4848

49-
describe('Batching network interface', function() {
49+
describe('Batching network interface without subscription enabled', function() {
5050

5151
// from apollo-client/src/transport/networkInterface
5252
const printRequest = request => ({...request, query: print(request.query)});
@@ -60,6 +60,7 @@ describe('Batching network interface', function() {
6060
}) => {
6161
const batchedNetworkInterface = createMeteorNetworkInterface({
6262
batchingInterface: true,
63+
useSubscription: false,
6364
opts
6465
});
6566

tests/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ describe('Graphql Server', function() {
3434

3535
const schema = makeExecutableSchema({ typeDefs, resolvers, });
3636

37-
it('should create an express graphql server accepting a test query', async function() {
37+
it('should create an express graphql server accepting a test query (without subscription enabled)', async function() {
3838

3939
// instantiate the apollo server
40-
const apolloServer = createApolloServer({ schema, });
40+
const apolloServer = createApolloServer({ schema,}, { useSubscription: false});
4141

4242
// send a query to the server
4343
const { data: queryResult } = await HTTP.post(Meteor.absoluteUrl('/graphql'), {

0 commit comments

Comments
 (0)