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

Commit 6d76fad

Browse files
author
Gery Hirschfeld
committed
fix(): Fix all issues, so we can test, build and run the server
1 parent bc50679 commit 6d76fad

28 files changed

+246
-121
lines changed

build/tasks/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ gulp.task('test:pretty', (callback) => {
2929
});
3030

3131
gulp.task('test:run', () =>
32-
gulp.src(['test/lib/setup.ts', path.join(path.join(paths.test, '**/*.spec.js'))], { read: true })
32+
gulp.src([path.join(path.join(paths.test, '**/*.spec.js'))], { read: true })
3333
.pipe($.jasmine())
3434
);
3535

3636
gulp.task('test:pretty:run', () =>
37-
gulp.src(['test/lib/setup.ts', path.join(path.join(paths.test, '**/*.spec.js'))], { read: true })
37+
gulp.src([path.join(path.join(paths.test, '**/*.spec.js'))], { read: true })
3838
.pipe($.jasmine({
3939
reporter: new SpecReporter()
4040
}))

knexfile.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as _ from 'lodash';
22

3-
const config = require('./config');
3+
const config = require('./src/core/config');
44

55
_.forOwn(config, (value, key) => config[key] = value.database);
66

src/context.ts

-56
This file was deleted.

src/context/context.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as Express from 'express';
2+
3+
import { RepositoriesContext } from './repositories-context';
4+
import { DataLoadersContext } from './dataloaders-context';
5+
6+
7+
export class Context {
8+
9+
constructor(
10+
private request: Express.Request,
11+
private repsonse: Express.Response,
12+
private repositories: RepositoriesContext,
13+
private dataLoaders: DataLoadersContext
14+
) { }
15+
16+
public get Response() {
17+
return this.repsonse;
18+
}
19+
20+
public get Request() {
21+
return this.request;
22+
}
23+
24+
public get Repositories() {
25+
return this.repositories;
26+
}
27+
28+
public get DataLoaders() {
29+
return this.dataLoaders;
30+
}
31+
32+
public getLanguage() {
33+
return this.request.acceptsLanguages();
34+
}
35+
36+
public hasUserRoles(roles: string[]): boolean {
37+
// TODO: Here you should check if the user as the needed roles for the requested query
38+
return true;
39+
}
40+
41+
}

src/context/dataloaders-context.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as DataLoader from 'dataloader';
2+
3+
import { AuthorRepository } from '../repositories/author.repository';
4+
import { BookRepository } from '../repositories/book.repository';
5+
6+
import { Logger } from '../core/logger';
7+
const log = Logger('app:context:DataLoadersContext');
8+
9+
10+
export class DataLoadersContext {
11+
12+
private authorDataLaoder: DataLoader<number, any>;
13+
private bookDataLaoder: DataLoader<number, any>;
14+
15+
public get AuthorDataLoader() {
16+
return this.authorDataLaoder;
17+
}
18+
19+
public get BookDataLoader() {
20+
return this.bookDataLaoder;
21+
}
22+
23+
public setAuthorDataLoader(authorRepository: AuthorRepository): DataLoadersContext {
24+
this.authorDataLaoder = new DataLoader((ids: number[]) => authorRepository.findAuthorsByIds(ids));
25+
log.debug('setAuthorDataLoader');
26+
return this;
27+
}
28+
29+
public setBookDataLoader(bookRepository: BookRepository): DataLoadersContext {
30+
this.bookDataLaoder = new DataLoader((ids: number[]) => bookRepository.findBooksByIds(ids));
31+
log.debug('setBookDataLoader');
32+
return this;
33+
}
34+
35+
}

src/context/repositories-context.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { AuthorRepository } from '../repositories/author.repository';
2+
import { BookRepository } from '../repositories/book.repository';
3+
4+
import { Logger } from '../core/logger';
5+
const log = Logger('app:context:RepositoriesContext');
6+
7+
8+
export class RepositoriesContext {
9+
10+
private authorRepository: AuthorRepository;
11+
private bookRepository: BookRepository;
12+
13+
public get AuthorRepository() {
14+
return this.authorRepository;
15+
}
16+
17+
public get BookRepository() {
18+
return this.bookRepository;
19+
}
20+
21+
public setAuthorRepository(authorRepository: AuthorRepository): RepositoriesContext {
22+
this.authorRepository = authorRepository;
23+
log.debug('setAuthorRepository');
24+
return this;
25+
}
26+
27+
public setBookRepository(bookRepository: BookRepository): RepositoriesContext {
28+
this.bookRepository = bookRepository;
29+
log.debug('setBookRepository');
30+
return this;
31+
}
32+
33+
}

config.ts src/core/config.ts

File renamed without changes.

src/core/environment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const config = require('../../config');
1+
const config = require('./config');
22

33
export const name = (): string => process.env.NODE_ENV || 'development';
44

src/database/seeds/20170220183349_authors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { makeAuthor } from '../factories/author.factory';
88
exports.seed = (db: Knex) => {
99

1010
// generate fake authors
11-
let entries = _.times(1000, () => makeAuthor().buildRaw());
11+
let entries = _.times(1000, () => makeAuthor().toDatabaseObject);
1212

1313
// Inserts seed entries
1414
return db(TABLE.AUTHOR).insert(entries);

src/database/seeds/20170221195948_book.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exports.seed = async (db: Knex) => {
1313

1414
let entries = [];
1515
_.forEach(authorIds, (authorId: number) => {
16-
entries = _.concat(entries, _.times(10, () => makeBook(authorId).buildRaw()));
16+
entries = _.concat(entries, _.times(10, () => makeBook(authorId).toDatabaseObject()));
1717
});
1818

1919
// Inserts seed entries

src/index.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,39 @@ app.use(morgan('dev', debugStream));
3333
app.use(morgan('combined', winstonStream));
3434

3535

36+
// Requests to /graphql redirect to /
37+
app.all('/graphql', (req, res) => res.redirect('/'));
38+
39+
3640
// Add GraphQL API to our express app
3741
import { db } from './core/database';
3842
import { schema } from './schemas';
3943
import { RootValue } from './root-value';
40-
import { Context } from './context';
44+
import { Context } from './context/context';
45+
import { RepositoriesContext } from './context/repositories-context';
46+
import { DataLoadersContext } from './context/dataloaders-context';
4147

4248
import { AuthorRepository } from './repositories/author.repository';
4349
import { BookRepository } from './repositories/book.repository';
4450

45-
// Requests to /graphql redirect to /
46-
app.all('/graphql', (req, res) => res.redirect('/'));
51+
52+
const repositoriesContext = new RepositoriesContext()
53+
.setAuthorRepository(new AuthorRepository(db))
54+
.setBookRepository(new BookRepository(db));
55+
56+
const dataLoadersContext = new DataLoadersContext()
57+
.setAuthorDataLoader(repositoriesContext.AuthorRepository)
58+
.setBookDataLoader(repositoriesContext.BookRepository);
59+
4760

4861
app.use('/', (req: express.Request, res: express.Response) => {
4962
log.debug('Setup GraphQLHTTP');
5063

51-
// Set all needed repositories and config a data loader within the setter
52-
const context = new Context(req, res)
53-
.setAuthorRepository(new AuthorRepository(db))
54-
.setBookRepository(new BookRepository(db));
55-
5664
// Creates a GraphQLHTTP per request
5765
GraphQLHTTP({
5866
schema: schema,
5967
rootValue: new RootValue(),
60-
context: context,
68+
context: new Context(req, res, repositoriesContext, dataLoadersContext),
6169
graphiql: environment.server.graphiql
6270
})(req, res);
6371

src/schemas/abstract.field.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Context } from '../context';
1+
import { Context } from '../context/context';
22

33

44
export interface IGraphQLField {
@@ -84,7 +84,7 @@ export class AbstractField {
8484
public resolve = async (source, args, context: Context): Promise<any> => {
8585
//first check roles
8686
if (!context.hasUserRoles(this.allow)) {
87-
return context.repsonse.send(401);
87+
return context.Response.send(401);
8888
}
8989

9090
//go throw before

src/schemas/abstract.mutation.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { AbstractQuery, IGraphQLQuery } from './abstract.query';
2+
3+
export interface IGraphQLMutation extends IGraphQLQuery {
4+
}
5+
6+
export class AbstractMutation extends AbstractQuery {
7+
}

src/schemas/abstract.query.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Context } from '../context';
1+
import { Context } from '../context/context';
22

33

44
export interface IGraphQLQuery {
@@ -84,7 +84,7 @@ export class AbstractQuery {
8484
public resolve = async (root, args, context: Context): Promise<any> => {
8585
//first check roles
8686
if (!context.hasUserRoles(this.allow)) {
87-
return context.repsonse.send(401);
87+
return context.Response.send(401);
8888
}
8989

9090
//go throw before

src/schemas/author/author.field.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { GraphQLFieldDefinition } from 'graphql';
22

3-
import { Context } from '../../context';
3+
import { Context } from '../../context/context';
44
import { AbstractField, IGraphQLField } from '../abstract.field';
55
import { AuthorType } from '../author/author.type';
66
import { Book } from '../../models/book.model';
@@ -21,10 +21,10 @@ export class AuthorField extends AbstractField implements GraphQLFieldDefinition
2121
log.debug('Resolve auhtor %s of the book ' + source.id, source.authorId);
2222

2323
// Repo way
24-
// return context.repos.author.findAuthorById(source.authorId);
24+
// return context.getRepositories().getAuthorRepository().findAuthorById(source.authorId);
2525

2626
// DataLoader
27-
return context.loaders.author.load(source.authorId);
27+
return context.DataLoaders.AuthorDataLoader.load(source.authorId);
2828

2929
// Benchmark with 1000 Authors and per Author 10 Books
3030
// With Loaders => ca. 2s

src/schemas/author/author.mutation.ts

-26
This file was deleted.

0 commit comments

Comments
 (0)