Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-days-eat.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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