Commit 3048c0fc authored by Max Hartmann's avatar Max Hartmann

tons of fixes

parent b2f55744
Pipeline #52463 passed with stages
in 1 minute and 53 seconds
# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
!.elasticbeanstalk/*.global.yml
.idea
.node_modules
.src
# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
......@@ -7,17 +7,17 @@ before_script:
stages:
- test
# lint and test are two different jobs in the same stage.
# This allows us to run these two in parallel and making build faster
# Job 1:
- build
lint:
stage: test
script:
- npm run lint
# Job 2:
test:
stage: test
script:
- npm run test
build:
stage: build
script:
- npm run build
FROM node:alpine
# Bundle app source
COPY package.json .
COPY dist /dist
EXPOSE 3000
CMD [ "npm", "start" ]
{
"name": "graphql-boilerplate",
"scripts": {
"build": "webpack --production",
"start": "node dist/js/bundle.js",
"build": "webpack --mode production",
"dev": "tsc --watch | nodemon --inspect ./dist/app.js --watch dist",
"test": "mocha -r ts-node/register src/**/*.spec.ts",
"lint": "tslint **/*.ts"
......@@ -10,15 +11,13 @@
"@tsed/common": "^5.1.1",
"@tsed/core": "^5.1.1",
"@tsed/di": "^5.1.1",
"@types/express-graphql": "^0.6.2",
"@types/express": "^4.16.1",
"@types/express-graphql": "^0.6.2",
"@types/graphql": "^14.0.5",
"@types/pg": "^7.4.11",
"body-parser": "^1.18.3",
"express": "^4.16.4",
"express-graphql": "^0.7.1",
"graphql": "^14.1.1",
"pg": "^7.8.0"
"graphql": "^14.1.1"
},
"devDependencies": {
"@types/chai": "^4.1.7",
......
import {expect} from 'chai';
import {QueryResult} from 'pg';
import request from 'supertest';
import {GraphQlController, PersonController} from './controllers';
import {Person} from './models';
......@@ -7,37 +6,36 @@ import {PersonService} from './services';
import express from 'express';
const personServiceMock = {
findPersons(): Promise<QueryResult> {
return new Promise((resolve) => {
resolve({rows: [{id: 1, firstname: 'Max', surname: 'Hartmann'} as Person]} as QueryResult);
});
findPersons(): Person[] {
return [{id: 1, firstname: 'Max', surname: 'Hartmann'} as Person] as Person[];
},
findPerson(id: number) {
return {id: 1, firstname: 'Max', surname: 'Hartmann'} as Person;
}
} as PersonService;
describe('Controller Specs', () => {
const app = express();
describe('Graphql Controller', () => {
// noinspection JSUnusedLocalSymbols
const should = require('chai').should();
it('should create Middleware', () => {
const graphQlController = new GraphQlController(personServiceMock);
const middleware = graphQlController.createGraphqlMiddleware();
expect(middleware).not.to.be.null;
// noinspection JSUnusedLocalSymbols
const should = require('chai').should();
describe('Person Controller', () => {
it('should create PersonController', () => {
const personController = new PersonController(personServiceMock);
expect(personController).not.to.be.null;
});
it('should load Persons', (done) => {
const graphQlController = new GraphQlController(personServiceMock);
const middleware = graphQlController.createGraphqlMiddleware();
app.use('/graphql', middleware);
const personController = new PersonController(personServiceMock);
app.use('/person', personController.routes());
request(app)
.get('/graphql')
.send({query: '{ persons { surname firstname } }'})
.get('/person')
.expect(200)
.end((err, res) => {
res.body.data.persons[0].should.have.property('firstname');
res.body.data.persons[0].should.have.property('surname');
res.body.data.persons[0].firstname.should.equal('Max');
res.body.data.persons[0].surname.should.equal('Hartmann');
res.body[0].should.have.property('firstname');
res.body[0].should.have.property('surname');
res.body[0].firstname.should.equal('Max');
res.body[0].surname.should.equal('Hartmann');
done();
});
});
......
......@@ -19,16 +19,13 @@ export class PersonController implements Controller {
return routes;
}
private async persons(req: Request, res: Response) {
return this.service.findPersons().then((result) => res.send(result.rows));
private persons(req: Request, res: Response) {
res.send(this.service.findPersons());
}
private async savePerson(req: Request, res: Response) {
private savePerson(req: Request, res: Response) {
const person = req.body.person as Person;
this.service.save(person).then((savedId) => {
person.id = savedId;
res.send(person);
});
this.service.save(person);
}
}
......@@ -42,9 +39,8 @@ export class GraphQlController {
const schema = buildSchema(Schema);
// noinspection JSUnusedGlobalSymbols
const root = {
persons: () => this.service.findPersons()
.then((result) => result.rows),
person: (param: {id: number}) => this.service.findPerson(param.id).then((result) => result.rows[0]),
persons: () => this.service.findPersons(),
person: (param: {id: number}) => this.service.findPerson(param.id),
createPerson: (person: Person) => this.service.save(person).then((savedId) => {
person.id = savedId;
return person;
......
......@@ -16,7 +16,8 @@ export default class Server {
this.app.use(bodyParser.json());
}
public start() {
this.app.listen(8080, () => {
const port = process.env.PORT || 3000;
this.app.listen(port, () => {
console.log(`server started at http://localhost:8080`);
});
}
......
import {Pool, QueryResult} from 'pg';
import {Person} from './models';
export class PersonService {
private pool: Pool;
/*private pool: Pool;
constructor() {
this.pool = new Pool({
database: 'postgres',
......@@ -11,10 +10,10 @@ export class PersonService {
user: 'postgres',
password: 'keule',
});
}
}*/
public async save(person: Person): Promise<number> {
const client = await this.pool.connect();
public save(person: Person): Promise<number> {
/*const client = await this.pool.connect();
try {
await client.query('BEGIN');
const {rows} = await client
......@@ -26,14 +25,17 @@ export class PersonService {
await client.query('ROLLBACK');
} finally {
client.release();
}
}*/
return new Promise((resolve) => {
resolve(1);
});
}
public async findPersons(): Promise<QueryResult> {
return await this.pool.query('SELECT * FROM person order BY id ASC');
public findPersons(): Person[] {
return [] as Person[];
}
public async findPerson(id: number) {
return await this.pool.query('SELECT * FROM person WHERE id = $1', [id]);
public findPerson(id: number) {
return {} as Person;
}
}
......@@ -21,6 +21,11 @@ module.exports = {
test: /\.ts$/,
loader: 'awesome-typescript-loader',
exclude: path.resolve(__dirname, "node_modules")
},
{
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto'
}
]
},
......@@ -30,4 +35,4 @@ module.exports = {
'.js'
]
}
};
\ No newline at end of file
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment