I have one working solution: the idea is to not create one instance of SubscriptionClient
for the whole application. Instead, I’m creating the clients for each connection to the proxy server:
server.start({
port: 4000,
subscriptions: {
onConnect: (connectionParams, websocket, context) => {
return {
subscriptionClients: {
messageService: new SubscriptionClient(process.env.MESSAGE_SERVICE_SUBSCRIPTION_URL, {
connectionParams,
reconnect: true,
}, ws)
}
};
},
onDisconnect: async (websocket, context) => {
const params = await context.initPromise;
const { subscriptionClients } = params;
for (const key in subscriptionClients) {
subscriptionClients[key].close();
}
}
}
}, (options) => console.log('Server is running on http://localhost:4000'))
if you would have more remote schemas you would just create more instances of SubscriptionClient
in the subscriptionClients
map.
To use those clients in the remote schema you need to do two things:
-
expose them in the context:
const server = new GraphQLServer({ schema, context: ({ connection }) => { if (connection && connection.context) { return connection.context; } } });
-
use custom link implementation instead of WsLink
(operation, forward) => { const context = operation.getContext(); const { graphqlContext: { subscriptionClients } } = context; return subscriptionClients && subscriptionClients[clientName] && subscriptionClients[clientName].request(operation); };
In this way, the whole connection params will be passed to the remote server.
The whole example can be found here: https://gist.github.com/josephktcheung/cd1b65b321736a520ae9d822ae5a951b
Disclaimer:
The code is not mine, as @josephktcheung outrun me with providing an example. I just helped with it a little. Here is the original discussion: https://github.com/apollographql/graphql-tools/issues/864