1
1
require ( "module-alias" ) . addAlias ( "@" , __dirname ) ;
2
2
import "dotenv/config" ;
3
3
import "reflect-metadata" ;
4
- import * as Sentry from "@sentry/node" ;
4
+ import http from "http" ;
5
+ import { init as SentryInit , Handlers as SentryHandlers } from "@sentry/node" ;
5
6
import { RewriteFrames } from "@sentry/integrations" ;
6
7
import Express from "express" ;
7
8
import cors from "cors" ;
8
- import { ApolloServer } from "apollo-server-express" ;
9
+ import { json } from "body-parser" ;
10
+ import { ApolloServer } from "@apollo/server" ;
11
+ import { expressMiddleware } from "@apollo/server/express4" ;
12
+ import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer" ;
9
13
import { buildSchema } from "type-graphql" ;
10
- import { GraphQLSchema } from "graphql" ;
11
14
import createDatabaseConnection from "@/database/createConnection" ;
12
15
import { RESOLVERS } from "@/gql" ;
13
16
import { apolloServerSentryPlugin } from "@/gql/plugins/sentry" ;
14
17
15
- Sentry . init ( {
16
- environment : process . env . APP_ENV ,
17
- release : 'jira-clone-api' ,
18
- dsn : process . env . SENTRY_DSN ,
19
- integrations : [
20
- new RewriteFrames ( {
21
- root : process . cwd ( ) ,
22
- } ) as any ,
23
- ] ,
24
- } ) ;
18
+ const PORT = process . env . PORT || 5001 ;
19
+
20
+ if ( process . env . NODE_ENV === "production" ) {
21
+ SentryInit ( {
22
+ environment : process . env . APP_ENV ,
23
+ release : "jira-clone-api" ,
24
+ dsn : process . env . SENTRY_DSN ,
25
+ integrations : [
26
+ new RewriteFrames ( {
27
+ root : process . cwd ( ) ,
28
+ } ) ,
29
+ ] ,
30
+ } ) ;
31
+ }
25
32
26
33
const establishDatabaseConnection = async ( ) : Promise < void > => {
27
34
try {
@@ -36,31 +43,40 @@ const initExpressGraphql = async () => {
36
43
resolvers : RESOLVERS ,
37
44
} ) . catch ( ( err ) => console . log ( err ) ) ;
38
45
39
- const apolloServer = new ApolloServer ( {
40
- schema : schema as GraphQLSchema ,
41
- context : ( { req, res } : any ) => ( { req, res } ) ,
42
- playground : true ,
46
+ if ( ! schema ) {
47
+ throw new Error ( "Could not build graphql schema" ) ;
48
+ }
49
+
50
+ const app = Express ( ) ;
51
+ const httpServer = http . createServer ( app ) ;
52
+
53
+ const gqlServer = new ApolloServer ( {
54
+ schema,
55
+ plugins : [
56
+ ApolloServerPluginDrainHttpServer ( { httpServer } ) ,
57
+ apolloServerSentryPlugin ,
58
+ ] ,
43
59
introspection : true ,
44
- plugins : [ apolloServerSentryPlugin as any ] ,
45
60
} ) ;
46
61
47
- const app = Express ( ) ;
62
+ await gqlServer . start ( ) ;
48
63
49
- app . use ( Sentry . Handlers . requestHandler ( ) ) ;
64
+ app . use ( SentryHandlers . requestHandler ( ) ) ;
50
65
51
- app . use ( cors ( ) ) ;
52
- app . use ( Express . urlencoded ( { extended : true } ) ) ;
66
+ app . use (
67
+ "/graphql" ,
68
+ cors ( ) ,
69
+ json ( ) ,
70
+ expressMiddleware ( gqlServer ) ,
71
+ SentryHandlers . errorHandler ( )
72
+ ) ;
53
73
54
74
app . get ( "/" , ( _ , res ) => {
55
75
res . json ( { server : "jira-clone-api" } ) ;
56
76
} ) ;
57
77
58
- apolloServer . applyMiddleware ( { app } ) ;
59
- app . use ( Sentry . Handlers . errorHandler ( ) ) ;
60
- app . listen ( process . env . PORT || 5000 , ( ) => {
61
- console . log (
62
- `server started on http://localhost:5000${ apolloServer . graphqlPath } `
63
- ) ;
78
+ httpServer . listen ( { port : PORT } , ( ) => {
79
+ console . log ( `🚀 Server ready at http://localhost:${ PORT } /graphql` ) ;
64
80
} ) ;
65
81
} ;
66
82
0 commit comments