diff --git a/packages/apollo-server-core/src/requestPipeline.ts b/packages/apollo-server-core/src/requestPipeline.ts index 7c3d672eeee..7c4429c3f60 100644 --- a/packages/apollo-server-core/src/requestPipeline.ts +++ b/packages/apollo-server-core/src/requestPipeline.ts @@ -22,6 +22,7 @@ import { } from 'apollo-cache-control'; import { TracingExtension } from 'apollo-tracing'; import { + ApolloError, fromGraphQLError, SyntaxError, ValidationError, @@ -135,15 +136,9 @@ export async function processGraphQLRequest( persistedQueryRegister = true; - // Store the query asynchronously so we don't block. - (async () => { - return ( - config.persistedQueries && - config.persistedQueries.cache.set(`apq:${queryHash}`, query) - ); - })().catch(error => { - console.warn(error); - }); + Promise.resolve( + config.persistedQueries.cache.set(`apq:${queryHash}`, query), + ).catch(console.warn); } } else if (query) { // FIXME: We'll compute the APQ query hash to use as our cache key for @@ -179,13 +174,7 @@ export async function processGraphQLRequest( parsingDidEnd(); } catch (syntaxError) { parsingDidEnd(syntaxError); - return sendResponse({ - errors: [ - fromGraphQLError(syntaxError, { - errorClass: SyntaxError, - }), - ], - }); + return sendErrorResponse(syntaxError, SyntaxError); } requestContext.document = document; @@ -197,19 +186,13 @@ export async function processGraphQLRequest( const validationErrors = validate(document); - if (validationErrors.length > 0) { + if (validationErrors.length === 0) { + validationDidEnd(); + } else { validationDidEnd(validationErrors); - return sendResponse({ - errors: validationErrors.map(validationError => - fromGraphQLError(validationError, { - errorClass: ValidationError, - }), - ), - }); + return sendErrorResponse(validationErrors, ValidationError); } - validationDidEnd(); - // FIXME: If we want to guarantee an operation has been set when invoking // `willExecuteOperation` and executionDidStart`, we need to throw an // error here and not leave this to `buildExecutionContext` in @@ -248,9 +231,7 @@ export async function processGraphQLRequest( executionDidEnd(); } catch (executionError) { executionDidEnd(executionError); - return sendResponse({ - errors: [fromGraphQLError(executionError)], - }); + return sendErrorResponse(executionError); } const formattedExtensions = extensionStack.format(); @@ -346,6 +327,27 @@ export async function processGraphQLRequest( return requestContext.response!; } + function sendErrorResponse( + errorOrErrors: ReadonlyArray | GraphQLError, + errorClass?: typeof ApolloError, + ) { + // If a single error is passed, it should still be encapsulated in an array. + const errors = Array.isArray(errorOrErrors) + ? errorOrErrors + : [errorOrErrors]; + + return sendResponse({ + errors: errors.map(err => + fromGraphQLError( + err, + errorClass && { + errorClass, + }, + ), + ), + }); + } + function initializeRequestListenerDispatcher(): Dispatcher< GraphQLRequestListener > {