add log for ws dis/connect and fix publish for non-plugin modules

Signed-off-by: Alexis Tyler <xo@wvvw.me>
This commit is contained in:
Alexis Tyler
2019-08-25 16:44:15 +09:30
parent a1c81a1768
commit f3fd17597f
2 changed files with 28 additions and 5 deletions

View File

@@ -277,22 +277,31 @@ module.exports = function (
throw new AppError('Invalid apikey.');
}
};
// Connected ws clients
const clients = new Map();
return {
schema,
types,
resolvers,
subscriptions: {
onConnect: connectionParams => {
console.log(`Subscription client connected using Apollo server's built-in SubscriptionServer.`)
onConnect: (connectionParams, webSocket) => {
const apiKey = connectionParams['x-api-key'];
ensureApiKey(apiKey);
const user = Users.findOne({ apiKey }) || { name: 'guest', apiKey, role: 'guest' };
log.debug(`<ws> ${user.name} connected.`);
clients.set(webSocket, user);
return {
user
};
},
onDisconnect: webSocket => {
const user = clients.get(webSocket);
log.debug(`<ws> ${user.name} disconnected.`);
}
},
context: ({ req, connection }) => {

View File

@@ -3,7 +3,7 @@
* Written by: Alexis Tyler
*/
module.exports = function ($injector, GraphQLJSON, GraphQLLong, GraphQLUUID, pubsub, setIntervalAsync, PluginManager) {
module.exports = function ($injector, GraphQLJSON, GraphQLLong, GraphQLUUID, pubsub, setIntervalAsync, PluginManager, log) {
const publish = (channel, mutation, {
node = undefined,
moduleToRun = undefined,
@@ -34,15 +34,29 @@ module.exports = function ($injector, GraphQLJSON, GraphQLLong, GraphQLUUID, pub
result = result();
}
if (filePath) {
const [pluginName, moduleName] = channel.split('/');
log.debug('Plugin:', pluginName, 'Module:', moduleName, 'Result:', result);
} else {
log.debug('Module:', channel, 'Result:', result);
}
// Update "node"
pubsub.publish(channel, {
pluginModule: {
[filePath ? 'pluginModule' : channel]: {
mutation,
node: result.json
}
});
} catch (error) {
console.error(error);
// Ensure we aren't leaking anything in production
if (process.env.NODE_ENV === 'production') {
log.debug('Error:', error.message);
}
const logger = log[error.status && error.status >= 400 ? 'error' : 'warn'];
logger('Error:', error.message);
clearInterval(timer);
}
}, interval);