Skip to content

Commit

Permalink
Merge pull request #2108 from apollographql/abernix/caching-tweak-prep
Browse files Browse the repository at this point in the history
chores: Request pipeline readability/DRYing.
  • Loading branch information
abernix authored Dec 18, 2018
2 parents 6b47f6f + 5a50e1b commit 8ad2bdd
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from 'apollo-cache-control';
import { TracingExtension } from 'apollo-tracing';
import {
ApolloError,
fromGraphQLError,
SyntaxError,
ValidationError,
Expand Down Expand Up @@ -135,15 +136,9 @@ export async function processGraphQLRequest<TContext>(

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
Expand Down Expand Up @@ -179,13 +174,7 @@ export async function processGraphQLRequest<TContext>(
parsingDidEnd();
} catch (syntaxError) {
parsingDidEnd(syntaxError);
return sendResponse({
errors: [
fromGraphQLError(syntaxError, {
errorClass: SyntaxError,
}),
],
});
return sendErrorResponse(syntaxError, SyntaxError);
}

requestContext.document = document;
Expand All @@ -197,19 +186,13 @@ export async function processGraphQLRequest<TContext>(

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
Expand Down Expand Up @@ -248,9 +231,7 @@ export async function processGraphQLRequest<TContext>(
executionDidEnd();
} catch (executionError) {
executionDidEnd(executionError);
return sendResponse({
errors: [fromGraphQLError(executionError)],
});
return sendErrorResponse(executionError);
}

const formattedExtensions = extensionStack.format();
Expand Down Expand Up @@ -346,6 +327,27 @@ export async function processGraphQLRequest<TContext>(
return requestContext.response!;
}

function sendErrorResponse(
errorOrErrors: ReadonlyArray<GraphQLError> | 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
> {
Expand Down

0 comments on commit 8ad2bdd

Please sign in to comment.