diff --git a/app/graphql/index.js b/app/graphql/index.js index 16a797ad2..6c39dd48d 100644 --- a/app/graphql/index.js +++ b/app/graphql/index.js @@ -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(` ${user.name} connected.`); + clients.set(webSocket, user); + return { user }; + }, + onDisconnect: webSocket => { + const user = clients.get(webSocket); + log.debug(` ${user.name} disconnected.`); } }, context: ({ req, connection }) => { diff --git a/app/graphql/schema/resolvers.js b/app/graphql/schema/resolvers.js index 1680c4b2a..de5766b8a 100644 --- a/app/graphql/schema/resolvers.js +++ b/app/graphql/schema/resolvers.js @@ -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);