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

Ensure executionDidEnd hooks are only called once #6846

Merged
merged 3 commits into from Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-days-eat.md
@@ -0,0 +1,5 @@
---
"@apollo/server": patch
---

Ensure executionDidEnd hooks are only called once (when they throw)
39 changes: 39 additions & 0 deletions packages/server/src/__tests__/runQuery.test.ts
Expand Up @@ -22,6 +22,7 @@ import {
GraphQLRequestListenerParsingDidEnd,
GraphQLRequestListenerValidationDidEnd,
} from '..';
import { mockLogger } from './mockLogger';

async function runQuery(
config: ApolloServerOptions<BaseContext>,
Expand Down Expand Up @@ -425,6 +426,44 @@ describe('request pipeline life-cycle hooks', () => {
expect(executionDidStart).toHaveBeenCalledTimes(1);
expect(executionDidEnd).toHaveBeenCalledTimes(1);
});

it('is only called once if it throws', async () => {
const executionDidEnd = jest.fn(() => {
throw new Error('boom');
});

const plugins = [
{
async requestDidStart() {
return {
async executionDidStart() {
return {
executionDidEnd,
};
},
};
},
},
];

const logger = mockLogger();

await expect(
runQuery(
{
schema,
plugins,
logger,
},
{ query: '{ testString }' },
),
).rejects.toThrowError(/Internal server error/);

expect(executionDidEnd).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenCalledWith(
'Unexpected error processing request: Error: boom',
);
});
});

describe('willResolveField', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/requestPipeline.ts
Expand Up @@ -459,8 +459,6 @@ export async function processGraphQLRequest<TContext extends BaseContext>(
...result,
errors: resultErrors ? formatErrors(resultErrors) : undefined,
};

await Promise.all(executionListeners.map((l) => l.executionDidEnd?.()));
} catch (executionMaybeError: unknown) {
const executionError = ensureError(executionMaybeError);
await Promise.all(
Expand All @@ -472,6 +470,8 @@ export async function processGraphQLRequest<TContext extends BaseContext>(
newHTTPGraphQLHead(statusIfExecuteThrows),
);
}

await Promise.all(executionListeners.map((l) => l.executionDidEnd?.()));
}

await invokeWillSendResponse();
Expand Down