Skip to content

Commit

Permalink
Ensure executionDidEnd hooks are only called once (#6846)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Aug 24, 2022
1 parent 0861ba6 commit 2cab8f7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
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

0 comments on commit 2cab8f7

Please sign in to comment.