mirror of
https://github.com/wassname/talk.git
synced 2026-07-14 11:18:50 +08:00
added tenant + graph support
This commit is contained in:
@@ -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' });
|
||||
@@ -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);
|
||||
};
|
||||
Reference in New Issue
Block a user