@@ -187,9 +187,7 @@ type Middleware = (request: Request, response: Response) => Promise<void>;
187187 * configure behavior, and returns an express middleware.
188188 */
189189export 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.' ) ;
193191
194192 return async function graphqlMiddleware (
195193 request : Request ,
@@ -242,12 +240,10 @@ export function graphqlHTTP(options: Options): Middleware {
242240 formatErrorFn ;
243241
244242 // 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+ ) ;
251247
252248 // GraphQL HTTP only supports GET and POST methods.
253249 if ( request . method !== 'GET' && request . method !== 'POST' ) {
@@ -436,12 +432,10 @@ export function graphqlHTTP(options: Options): Middleware {
436432 : options ,
437433 ) ;
438434
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+ ) ;
445439
446440 if ( optionsResult . formatError ) {
447441 // eslint-disable-next-line no-console
@@ -538,3 +532,10 @@ function sendResponse(response: Response, type: string, data: string): void {
538532 response . setHeader ( 'Content-Length' , String ( chunk . length ) ) ;
539533 response . end ( chunk ) ;
540534}
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