diff --git a/graph/connectors.js b/graph/connectors.js index d7c2f87d8..15220b2d5 100644 --- a/graph/connectors.js +++ b/graph/connectors.js @@ -5,7 +5,8 @@ const merge = require('lodash/merge'); const errors = require('../errors'); // Graph -const subscriptions = require('./subscriptions'); +const { getBroker } = require('./subscriptions/broker'); +const { getPubsub } = require('./subscriptions/pubsub'); const resolvers = require('./resolvers'); const mutators = require('./mutators'); const loaders = require('./loaders'); @@ -85,7 +86,7 @@ const defaultConnectors = { Wordlist, }, graph: { - subscriptions, + subscriptions: { getBroker, getPubsub }, resolvers, mutators, loaders, diff --git a/graph/subscriptions/index.js b/graph/subscriptions/index.js index 40c1c4437..5ade76feb 100644 --- a/graph/subscriptions/index.js +++ b/graph/subscriptions/index.js @@ -2,7 +2,6 @@ const { SubscriptionManager } = require('graphql-subscriptions'); const { SubscriptionServer } = require('subscriptions-transport-ws'); const debug = require('debug')('talk:graph:subscriptions'); -const { getBroker } = require('./broker'); const { getPubsub } = require('./pubsub'); const schema = require('../schema'); const Context = require('../context'); @@ -116,6 +115,4 @@ const createSubscriptionManager = server => module.exports = { createSubscriptionManager, - getBroker, - getPubsub, }; diff --git a/middleware/authentication.js b/middleware/authentication.js index b9663e768..5f6009136 100644 --- a/middleware/authentication.js +++ b/middleware/authentication.js @@ -1,5 +1,4 @@ const { passport } = require('../services/passport'); -const Context = require('../graph/context'); const debug = require('debug')('talk:middleware:authentication'); const authentication = (req, res, next) => @@ -23,9 +22,6 @@ const authentication = (req, res, next) => debug('user was not on request'); } - // Attach a new context to the request. - req.context = new Context(req); - next(); } )(req, res, next); diff --git a/middleware/context.js b/middleware/context.js new file mode 100644 index 000000000..57620f827 --- /dev/null +++ b/middleware/context.js @@ -0,0 +1,8 @@ +const Context = require('../graph/context'); + +// Attach a new context to the request. +module.exports = (req, res, next) => { + req.context = new Context(req); + + next(); +}; diff --git a/routes/index.js b/routes/index.js index 88c615281..143ceefd8 100644 --- a/routes/index.js +++ b/routes/index.js @@ -13,6 +13,7 @@ const staticMiddleware = require('express-static-gzip'); const { DISABLE_STATIC_SERVER } = require('../config'); const { passport } = require('../services/passport'); const { MOUNT_PATH } = require('../url'); +const context = require('../middleware/context'); const router = express.Router(); @@ -100,9 +101,12 @@ plugins.get('server', 'passport').forEach(plugin => { // Setup the PassportJS Middleware. router.use(passport.initialize()); +// Setup the Graph Context on the router. +router.use(authentication, context); + // Attach the authentication middleware, this will be responsible for decoding // (if present) the JWT on the request. -router.use('/api', authentication, require('./api')); +router.use('/api', require('./api')); //============================================================================== // DEVELOPMENT ROUTES diff --git a/routes/plugins.js b/routes/plugins.js index 621cf172d..0e5a7636a 100644 --- a/routes/plugins.js +++ b/routes/plugins.js @@ -1,17 +1,9 @@ const express = require('express'); const debug = require('debug')('talk:routes:plugins'); const plugins = require('../services/plugins'); -const { connectors } = require('../graph'); -const { set } = require('lodash'); const router = express.Router(); -// Mount the connectors on the router. -router.use((req, res, next) => { - set(req, 'talk.connectors', connectors); - next(); -}); - // Inject server route plugins. plugins.get('server', 'router').forEach(plugin => { debug(`added plugin '${plugin.plugin.name}'`);