initial commit

This commit is contained in:
Wyatt Johnson
2018-06-16 17:20:51 -06:00
commit 02e1236792
39 changed files with 5258 additions and 0 deletions
@@ -0,0 +1,3 @@
import playground from 'graphql-playground-middleware-express';
export default () => playground({ endpoint: '/api/graphql' });
+15
View File
@@ -0,0 +1,15 @@
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 }),
}));
+43
View File
@@ -0,0 +1,43 @@
import { RequestHandler, ErrorRequestHandler } from 'express';
import logger from '../../logger';
import now from 'performance-now';
export const access: RequestHandler = (req, res, next) => {
const startTime = now();
const end = res.end;
res.end = (chunk: any, encodingOrCb?: string | Function, cb?: Function) => {
// Compute the end time.
const responseTime = Math.round(now() - startTime);
// Get some extra goodies from the request.
const userAgent = req.get('User-Agent');
// Reattach the old end, and finish.
res.end = end;
if (typeof encodingOrCb === 'function') {
res.end(chunk, encodingOrCb);
} else {
res.end(chunk, encodingOrCb, cb);
}
// Log this out.
logger.info(
{
// traceID: req.id,
url: req.originalUrl || req.url,
method: req.method,
statusCode: res.statusCode,
userAgent,
responseTime,
},
'http request'
);
};
next();
};
export const error: ErrorRequestHandler = (err, req, res, next) => {
logger.error({ err }, 'http error');
next(err);
};
@@ -0,0 +1,4 @@
import serveStatic from 'express-static-gzip';
import path from 'path';
export default serveStatic(path.join(__dirname, '..', '..', 'dist'), {});