@@ -187,9 +187,7 @@ type Middleware = (request: Request, response: Response) => Promise<void>;
187
187
* configure behavior, and returns an express middleware.
188
188
*/
189
189
export function graphqlHTTP ( options : Options ) : Middleware {
190
- if ( options == null ) {
191
- throw new Error ( 'GraphQL middleware requires options.' ) ;
192
- }
190
+ devAssert ( options != null , 'GraphQL middleware requires options.' ) ;
193
191
194
192
return async function graphqlMiddleware (
195
193
request : Request ,
@@ -242,12 +240,10 @@ export function graphqlHTTP(options: Options): Middleware {
242
240
formatErrorFn ;
243
241
244
242
// Assert that schema is required.
245
- if ( schema == null ) {
246
- throw httpError (
247
- 500 ,
248
- 'GraphQL middleware options must contain a schema.' ,
249
- ) ;
250
- }
243
+ devAssert (
244
+ schema != null ,
245
+ 'GraphQL middleware options must contain a schema.' ,
246
+ ) ;
251
247
252
248
// GraphQL HTTP only supports GET and POST methods.
253
249
if ( request . method !== 'GET' && request . method !== 'POST' ) {
@@ -436,12 +432,10 @@ export function graphqlHTTP(options: Options): Middleware {
436
432
: options ,
437
433
) ;
438
434
439
- // Assert that optionsData is in fact an Object.
440
- if ( optionsResult == null || typeof optionsResult !== 'object' ) {
441
- throw new Error (
442
- 'GraphQL middleware option function must return an options object or a promise which will be resolved to an options object.' ,
443
- ) ;
444
- }
435
+ devAssert (
436
+ optionsResult != null && typeof optionsResult === 'object' ,
437
+ 'GraphQL middleware option function must return an options object or a promise which will be resolved to an options object.' ,
438
+ ) ;
445
439
446
440
if ( optionsResult . formatError ) {
447
441
// eslint-disable-next-line no-console
@@ -538,3 +532,10 @@ function sendResponse(response: Response, type: string, data: string): void {
538
532
response . setHeader ( 'Content-Length' , String ( chunk . length ) ) ;
539
533
response . end ( chunk ) ;
540
534
}
535
+
536
+ function devAssert ( condition : unknown , message : string ) : asserts condition {
537
+ const booleanCondition = Boolean ( condition ) ;
538
+ if ( ! booleanCondition ) {
539
+ throw new Error ( message ) ;
540
+ }
541
+ }
0 commit comments