From 9e8984c2ee977f75e4d4bdebb295fe5826e5518e Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 12 Mar 2018 20:25:12 +0100 Subject: [PATCH] Inline all possible functions that are used only once (#821) * Inline isFunction type-guard * Don't redefine functions on doRunQuery * Update CHANGELOG --- CHANGELOG.md | 1 + .../apollo-server-core/src/graphqlOptions.ts | 8 +-- .../apollo-server-core/src/runHttpQuery.ts | 6 +- packages/apollo-server-core/src/runQuery.ts | 61 ++++++++++--------- 4 files changed, 35 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf178da7cc4..795b22a007b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### vNEXT * Fix apollo-server-core runQuery breaks async_hooks tracking [PR #733](https://github.com/apollographql/apollo-server/pull/733) +* Optimize one-time functions [PR# 821](https://github.com/apollographql/apollo-server/pull/821) ### v1.3.0 diff --git a/packages/apollo-server-core/src/graphqlOptions.ts b/packages/apollo-server-core/src/graphqlOptions.ts index 242e6f532ca..b453e18bb85 100644 --- a/packages/apollo-server-core/src/graphqlOptions.ts +++ b/packages/apollo-server-core/src/graphqlOptions.ts @@ -42,7 +42,7 @@ export async function resolveGraphqlOptions( options: GraphQLServerOptions | Function, ...args ): Promise { - if (isOptionsFunction(options)) { + if (typeof options === 'function') { try { return await options(...args); } catch (e) { @@ -52,9 +52,3 @@ export async function resolveGraphqlOptions( return options; } } - -export function isOptionsFunction( - arg: GraphQLServerOptions | Function, -): arg is Function { - return typeof arg === 'function'; -} diff --git a/packages/apollo-server-core/src/runHttpQuery.ts b/packages/apollo-server-core/src/runHttpQuery.ts index 780ca7f97c3..73528afff90 100644 --- a/packages/apollo-server-core/src/runHttpQuery.ts +++ b/packages/apollo-server-core/src/runHttpQuery.ts @@ -41,10 +41,6 @@ function isQueryOperation(query: DocumentNode, operationName: string) { return operationAST.operation === 'query'; } -function isFunction(arg: any): arg is Function { - return typeof arg === 'function'; -} - export async function runHttpQuery( handlerArguments: Array, request: HttpQueryRequest, @@ -135,7 +131,7 @@ export async function runHttpQuery( } let context = optionsObject.context || {}; - if (isFunction(context)) { + if (typeof context === 'function') { context = context(); } else if (isBatch) { context = Object.assign( diff --git a/packages/apollo-server-core/src/runQuery.ts b/packages/apollo-server-core/src/runQuery.ts index 5a3e4736999..62c18b1ef96 100644 --- a/packages/apollo-server-core/src/runQuery.ts +++ b/packages/apollo-server-core/src/runQuery.ts @@ -71,11 +71,31 @@ export interface QueryOptions { cacheControl?: boolean; } -function runQuery(options: QueryOptions): Promise { +export function runQuery(options: QueryOptions): Promise { // Fiber-aware Promises run their .then callbacks in Fibers. return Promise.resolve().then(() => doRunQuery(options)); } +function printStackTrace(error: Error) { + console.error(error.stack); +} + +function format(errors: Array, formatter?: Function): Array { + return errors.map(error => { + if (formatter !== undefined) { + try { + return formatter(error); + } catch (err) { + console.error('Error in formatError function:', err); + const newError = new Error('Internal server error'); + return formatError(newError); + } + } else { + return formatError(error); + } + }) as Array; +} + function doRunQuery(options: QueryOptions): Promise { let documentAST: DocumentNode; @@ -86,8 +106,7 @@ function doRunQuery(options: QueryOptions): Promise { }; const debugDefault = process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test'; - const debug = - typeof options.debug !== 'undefined' ? options.debug : debugDefault; + const debug = options.debug !== undefined ? options.debug : debugDefault; logFunction({ action: LogAction.request, step: LogStep.start }); @@ -109,26 +128,6 @@ function doRunQuery(options: QueryOptions): Promise { extensionStack.requestDidStart(); } - function format(errors: Array): Array { - return errors.map(error => { - if (options.formatError) { - try { - return options.formatError(error); - } catch (err) { - console.error('Error in formatError function:', err); - const newError = new Error('Internal server error'); - return formatError(newError); - } - } else { - return formatError(error); - } - }) as Array; - } - - function printStackTrace(error: Error) { - console.error(error.stack); - } - const qry = typeof options.query === 'string' ? options.query : print(options.query); logFunction({ @@ -159,7 +158,9 @@ function doRunQuery(options: QueryOptions): Promise { logFunction({ action: LogAction.parse, step: LogStep.end }); } catch (syntaxError) { logFunction({ action: LogAction.parse, step: LogStep.end }); - return Promise.resolve({ errors: format([syntaxError]) }); + return Promise.resolve({ + errors: format([syntaxError], options.formatError), + }); } } else { documentAST = options.query as DocumentNode; @@ -173,7 +174,9 @@ function doRunQuery(options: QueryOptions): Promise { const validationErrors = validate(options.schema, documentAST, rules); logFunction({ action: LogAction.validation, step: LogStep.end }); if (validationErrors.length) { - return Promise.resolve({ errors: format(validationErrors) }); + return Promise.resolve({ + errors: format(validationErrors, options.formatError), + }); } if (extensionStack) { @@ -201,7 +204,7 @@ function doRunQuery(options: QueryOptions): Promise { }; if (result.errors) { - response.errors = format(result.errors); + response.errors = format(result.errors, options.formatError); if (debug) { result.errors.map(printStackTrace); } @@ -222,8 +225,8 @@ function doRunQuery(options: QueryOptions): Promise { } catch (executionError) { logFunction({ action: LogAction.execute, step: LogStep.end }); logFunction({ action: LogAction.request, step: LogStep.end }); - return Promise.resolve({ errors: format([executionError]) }); + return Promise.resolve({ + errors: format([executionError], options.formatError), + }); } } - -export { runQuery };