diff --git a/app.js b/app.js index b65126aa0..c180d2c04 100644 --- a/app.js +++ b/app.js @@ -1,8 +1,10 @@ const express = require('express'); const morgan = require('morgan'); const path = require('path'); +const uuid = require('uuid'); const merge = require('lodash/merge'); const helmet = require('helmet'); +const plugins = require('./services/plugins'); const compression = require('compression'); const {HELMET_CONFIGURATION} = require('./config'); const {MOUNT_PATH} = require('./url'); @@ -11,6 +13,25 @@ const debug = require('debug')('talk:app'); const app = express(); +// Request Identity Middleware +app.use((req, res, next) => { + req.id = uuid.v4(); + + next(); +}); + +//============================================================================== +// PLUGIN PRE APPLICATION MIDDLEWARE +//============================================================================== + +// Inject server route plugins. +plugins.get('server', 'app').forEach(({plugin, app: callback}) => { + debug(`added plugin '${plugin.name}'`); + + // Pass the app to the plugin to mount it's routes. + callback(app); +}); + //============================================================================== // APPLICATION WIDE MIDDLEWARE //============================================================================== diff --git a/graph/context.js b/graph/context.js index d6d85a651..e894c1534 100644 --- a/graph/context.js +++ b/graph/context.js @@ -42,14 +42,15 @@ const decorateContextPlugins = (context, contextPlugins) => { * Stores the request context. */ class Context { - constructor({user = null}) { + constructor(parent) { - // Generate a new context id for the request. - this.id = uuid.v4(); + // Generate a new context id for the request if the parent doesn't provide + // one. + this.id = parent.id || uuid.v4(); - // Load the current logged in user to `user`, otherwise this'll be null. - if (user) { - this.user = user; + // Load the current logged in user to `user`, otherwise this will be null. + if (parent.user) { + this.user = parent.user; } // Attach the connectors. @@ -66,6 +67,9 @@ class Context { // Bind the publish/subscribe to the context. this.pubsub = pubsub.getClient(); + + // Bind the parent context. + this.parent = parent; } } diff --git a/plugins.js b/plugins.js index dfe3d3312..487825be7 100644 --- a/plugins.js +++ b/plugins.js @@ -55,7 +55,8 @@ const hookSchemas = { loaders: Joi.func().maxArity(1), mutators: Joi.func().maxArity(1), resolvers: Joi.object().pattern(/\w/, Joi.object().pattern(/(?:__resolveType|\w+)/, Joi.func())), - typeDefs: Joi.string() + typeDefs: Joi.string(), + schemaLevelResolveFunction: Joi.func(), }; /** @@ -172,7 +173,7 @@ class PluginSection { if (this.required) { return; } - + this.required = true; this.plugins.forEach((plugin) => {