Skip to content

Commit

Permalink
Merge pull request #123 from shawnmcknight/fix-unhandled-promises
Browse files Browse the repository at this point in the history
Await graphql handler to avoid unhandled promise rejections
  • Loading branch information
icebob authored Feb 6, 2024
2 parents cef1659 + 26fabda commit f25fe16
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,14 +770,15 @@ module.exports = function (mixinOptions) {
async "/"(req, res) {
try {
await this.prepareGraphQLSchema();
return this.graphqlHandler(req, res);
return await this.graphqlHandler(req, res);
} catch (err) {
this.sendError(req, res, err);
}
},
async "GET /.well-known/apollo/server-health"(req, res) {
try {
await this.prepareGraphQLSchema();
return await this.graphqlHandler(req, res);
} catch (err) {
res.statusCode = 503;
return this.sendResponse(
Expand All @@ -787,7 +788,6 @@ module.exports = function (mixinOptions) {
{ responseType: "application/health+json" }
);
}
return this.graphqlHandler(req, res);
},
},

Expand Down
66 changes: 57 additions & 9 deletions test/unit/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("Test Service", () => {
await stop();
});

it("should call sendError if error occured", async () => {
it("should call sendError if error occurs when preparing graphql schema", async () => {
const { svc, stop } = await startService();

const err = new Error("Something happened");
Expand All @@ -99,6 +99,29 @@ describe("Test Service", () => {

await stop();
});

it("should call sendError if error occurs when handling graphql request", async () => {
const { svc, stop } = await startService();

const err = new Error("Something happened");
svc.sendError = jest.fn();
svc.prepareGraphQLSchema = jest.fn();
svc.graphqlHandler = jest.fn(() => {
throw err;
});
const fakeReq = { req: 1 };
const fakeRes = { res: 1 };

const res = await svc.settings.routes[0].aliases["/"].call(svc, fakeReq, fakeRes);

expect(res).toBeUndefined();
expect(svc.prepareGraphQLSchema).toBeCalledTimes(1);
expect(svc.graphqlHandler).toBeCalledTimes(1);
expect(svc.sendError).toBeCalledTimes(1);
expect(svc.sendError).toBeCalledWith(fakeReq, fakeRes, err);

await stop();
});
});

describe("Test `GET /.well-known/apollo/server-health` route handler", () => {
Expand All @@ -123,7 +146,7 @@ describe("Test Service", () => {
await stop();
});

it("should call sendError if error occured", async () => {
it("should call sendError if error occurs when preparing graphql schema", async () => {
const { svc, stop } = await startService();

const err = new Error("Something happened");
Expand All @@ -146,13 +169,38 @@ describe("Test Service", () => {
expect(svc.sendResponse).toBeCalledWith(
fakeReq,
fakeRes,
{
status: "fail",
schema: false,
},
{
responseType: "application/health+json",
}
{ status: "fail", schema: false },
{ responseType: "application/health+json" }
);

await stop();
});

it("should call sendError if error occurs when handling graphql request", async () => {
const { svc, stop } = await startService();

const err = new Error("Something happened");
svc.sendResponse = jest.fn();
svc.prepareGraphQLSchema = jest.fn();
svc.graphqlHandler = jest.fn(() => {
throw err;
});
const fakeReq = { req: 1 };
const fakeRes = { res: 1 };

const res = await svc.settings.routes[0].aliases[
"GET /.well-known/apollo/server-health"
].call(svc, fakeReq, fakeRes);

expect(res).toBeUndefined();
expect(svc.prepareGraphQLSchema).toBeCalledTimes(1);
expect(svc.graphqlHandler).toBeCalledTimes(1);
expect(svc.sendResponse).toBeCalledTimes(1);
expect(svc.sendResponse).toBeCalledWith(
fakeReq,
fakeRes,
{ status: "fail", schema: false },
{ responseType: "application/health+json" }
);

await stop();
Expand Down

0 comments on commit f25fe16

Please sign in to comment.