@@ -3,7 +3,11 @@ import { apolloUploadExpress, GraphQLUpload } from 'apollo-upload-server'
3
3
import * as bodyParser from 'body-parser-graphql'
4
4
import * as cors from 'cors'
5
5
import * as express from 'express'
6
- import { PathParams , RequestHandler , RequestHandlerParams } from 'express-serve-static-core'
6
+ import {
7
+ PathParams ,
8
+ RequestHandler ,
9
+ RequestHandlerParams ,
10
+ } from 'express-serve-static-core'
7
11
import * as fs from 'fs'
8
12
import { execute , GraphQLSchema , subscribe } from 'graphql'
9
13
import { importSchema } from 'graphql-import'
@@ -33,7 +37,10 @@ export class GraphQLServer {
33
37
context : any
34
38
35
39
private middlewares : {
36
- [ key : string ] : { path ?: PathParams ; handlers : RequestHandler [ ] | RequestHandlerParams [ ] } [ ] ,
40
+ [ key : string ] : {
41
+ path ?: PathParams
42
+ handlers : RequestHandler [ ] | RequestHandlerParams [ ]
43
+ } [ ]
37
44
} = { use : [ ] , get : [ ] , post : [ ] }
38
45
39
46
constructor ( props : Props ) {
@@ -49,7 +56,9 @@ export class GraphQLServer {
49
56
50
57
// read from .graphql file if path provided
51
58
if ( typeDefs . endsWith ( 'graphql' ) ) {
52
- const schemaPath = path . isAbsolute ( typeDefs ) ? path . resolve ( typeDefs ) : path . resolve ( typeDefs )
59
+ const schemaPath = path . isAbsolute ( typeDefs )
60
+ ? path . resolve ( typeDefs )
61
+ : path . resolve ( typeDefs )
53
62
54
63
if ( ! fs . existsSync ( schemaPath ) ) {
55
64
throw new Error ( `No schema found for path: ${ schemaPath } ` )
@@ -58,7 +67,9 @@ export class GraphQLServer {
58
67
typeDefs = importSchema ( schemaPath )
59
68
}
60
69
61
- const uploadMixin = typeDefs . includes ( 'scalar Upload' ) ? { Upload : GraphQLUpload } : { }
70
+ const uploadMixin = typeDefs . includes ( 'scalar Upload' )
71
+ ? { Upload : GraphQLUpload }
72
+ : { }
62
73
this . executableSchema = makeExecutableSchema ( {
63
74
typeDefs,
64
75
resolvers : {
@@ -89,11 +100,24 @@ export class GraphQLServer {
89
100
return this
90
101
}
91
102
92
- start ( options : Options , callback ?: ( ( options : Options ) => void ) ) : Promise < void >
103
+ start (
104
+ options : Options ,
105
+ callback ?: ( ( options : Options ) => void ) ,
106
+ ) : Promise < void >
93
107
start ( callback ?: ( ( options : Options ) => void ) ) : Promise < void >
94
- start ( optionsOrCallback ?: Options | ( ( options : Options ) => void ) , callback ?: ( ( options : Options ) => void ) ) : Promise < void > {
95
- const options = ( optionsOrCallback && typeof optionsOrCallback === 'function' ) ? { } : optionsOrCallback
96
- const callbackFunc = callback ? callback : ( optionsOrCallback && typeof optionsOrCallback === 'function' ) ? optionsOrCallback : ( ) => null
108
+ start (
109
+ optionsOrCallback ?: Options | ( ( options : Options ) => void ) ,
110
+ callback ?: ( ( options : Options ) => void ) ,
111
+ ) : Promise < void > {
112
+ const options =
113
+ optionsOrCallback && typeof optionsOrCallback === 'function'
114
+ ? { }
115
+ : optionsOrCallback
116
+ const callbackFunc = callback
117
+ ? callback
118
+ : optionsOrCallback && typeof optionsOrCallback === 'function'
119
+ ? optionsOrCallback
120
+ : ( ) => null
97
121
98
122
const app = this . express
99
123
@@ -117,7 +141,11 @@ export class GraphQLServer {
117
141
app . use ( cors ( ) )
118
142
}
119
143
120
- app . post ( this . options . endpoint , bodyParser . graphql ( ) , apolloUploadExpress ( this . options . uploads ) )
144
+ app . post (
145
+ this . options . endpoint ,
146
+ bodyParser . graphql ( ) ,
147
+ apolloUploadExpress ( this . options . uploads ) ,
148
+ )
121
149
122
150
if ( this . options . uploads ) {
123
151
app . post ( this . options . endpoint , apolloUploadExpress ( this . options . uploads ) )
@@ -154,7 +182,10 @@ export class GraphQLServer {
154
182
graphqlExpress ( async request => {
155
183
let context
156
184
try {
157
- context = typeof this . context === 'function' ? await this . context ( { request } ) : this . context
185
+ context =
186
+ typeof this . context === 'function'
187
+ ? await this . context ( { request } )
188
+ : this . context
158
189
} catch ( e ) {
159
190
console . error ( e )
160
191
throw e
@@ -178,12 +209,9 @@ export class GraphQLServer {
178
209
)
179
210
180
211
if ( this . options . playground ) {
181
- const isDev = process . env . NODE_ENV === 'dev' || process . env . NODE_ENV === 'development'
182
- const playgroundOptions = isDev
183
- ? { useGraphQLConfig : true , env : process . env }
184
- : this . options . subscriptions
185
- ? { endpoint : this . options . endpoint , subscriptionsEndpoint : this . options . subscriptions }
186
- : { endpoint : this . options . endpoint }
212
+ const playgroundOptions = this . options . subscriptions
213
+ ? { endpoint : this . options . endpoint , subscriptionsEndpoint : this . options . subscriptions }
214
+ : { endpoint : this . options . endpoint }
187
215
188
216
app . get ( this . options . playground , expressPlayground ( playgroundOptions ) )
189
217
}
@@ -215,7 +243,9 @@ export class GraphQLServer {
215
243
let context
216
244
try {
217
245
context =
218
- typeof this . context === 'function' ? await this . context ( { connection } ) : this . context
246
+ typeof this . context === 'function'
247
+ ? await this . context ( { connection } )
248
+ : this . context
219
249
} catch ( e ) {
220
250
console . error ( e )
221
251
throw e
0 commit comments