@@ -5,21 +5,26 @@ import { Accounts } from 'meteor/accounts-base';
55import { Meteor } from 'meteor/meteor' ;
66import { _ } from 'meteor/underscore' ;
77import { print } from 'graphql-tag/printer' ;
8- import { Client } from 'subscriptions-transport-ws' ;
8+ import { SubscriptionClient , addGraphQLSubscriptions } from 'subscriptions-transport-ws' ;
99
1010const 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
2026export 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
9896export 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.
0 commit comments