Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the context parameter to the didEncounterErrors handler #3157

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export async function processGraphQLRequest<TContext>(
parsingDidEnd();
} catch (syntaxError) {
parsingDidEnd(syntaxError);
await didEncounterErrors(syntaxError, requestContext.context);
return await sendErrorResponse(syntaxError, SyntaxError);
}

Expand All @@ -253,6 +254,7 @@ export async function processGraphQLRequest<TContext>(
validationDidEnd();
} else {
validationDidEnd(validationErrors);
await didEncounterErrors(validationErrors, requestContext.context);
return await sendErrorResponse(validationErrors, ValidationError);
}

Expand Down Expand Up @@ -312,6 +314,7 @@ export async function processGraphQLRequest<TContext>(
if (err instanceof HttpQueryError) {
throw err;
}
await didEncounterErrors(err, requestContext.context);
return await sendErrorResponse(err);
}

Expand Down Expand Up @@ -348,7 +351,7 @@ export async function processGraphQLRequest<TContext>(
>);

if (result.errors) {
await didEncounterErrors(result.errors);
await didEncounterErrors(result.errors, requestContext.context);
}

response = {
Expand All @@ -359,6 +362,7 @@ export async function processGraphQLRequest<TContext>(
executionDidEnd();
} catch (executionError) {
executionDidEnd(executionError);
await didEncounterErrors(executionError, requestContext.context);
return await sendErrorResponse(executionError);
}
}
Expand Down Expand Up @@ -492,9 +496,13 @@ export async function processGraphQLRequest<TContext>(
return requestContext.response!;
}

async function didEncounterErrors(errors: ReadonlyArray<GraphQLError>) {
async function didEncounterErrors(
errorOrErrors: ReadonlyArray<GraphQLError> | GraphQLError,
context: TContext,
) {
const errors = ensureErrorArray(errorOrErrors);
requestContext.errors = errors;
extensionStack.didEncounterErrors(errors);
extensionStack.didEncounterErrors(errors, context);

return await dispatcher.invokeHookAsync(
'didEncounterErrors',
Expand All @@ -510,11 +518,7 @@ export async function processGraphQLRequest<TContext>(
errorClass?: typeof ApolloError,
) {
// If a single error is passed, it should still be encapsulated in an array.
const errors = Array.isArray(errorOrErrors)
? errorOrErrors
: [errorOrErrors];

await didEncounterErrors(errors);
const errors = ensureErrorArray(errorOrErrors);

return sendResponse({
errors: formatErrors(
Expand All @@ -539,6 +543,12 @@ export async function processGraphQLRequest<TContext>(
});
}

function ensureErrorArray(
errorOrErrors: ReadonlyArray<GraphQLError> | GraphQLError,
): ReadonlyArray<GraphQLError> {
return Array.isArray(errorOrErrors) ? errorOrErrors : [errorOrErrors];
}

function initializeRequestListenerDispatcher(): Dispatcher<
GraphQLRequestListener
> {
Expand Down
12 changes: 9 additions & 3 deletions packages/graphql-extensions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export class GraphQLExtension<TContext = any> {
executionArgs: ExecutionArgs;
}): EndHandler | void;

public didEncounterErrors?(errors: ReadonlyArray<GraphQLError>): void;
public didEncounterErrors?(
errors: ReadonlyArray<GraphQLError>,
context: TContext,
): void;

public willSendResponse?(o: {
graphqlResponse: GraphQLResponse;
Expand Down Expand Up @@ -107,10 +110,13 @@ export class GraphQLExtensionStack<TContext = any> {
);
}

public didEncounterErrors(errors: ReadonlyArray<GraphQLError>) {
public didEncounterErrors(
errors: ReadonlyArray<GraphQLError>,
context: TContext,
) {
this.extensions.forEach(extension => {
if (extension.didEncounterErrors) {
extension.didEncounterErrors(errors);
extension.didEncounterErrors(errors, context);
}
});
}
Expand Down