Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit ed00ba7

Browse files
Switch to using 'devAssert' similar to graphql-js (#718)
1 parent 219e78c commit ed00ba7

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

.eslintrc.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,7 @@ overrides:
490490
'@typescript-eslint/no-throw-literal': error
491491
'@typescript-eslint/no-type-alias': off # TODO consider
492492
'@typescript-eslint/no-unnecessary-boolean-literal-compare': error
493-
# FIXME: Disabled to enable checking conditions that would be prevented by types anyway
494-
'@typescript-eslint/no-unnecessary-condition': off
493+
'@typescript-eslint/no-unnecessary-condition': off # TODO blocked by https://github.com/typescript-eslint/typescript-eslint/issues/2752
495494
'@typescript-eslint/no-unnecessary-qualifier': error
496495
'@typescript-eslint/no-unnecessary-type-arguments': error
497496
'@typescript-eslint/no-unnecessary-type-assertion': error

src/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ type Middleware = (request: Request, response: Response) => Promise<void>;
187187
* configure behavior, and returns an express middleware.
188188
*/
189189
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.');
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

Comments
 (0)