Skip to content

Commit 618b972

Browse files
committed
structure set up
1 parent c0de712 commit 618b972

29 files changed

+628
-618
lines changed

.DS_Store

6 KB
Binary file not shown.

.dockerignore

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

.editorconfig

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

.gqlconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
schema: {
3+
files: '**/*.gql'
4+
}
5+
}

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,13 @@ Files explained:
3030
----
3131
1. src - directory is used for typescript code that is part of the project
3232
1a. main.ts - Main server file. (Starting Apollo server)
33-
1b. main.spec.ts - Tests file for main
34-
1c. schema - Module used to build schema
33+
1c. dw/schema - Contains modules used to build dataware house schemas
3534
- index.ts - simple logic to merge all modules into a schema using graphql-tools
36-
- modules/ - directory for modules to be used with graphql-tools
37-
1c. schema.spec.ts - Basic test for schema.
38-
1c. main.test.ts - Main for tests runner.
35+
- modules/ - directory for modules
3936
3. package.json - file is used to describe the library
4037
4. tsconfig.json - configuration file for the library compilation
4138
6. tslint.json - configuration file for the linter
42-
7. typings.json - typings needed for the server
4339
8. webpack.config.js - configuration file of the compilation automation process for the library
44-
9. webpack.config.test.js - configuration file of the compilation when testing
4540
10. Dockerfile - Dockerfile used to describe how to make a container out of apollo server
4641

4742

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"build:watch": "webpack --watch",
99
"pretest": "npm run build",
1010
"test": "jest",
11+
"lint": "tslint . --ext js,gql,graphql",
1112
"test:watch": "npm test -- --watch",
1213
"test:notify": "npm run test:watch -- --notify",
1314
"coverage": "npm test -- --coverage",
@@ -37,24 +38,30 @@
3738
},
3839
"homepage": "https://github.com/datahub/datahub-api#readme",
3940
"dependencies": {
40-
"@types/body-parser": "1.16.3",
41-
"@types/cors": "2.8.1",
42-
"@types/express": "^4.0.35",
43-
"@types/helmet": "0.0.35",
44-
"@types/morgan": "^1.7.32",
4541
"body-parser": "^1.17.1",
42+
"compression": "^1.6.2",
4643
"cors": "^2.8.3",
44+
"dataloader": "^1.3.0",
4745
"express": "^4.15.2",
4846
"graphql": "0.10.0",
4947
"graphql-server-express": "^0.7.2",
5048
"graphql-tools": "0.11.0",
5149
"helmet": "^3.6.0",
52-
"morgan": "^1.8.1"
50+
"merge-graphql-schemas": "^0.0.17",
51+
"morgan": "^1.8.1",
52+
"ramda": "^0.24.0"
5353
},
5454
"devDependencies": {
55+
"@playlyfe/gql": "^2.3.1",
56+
"@types/body-parser": "1.16.3",
57+
"@types/cors": "2.8.1",
58+
"@types/express": "^4.0.35",
5559
"@types/graphql": "^0.9.1",
60+
"@types/helmet": "0.0.35",
5661
"@types/jest": "^19.2.3",
57-
"@types/node": "^7.0.18",
62+
"@types/morgan": "^1.7.32",
63+
"@types/node": "^7.0.22",
64+
"@types/ramda": "^0.0.10",
5865
"awesome-typescript-loader": "^3.1.3",
5966
"concurrently": "^3.4.0",
6067
"jest": "^20.0.1",

src/__snapshots__/schema.spec.ts.snap

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

src/data-base/person-database.ts

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

src/main.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import * as express from 'express';
21
import * as bodyParser from 'body-parser';
3-
import {graphqlExpress, graphiqlExpress} from 'graphql-server-express';
4-
import {Schema} from './schema';
2+
import compression from 'compression';
53
import * as cors from 'cors';
4+
import * as express from 'express';
5+
import {graphiqlExpress, graphqlExpress} from 'graphql-server-express';
66
import * as helmet from 'helmet';
77
import * as morgan from 'morgan';
8-
import {persons, findPerson, addPerson} from './data-base/person-database';
8+
import * as path from 'path';
9+
import { mergeGraphqlSchemas } from 'merge-graphql-schemas';
10+
11+
const Schema = mergeGraphqlSchemas(path.join(__dirname, './schema'));
912

1013
// Default port or given one.
11-
export const GRAPHQL_ROUTE = "/graphql";
12-
export const GRAPHIQL_ROUTE = "/graphiql";
14+
export const GRAPHQL_ROUTE = '/graphql';
15+
export const GRAPHIQL_ROUTE = '/graphiql';
1316

1417
interface IMainOptions {
1518
enableCors: boolean;
@@ -27,63 +30,66 @@ function verbosePrint(port, enableGraphiql) {
2730
}
2831
}
2932

30-
export class TestConnector {
31-
public get testString() {
32-
return "it works from connector as well!";
33-
}
34-
}
33+
const graphqlMiddleware = [
34+
bodyParser.json(),
35+
bodyParser.text({ type: 'application/graphql' }),
36+
(req, res, next) => {
37+
if (req.is('application/graphql')) {
38+
req.body = { query: req.body };
39+
}
40+
next();
41+
}
42+
];
3543

3644
export function main(options: IMainOptions) {
37-
let app = express();
45+
const app = express();
3846

3947
app.use(helmet());
4048

4149
app.use(morgan(options.env));
4250

43-
if (true === options.enableCors) {
44-
app.use(GRAPHQL_ROUTE, cors());
45-
}
51+
if (true === options.enableCors) app.use(GRAPHQL_ROUTE, cors());
52+
53+
if (options.env === 'production') app.use(compression);
4654

47-
let testConnector = new TestConnector();
48-
app.use(GRAPHQL_ROUTE, bodyParser.json(), graphqlExpress({
55+
app.use(GRAPHQL_ROUTE, ...graphqlMiddleware, graphqlExpress(request => ({
4956
context: {
50-
testConnector,
51-
persons,
52-
findPerson,
53-
addPerson
57+
request,
58+
// add dbConnection here
5459
},
55-
schema: Schema,
56-
}));
60+
schema: Schema
61+
})));
5762

5863
if (true === options.enableGraphiql) {
5964
app.use(GRAPHIQL_ROUTE, graphiqlExpress({endpointURL: GRAPHQL_ROUTE}));
6065
}
6166

6267
return new Promise((resolve, reject) => {
63-
let server = app.listen(options.port, () => {
68+
const server = app.listen(options.port, () => {
6469
/* istanbul ignore if: no need to test verbose print */
6570
if (options.verbose) {
6671
verbosePrint(options.port, options.enableGraphiql);
6772
}
6873

6974
resolve(server);
70-
}).on("error", (err: Error) => {
75+
}).on('error', (err: Error) => {
7176
reject(err);
7277
});
7378
});
7479
}
7580

7681
/* istanbul ignore if: main scope */
82+
// for testing purposes. during testing its required as a module and hence the code below wont run
7783
if (require.main === module) {
7884
const PORT = process.env.PORT || 3000;
7985

8086
// Either to export GraphiQL (Debug Interface) or not.
81-
const NODE_ENV = process.env.NODE_ENV !== "production" ? "dev" : "production";
87+
const NODE_ENV = process.env.NODE_ENV !== 'production' ? 'dev' : 'production';
8288

83-
const EXPORT_GRAPHIQL = NODE_ENV !== "production";
89+
const EXPORT_GRAPHIQL = NODE_ENV !== 'production';
8490

8591
// Enable cors (cross-origin HTTP request) or not.
86-
const ENABLE_CORS = NODE_ENV !== "production";
92+
const ENABLE_CORS = NODE_ENV !== 'production';
8793

8894
main({
8995
enableCors: ENABLE_CORS,

0 commit comments

Comments
 (0)