Skip to content

Commit

Permalink
Implement fix, update test
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Aug 24, 2022
1 parent a0a6fab commit aa3b27d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
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

0 comments on commit aa3b27d

Please sign in to comment.