added tenant + graph support

This commit is contained in:
Wyatt Johnson
2018-06-16 17:21:04 -06:00
parent 02e1236792
commit 4318e1ddbe
35 changed files with 1024 additions and 269 deletions
+50 -15
View File
@@ -1,16 +1,59 @@
import express, { Express } from 'express';
import express, { Express, Router } from 'express';
import { Db } from 'mongodb';
import { Config } from 'talk-server/config';
import schema from 'talk-server/graph/tenant/schema';
import { create } from 'talk-server/services/mongodb';
import serveStatic from './middleware/serveStatic';
import graphql from './middleware/graphql';
import graphiql from './middleware/graphiql';
import playground from './middleware/playground';
import {
access as accessLogger,
error as errorLogger,
} from './middleware/logging';
import tenantGraphMiddleware from 'talk-server/graph/tenant/middleware';
import schema from 'talk-server/graph/schema';
import { create } from 'talk-server/services/mongodb';
async function createTenantRouter(config: Config, db: Db): Promise<Router> {
const router = express.Router({ mergeParams: true });
if (config.get('env') === 'development') {
// GraphiQL
router.get(
'/graphiql',
playground(req => ({
endpoint: `/api/tenant/${req.params.tenantID}/graphql`,
}))
);
}
// Tenant API
router.use('/graphql', express.json(), tenantGraphMiddleware(db));
return router;
}
async function createAPIRouter(config: Config, db: Db): Promise<Router> {
// Create a router.
const router = express.Router({ mergeParams: true });
// Configure the tenant routes.
router.use('/tenant/:tenantID', await createTenantRouter(config, db));
return router;
}
async function createRouter(config: Config): Promise<Router> {
// Setup MongoDB.
const db = await create(config);
// Create a router.
const router = express.Router({ mergeParams: true });
router.use('/api', await createAPIRouter(config, db));
return router;
}
/**
* createApp will create a Talk Express app that can be used to handle requests.
@@ -27,16 +70,8 @@ export async function createApp(
// Static Files
app.use(serveStatic);
if (config.get('env') === 'development') {
// GraphiQL
app.get('/graphiql', graphiql());
}
// Setup MongoDB.
const db = await create(config);
// API
app.use('/api/graphql', express.json(), graphql({ schema, db }));
// Mount the router.
app.use(await createRouter(config));
// Error Handling
app.use(errorLogger);
@@ -1,3 +0,0 @@
import playground from 'graphql-playground-middleware-express';
export default () => playground({ endpoint: '/api/graphql' });
-15
View File
@@ -1,15 +0,0 @@
import { graphqlExpress } from 'apollo-server-express';
import { GraphQLSchema } from 'graphql';
import Context from 'talk-server/graph/context';
import { Db } from 'mongodb';
export interface GraphQLOptions {
schema: GraphQLSchema;
db: Db;
}
export default (opts: GraphQLOptions) =>
graphqlExpress(req => ({
schema: opts.schema,
context: new Context({ db: opts.db, req }),
}));
@@ -0,0 +1,16 @@
import { Request, RequestHandler } from 'express';
import { MiddlewareOptions } from 'graphql-playground-html';
import playground from 'graphql-playground-middleware-express';
export type PlaygroundFn = (req: Request) => MiddlewareOptions;
export default (fn: PlaygroundFn): RequestHandler => (req, res, next) => {
// Generate the options.
const options: MiddlewareOptions = fn(req);
// Create the playground handler.
const handler = playground(options);
// Execute it.
handler(req, res, next);
};