Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,7 @@ overrides:
'@typescript-eslint/no-throw-literal': error
'@typescript-eslint/no-type-alias': off # TODO consider
'@typescript-eslint/no-unnecessary-boolean-literal-compare': error
# FIXME: Disabled to enable checking conditions that would be prevented by types anyway
'@typescript-eslint/no-unnecessary-condition': off
'@typescript-eslint/no-unnecessary-condition': off # TODO blocked by https://github.com/typescript-eslint/typescript-eslint/issues/2752
'@typescript-eslint/no-unnecessary-qualifier': error
'@typescript-eslint/no-unnecessary-type-arguments': error
'@typescript-eslint/no-unnecessary-type-assertion': error
Expand Down
31 changes: 16 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ type Middleware = (request: Request, response: Response) => Promise<void>;
* configure behavior, and returns an express middleware.
*/
export function graphqlHTTP(options: Options): Middleware {
if (options == null) {
throw new Error('GraphQL middleware requires options.');
}
devAssert(options != null, 'GraphQL middleware requires options.');

return async function graphqlMiddleware(
request: Request,
Expand Down Expand Up @@ -242,12 +240,10 @@ export function graphqlHTTP(options: Options): Middleware {
formatErrorFn;

// Assert that schema is required.
if (schema == null) {
throw httpError(
500,
'GraphQL middleware options must contain a schema.',
);
}
devAssert(
schema != null,
'GraphQL middleware options must contain a schema.',
);

// GraphQL HTTP only supports GET and POST methods.
if (request.method !== 'GET' && request.method !== 'POST') {
Expand Down Expand Up @@ -436,12 +432,10 @@ export function graphqlHTTP(options: Options): Middleware {
: options,
);

// Assert that optionsData is in fact an Object.
if (optionsResult == null || typeof optionsResult !== 'object') {
throw new Error(
'GraphQL middleware option function must return an options object or a promise which will be resolved to an options object.',
);
}
devAssert(
optionsResult != null && typeof optionsResult === 'object',
'GraphQL middleware option function must return an options object or a promise which will be resolved to an options object.',
);

if (optionsResult.formatError) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -538,3 +532,10 @@ function sendResponse(response: Response, type: string, data: string): void {
response.setHeader('Content-Length', String(chunk.length));
response.end(chunk);
}

function devAssert(condition: unknown, message: string): asserts condition {
const booleanCondition = Boolean(condition);
if (!booleanCondition) {
throw new Error(message);
}
}