@@ -33,31 +33,39 @@ app.use(morgan('dev', debugStream));
33
33
app . use ( morgan ( 'combined' , winstonStream ) ) ;
34
34
35
35
36
+ // Requests to /graphql redirect to /
37
+ app . all ( '/graphql' , ( req , res ) => res . redirect ( '/' ) ) ;
38
+
39
+
36
40
// Add GraphQL API to our express app
37
41
import { db } from './core/database' ;
38
42
import { schema } from './schemas' ;
39
43
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' ;
41
47
42
48
import { AuthorRepository } from './repositories/author.repository' ;
43
49
import { BookRepository } from './repositories/book.repository' ;
44
50
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
+
47
60
48
61
app . use ( '/' , ( req : express . Request , res : express . Response ) => {
49
62
log . debug ( 'Setup GraphQLHTTP' ) ;
50
63
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
-
56
64
// Creates a GraphQLHTTP per request
57
65
GraphQLHTTP ( {
58
66
schema : schema ,
59
67
rootValue : new RootValue ( ) ,
60
- context : context ,
68
+ context : new Context ( req , res , repositoriesContext , dataLoadersContext ) ,
61
69
graphiql : environment . server . graphiql
62
70
} ) ( req , res ) ;
63
71
0 commit comments