Skip to content
This repository was archived by the owner on Nov 11, 2018. It is now read-only.

Commit a64cab9

Browse files
author
Gery Hirschfeld
committed
feat(routes): Moves routes to separate files
1 parent 3a817e8 commit a64cab9

File tree

8 files changed

+134
-129
lines changed

8 files changed

+134
-129
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ express-graphql-typescript-boilerplate
131131
| |-- services/ * use a services to separate the logic that retrieves the data and maps it to the entity model from the business logic that acts on the model
132132
| | |-- **/*Services.ts
133133
| |
134+
| |-- routes/ * defines our application routes
135+
| | |-- **/*Routes.ts
136+
| |
134137
| |-- schemas/ * our graphql schema definitions (use a single file for every graphql object action)
135138
| | |-- arguments/ * our graphql argument files
136139
| | |-- fields/ * our graphql field files
@@ -139,7 +142,6 @@ express-graphql-typescript-boilerplate
139142
| | |-- types/ * our graphql type files
140143
| |
141144
| |-- index.ts * main entry point for our application
142-
| |-- App.ts * our application
143145
| |-- RootValue.ts * RootValue with some functions for all the queries and mutations
144146
| |-- config.ts * has our configuration for our different environments
145147
|

src/App.ts

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/context/index.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,3 @@
11
export * from './Context';
22
export * from './DataloadersContext';
33
export * from './ServicesContext';
4-
5-
import { DB } from '../core/Database';
6-
import {
7-
AuthorRepository,
8-
BookRepository
9-
} from '../repositories';
10-
11-
12-
/**
13-
* Services
14-
*/
15-
import { ServicesContext } from './ServicesContext';
16-
17-
import {
18-
BookService,
19-
AuthorService
20-
} from '../services';
21-
22-
ServicesContext.getInstance()
23-
.setBookService(new BookService(new BookRepository(DB)))
24-
.setAuthorService(new AuthorService(new AuthorRepository(DB)));
25-
26-
27-
/**
28-
* Dataloaders
29-
*/
30-
import { DataLoadersContext } from './DataloadersContext';
31-
DataLoadersContext.getInstance()
32-
.setAuthorDataLoader(ServicesContext.getInstance().AuthorService)
33-
.setBookDataLoader(ServicesContext.getInstance().BookService);

src/core/Server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as express from 'express';
22
import * as http from 'http';
33

4-
import { Logger } from './logger';
4+
import { Environment, Logger } from './';
5+
56
const log = Logger('app:core:server');
67

78

@@ -15,6 +16,7 @@ export class Server {
1516
const server = app.listen(this.normalizePort(port));
1617
server.on('listening', () => this.onListening(server));
1718
server.on('error', (error) => this.onError(server, error));
19+
log.debug('Server was started on environment %s', Environment.getName());
1820
return server;
1921
}
2022

src/index.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,49 @@
99
*
1010
*/
1111

12-
import { App } from './app';
13-
App.getInstance().main();
12+
// Core elements to get the server started
13+
import {
14+
Environment,
15+
Server,
16+
winstonStream,
17+
debugStream
18+
} from './core';
19+
20+
// Import all express libs
21+
import * as helmet from 'helmet';
22+
import * as morgan from 'morgan';
23+
import * as cors from 'cors';
24+
25+
// Import our middlewares to add to the express chain
26+
import { oauth } from './middlewares';
27+
28+
// Import all routes
29+
import { DefaultRoutes, GraphQLRoutes } from './routes';
30+
31+
// Create a new express app
32+
const app = Server.init();
33+
34+
// Helmet helps you secure your Express apps by setting various HTTP headers
35+
app.use(helmet());
36+
app.use(helmet.noCache());
37+
app.use(helmet.hsts({
38+
maxAge: 31536000,
39+
includeSubdomains: true
40+
}));
41+
42+
// Enable cors for all routes and origins
43+
app.use(cors());
44+
45+
// Adds winston logger to the express framework
46+
app.use(morgan('dev', debugStream));
47+
app.use(morgan('combined', winstonStream));
48+
49+
// Our custom oauth middleware
50+
app.use(oauth({}));
51+
52+
// Map routes to the express application
53+
DefaultRoutes.map(app);
54+
GraphQLRoutes.map(app);
55+
56+
// Starts the server and listens for common errors
57+
Server.run(app, Environment.getConfig().server.port);

src/routes/DefaultRoutes.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as express from 'express';
2+
3+
4+
export class DefaultRoutes {
5+
6+
static map(app: express.Application): void {
7+
app.use('/', (req: express.Request, res: express.Response) => {
8+
const pkg = require('../../package.json');
9+
res.json({
10+
name: pkg.name,
11+
version: pkg.version,
12+
description: pkg.description
13+
});
14+
});
15+
}
16+
17+
}
18+
19+

src/routes/GraphQLRoutes.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as express from 'express';
2+
import * as GraphQLHTTP from 'express-graphql';
3+
4+
import { Environment, DB } from '../core';
5+
import { Exception } from '../exceptions';
6+
import { Schema } from '../schemas';
7+
import { RootValue } from '../RootValue';
8+
import {
9+
Context,
10+
DataLoadersContext,
11+
ServicesContext
12+
} from '../context';
13+
import {
14+
AuthorRepository,
15+
BookRepository
16+
} from '../repositories';
17+
import {
18+
BookService,
19+
AuthorService
20+
} from '../services';
21+
22+
23+
export class GraphQLRoutes {
24+
25+
static map(app: express.Application): void {
26+
GraphQLRoutes.buildContext();
27+
28+
// Add GraphQL to express route
29+
app.use('/graphql', (req: express.Request, res: express.Response) => {
30+
// Creates a GraphQLHTTP per request
31+
GraphQLHTTP({
32+
schema: Schema.get(),
33+
rootValue: new RootValue(),
34+
context: new Context(
35+
req, res,
36+
DataLoadersContext.getInstance(),
37+
ServicesContext.getInstance()
38+
),
39+
graphiql: Environment.getConfig().server.graphiql,
40+
formatError: exception => ({
41+
name: Exception.getName(exception.message),
42+
message: Exception.getMessage(exception.message),
43+
path: exception.path
44+
})
45+
})(req, res);
46+
});
47+
}
48+
49+
private static buildContext(): void {
50+
ServicesContext.getInstance()
51+
.setBookService(new BookService(new BookRepository(DB)))
52+
.setAuthorService(new AuthorService(new AuthorRepository(DB)));
53+
54+
DataLoadersContext.getInstance()
55+
.setAuthorDataLoader(ServicesContext.getInstance().AuthorService)
56+
.setBookDataLoader(ServicesContext.getInstance().BookService);
57+
}
58+
59+
}
60+
61+

src/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './DefaultRoutes';
2+
export * from './GraphQLRoutes';

0 commit comments

Comments
 (0)