Skip to content

Commit c4c269c

Browse files
author
Gery Hirschfeld
authored
Merge pull request #40 from w3tecch/feature/graphql
Feature/graphql
2 parents 2207e77 + 0fd8b20 commit c4c269c

36 files changed

+984
-163
lines changed

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ DB_DATABASE="my_database"
3030
DB_SYNCHRONIZE=false
3131
DB_LOGGING=false
3232

33+
#
34+
# GraphQL
35+
#
36+
GRAPHQL_ENABLED=true
37+
GRAPHQL_ROUTE="/graphql"
38+
GRAPHQL_EDITOR=true
39+
3340
#
3441
# Swagger
3542
#

.env.test

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ AUTH_ROUTE="http://localhost:3333/tokeninfo"
2121
#
2222
# DATABASE
2323
#
24-
DB_TYPE="mysql"
25-
DB_HOST="localhost"
26-
DB_PORT=3306
27-
DB_USERNAME="root"
28-
DB_PASSWORD=""
29-
DB_DATABASE="my_database"
30-
DB_SYNCHRONIZE=false
24+
DB_TYPE="sqlite"
25+
DB_DATABASE="./mydb.sql"
3126
DB_LOGGING=false
3227

28+
#
29+
# GraphQL
30+
#
31+
GRAPHQL_ENABLED=true
32+
GRAPHQL_ROUTE="/graphql"
33+
3334
#
3435
# Swagger
3536
#

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ test/**/*.js
3434
test/**/*.js.map
3535
coverage/
3636
!test/preprocessor.js
37+
mydb.sql

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
[Jest](https://facebook.github.io/jest/),
1919
[Swagger](http://swagger.io/),
2020
[validatejs](https://validatejs.org/),
21+
[GraphQL](http://graphql.org/),
22+
[DataLoaders](https://github.com/facebook/dataloader),
2123
by [w3tech](https://github.com/w3tecch)
2224

2325
## Why
@@ -44,6 +46,8 @@ Try it!! We are happy to hear your feedback or any kind of new features.
4446
- **Easy event dispatching** thanks to [event-dispatch](https://github.com/pleerock/event-dispatch).
4547
- **Fast Database Building** with simple migration from [TypeORM](https://github.com/typeorm/typeorm).
4648
- **Easy Data Seeding** with our own factories.
49+
- **GraphQL** provides as a awesome query language for our api [GraphQL](http://graphql.org/).
50+
- **DataLoaders** helps with performance thanks to caching and batching [DataLoaders](https://github.com/facebook/dataloader).
4751

4852
### Comming soon
4953

@@ -128,6 +132,7 @@ All script are defined in the package.json file, but the most important ones are
128132
### Tests
129133

130134
- Run the unit tests using `npm start test` (There is also a vscode task for this called `test`).
135+
- Run the integration tests using `npm start test:integration`.
131136
- Run the e2e tests using `npm start test:e2e` and don't forget to start your application and your [Auth0 Mock Server](https://github.com/hirsch88/auth0-mock-server).
132137

133138
### Running in dev mode
@@ -165,9 +170,11 @@ The swagger and the monitor route can be altered in the `.env` file.
165170
| Route | Description |
166171
| -------------- | ----------- |
167172
| **/api** | Shows us the name, description and the version of the package.json |
173+
| **/graphql** | Route to the graphql editor or your query/mutations requests |
168174
| **/swagger** | This is the Swagger UI with our API documentation |
169175
| **/monitor** | Shows a small monitor page for the server |
170176
| **/api/users** | Example entity endpoint |
177+
| **/api/pets** | Example entity endpoint |
171178

172179
## Project Structure
173180

@@ -187,6 +194,9 @@ The swagger and the monitor route can be altered in the `.env` file.
187194
| **src/api/services/** | Service layer |
188195
| **src/api/subscribers/** | Event subscribers |
189196
| **src/api/validators/** | Custom validators, which can be used in the request classes |
197+
| **src/api/queries/** | GraphQL queries |
198+
| **src/api/mutations/** | GraphQL mutations |
199+
| **src/api/types/** | GraphQL types |
190200
| **src/api/** swagger.json | Swagger documentation |
191201
| **src/auth/** | Authentication checkers and services |
192202
| **src/core/** | The core features like logger and env variables |
@@ -199,9 +209,12 @@ The swagger and the monitor route can be altered in the `.env` file.
199209
| **src/types/** *.d.ts | Custom type definitions and files that aren't on DefinitelyTyped |
200210
| **test** | Tests |
201211
| **test/e2e/** *.test.ts | End-2-End tests (like e2e) |
212+
| **test/integration/** *.test.ts | Integration test with SQLite3 |
202213
| **test/unit/** *.test.ts | Unit tests |
203214
| .env.example | Environment configurations |
215+
| .env.test | Test environment configurations |
204216
| ormconfig.json | TypeORM configuration for the database. Used by seeds and the migration. (generated file) |
217+
| mydb.sql | SQLite database for integration tests. Ignored by git and only available after integration tests |
205218

206219
## Logging
207220

@@ -378,6 +391,9 @@ npm start db.seed
378391
| [Auth0 API Documentation](https://auth0.com/docs/api/management/v2) | Authentification service |
379392
| [Jest](http://facebook.github.io/jest/) | Delightful JavaScript Testing Library for unit and e2e tests |
380393
| [swagger Documentation](http://swagger.io/) | API Tool to describe and document your api. |
394+
| [SQLite Documentation](https://www.sitepoint.com/getting-started-sqlite3-basic-commands/) | Getting Started with SQLite3 – Basic Commands. |
395+
| [GraphQL Documentation](http://graphql.org/graphql-js/) | A query language for your API. |
396+
| [DataLoader Documentation](https://github.com/facebook/dataloader) | DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching. |
381397
382398
## Related Projects
383399

package-scripts.js

Lines changed: 73 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -6,103 +6,83 @@ const { series, crossEnv, concurrent, rimraf, runInNewWindow } = require('nps-ut
66

77
module.exports = {
88
scripts: {
9-
default: {
10-
script: 'nps start'
11-
},
9+
default: 'nps start',
1210
/**
1311
* Starts the builded app from the dist directory
1412
*/
15-
start: {
16-
script: 'node dist/app.js'
17-
},
13+
start: 'node dist/app.js',
1814
/**
1915
* Serves the current app and watches for changes to restart it
2016
*/
21-
serve: {
22-
script: series(
23-
'nps banner.serve',
24-
'nodemon --watch src --watch .env'
25-
)
26-
},
17+
serve: series(
18+
'nps banner.serve',
19+
'nodemon --watch src --watch .env'
20+
),
2721
/**
2822
* Setup's the development environment and the database
2923
*/
30-
setup: {
31-
script: series(
32-
'yarn install',
33-
'nps db.migrate',
34-
'nps db.seed'
35-
)
36-
},
24+
setup: series(
25+
'yarn install',
26+
'nps db.migrate',
27+
'nps db.seed'
28+
),
3729
/**
3830
* Builds the app into the dist directory
3931
*/
40-
build: {
41-
script: series(
42-
'nps banner.build',
43-
'nps lint',
44-
'nps clean.dist',
45-
'nps transpile',
46-
'nps copy'
47-
)
48-
},
32+
build: series(
33+
'nps banner.build',
34+
'nps lint',
35+
'nps clean.dist',
36+
'nps transpile',
37+
'nps copy'
38+
),
4939
/**
5040
* Database scripts
5141
*/
5242
db: {
53-
migrate: {
54-
script: series(
55-
'nps banner.migrate',
56-
'nps db.config',
57-
runFast('./node_modules/typeorm/cli.js migrations:run')
58-
)
59-
},
60-
revert: {
61-
script: series(
62-
'nps banner.revert',
63-
'nps db.config',
64-
runFast('./node_modules/typeorm/cli.js migrations:revert')
65-
)
66-
},
67-
seed: {
68-
script: series(
69-
'nps banner.seed',
70-
'nps db.config',
71-
runFast('./src/lib/seeds/')
72-
)
73-
},
74-
config: {
75-
script: runFast('./src/lib/ormconfig.ts')
76-
},
77-
drop: {
78-
script: runFast('./node_modules/typeorm/cli.js schema:drop')
79-
}
43+
migrate: series(
44+
'nps banner.migrate',
45+
'nps db.config',
46+
runFast('./node_modules/typeorm/cli.js migrations:run')
47+
),
48+
revert: series(
49+
'nps banner.revert',
50+
'nps db.config',
51+
runFast('./node_modules/typeorm/cli.js migrations:revert')
52+
),
53+
seed: series(
54+
'nps banner.seed',
55+
'nps db.config',
56+
runFast('./src/lib/seeds/')
57+
),
58+
config: runFast('./src/lib/ormconfig.ts'),
59+
drop: runFast('./node_modules/typeorm/cli.js schema:drop')
8060
},
8161
/**
8262
* These run various kinds of tests. Default is unit.
8363
*/
8464
test: {
8565
default: 'nps test.unit',
8666
unit: {
87-
default: {
88-
script: series(
89-
'nps banner.test',
90-
'nps test.unit.pretest',
91-
'nps test.unit.run'
92-
)
93-
},
94-
pretest: {
95-
script: 'tslint -c ./tslint.json -t stylish ./test/unit/**/*.ts'
96-
},
97-
run: {
98-
script: 'cross-env NODE_ENV=test jest --testPathPattern=unit'
99-
},
100-
verbose: {
101-
script: 'nps "test --verbose"'
102-
},
103-
coverage: {
104-
script: 'nps "test --coverage"'
105-
}
67+
default: series(
68+
'nps banner.test',
69+
'nps test.unit.pretest',
70+
'nps test.unit.run'
71+
),
72+
pretest: 'tslint -c ./tslint.json -t stylish ./test/unit/**/*.ts',
73+
run: 'cross-env NODE_ENV=test jest --testPathPattern=unit',
74+
verbose: 'nps "test --verbose"',
75+
coverage: 'nps "test --coverage"'
76+
},
77+
integration: {
78+
default: series(
79+
'nps banner.test',
80+
'nps test.integration.pretest',
81+
'nps test.integration.run'
82+
),
83+
pretest: 'tslint -c ./tslint.json -t stylish ./test/integration/**/*.ts',
84+
verbose: 'nps "test.integration --verbose"',
85+
run: 'cross-env NODE_ENV=test jest --testPathPattern=integration -i',
10686
},
10787
e2e: {
10888
default: {
@@ -128,51 +108,37 @@ module.exports = {
128108
/**
129109
* Runs TSLint over your project
130110
*/
131-
lint: {
132-
script: `tslint -c ./tslint.json -p tsconfig.json src/**/*.ts --format stylish`
133-
},
111+
lint: `tslint -c ./tslint.json -p tsconfig.json src/**/*.ts --format stylish`,
134112
/**
135113
* Transpile your app into javascript
136114
*/
137-
transpile: {
138-
script: `tsc`
139-
},
115+
transpile: `tsc`,
140116
/**
141117
* Clean files and folders
142118
*/
143119
clean: {
144-
default: {
145-
script: series(
146-
`nps banner.clean`,
147-
`nps clean.dist`
148-
)
149-
},
150-
dist: {
151-
script: rimraf('./dist')
152-
}
120+
default: series(
121+
`nps banner.clean`,
122+
`nps clean.dist`
123+
),
124+
dist: rimraf('./dist')
153125
},
154126
/**
155127
* Copies static files to the build folder
156128
*/
157129
copy: {
158-
default: {
159-
script: series(
160-
`nps copy.swagger`,
161-
`nps copy.public`
162-
)
163-
},
164-
swagger: {
165-
script: copy(
166-
'./src/api/swagger.json',
167-
'./dist'
168-
)
169-
},
170-
public: {
171-
script: copy(
172-
'./src/public/*',
173-
'./dist'
174-
)
175-
}
130+
default: series(
131+
`nps copy.swagger`,
132+
`nps copy.public`
133+
),
134+
swagger: copy(
135+
'./src/api/swagger.json',
136+
'./dist'
137+
),
138+
public: copy(
139+
'./src/public/*',
140+
'./dist'
141+
)
176142
},
177143
/**
178144
* This creates pretty banner to the terminal

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,17 @@
5959
"compression": "^1.7.1",
6060
"copyfiles": "^1.2.0",
6161
"cors": "^2.8.4",
62+
"dataloader": "^1.3.0",
6263
"dotenv": "^4.0.0",
6364
"event-dispatch": "^0.4.1",
6465
"express": "^4.16.2",
6566
"express-basic-auth": "^1.1.3",
67+
"express-graphql": "^0.6.11",
6668
"express-status-monitor": "^1.0.1",
6769
"faker": "^4.1.0",
6870
"figlet": "^1.2.0",
6971
"glob": "^7.1.2",
72+
"graphql": "^0.11.7",
7073
"helmet": "^3.9.0",
7174
"jsonfile": "^4.0.0",
7275
"lodash": "^4.17.4",
@@ -113,6 +116,7 @@
113116
"mock-express-request": "^0.2.0",
114117
"mock-express-response": "^0.2.1",
115118
"nock": "^9.1.0",
119+
"sqlite3": "^3.1.13",
116120
"ts-jest": "^21.1.4"
117121
}
118122
}

src/api/middlewares/CompressionMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers';
55

66

77
@Middleware({ type: 'before' })
8-
export class SecurityMiddleware implements ExpressMiddlewareInterface {
8+
export class CompressionMiddleware implements ExpressMiddlewareInterface {
99

1010
public use(req: express.Request, res: express.Response, next: express.NextFunction): any {
1111
return compression()(req, res, next);

src/api/models/Pet.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
1+
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
22
import { IsNotEmpty } from 'class-validator';
33
import { User } from './User';
44

@@ -17,7 +17,13 @@ export class Pet {
1717
@Column()
1818
public age: number;
1919

20+
@Column({
21+
nullable: true,
22+
})
23+
public userId: number;
24+
2025
@ManyToOne(type => User, user => user.pets)
26+
@JoinColumn({ name: 'userId' })
2127
public user: User;
2228

2329
public toString(): string {

0 commit comments

Comments
 (0)