Skip to content

Commit f25fe16

Browse files
authored
Merge pull request #123 from shawnmcknight/fix-unhandled-promises
Await graphql handler to avoid unhandled promise rejections
2 parents cef1659 + 26fabda commit f25fe16

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

src/service.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,14 +770,15 @@ module.exports = function (mixinOptions) {
770770
async "/"(req, res) {
771771
try {
772772
await this.prepareGraphQLSchema();
773-
return this.graphqlHandler(req, res);
773+
return await this.graphqlHandler(req, res);
774774
} catch (err) {
775775
this.sendError(req, res, err);
776776
}
777777
},
778778
async "GET /.well-known/apollo/server-health"(req, res) {
779779
try {
780780
await this.prepareGraphQLSchema();
781+
return await this.graphqlHandler(req, res);
781782
} catch (err) {
782783
res.statusCode = 503;
783784
return this.sendResponse(
@@ -787,7 +788,6 @@ module.exports = function (mixinOptions) {
787788
{ responseType: "application/health+json" }
788789
);
789790
}
790-
return this.graphqlHandler(req, res);
791791
},
792792
},
793793

test/unit/service.spec.js

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe("Test Service", () => {
7777
await stop();
7878
});
7979

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

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

100100
await stop();
101101
});
102+
103+
it("should call sendError if error occurs when handling graphql request", async () => {
104+
const { svc, stop } = await startService();
105+
106+
const err = new Error("Something happened");
107+
svc.sendError = jest.fn();
108+
svc.prepareGraphQLSchema = jest.fn();
109+
svc.graphqlHandler = jest.fn(() => {
110+
throw err;
111+
});
112+
const fakeReq = { req: 1 };
113+
const fakeRes = { res: 1 };
114+
115+
const res = await svc.settings.routes[0].aliases["/"].call(svc, fakeReq, fakeRes);
116+
117+
expect(res).toBeUndefined();
118+
expect(svc.prepareGraphQLSchema).toBeCalledTimes(1);
119+
expect(svc.graphqlHandler).toBeCalledTimes(1);
120+
expect(svc.sendError).toBeCalledTimes(1);
121+
expect(svc.sendError).toBeCalledWith(fakeReq, fakeRes, err);
122+
123+
await stop();
124+
});
102125
});
103126

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

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

129152
const err = new Error("Something happened");
@@ -146,13 +169,38 @@ describe("Test Service", () => {
146169
expect(svc.sendResponse).toBeCalledWith(
147170
fakeReq,
148171
fakeRes,
149-
{
150-
status: "fail",
151-
schema: false,
152-
},
153-
{
154-
responseType: "application/health+json",
155-
}
172+
{ status: "fail", schema: false },
173+
{ responseType: "application/health+json" }
174+
);
175+
176+
await stop();
177+
});
178+
179+
it("should call sendError if error occurs when handling graphql request", async () => {
180+
const { svc, stop } = await startService();
181+
182+
const err = new Error("Something happened");
183+
svc.sendResponse = jest.fn();
184+
svc.prepareGraphQLSchema = jest.fn();
185+
svc.graphqlHandler = jest.fn(() => {
186+
throw err;
187+
});
188+
const fakeReq = { req: 1 };
189+
const fakeRes = { res: 1 };
190+
191+
const res = await svc.settings.routes[0].aliases[
192+
"GET /.well-known/apollo/server-health"
193+
].call(svc, fakeReq, fakeRes);
194+
195+
expect(res).toBeUndefined();
196+
expect(svc.prepareGraphQLSchema).toBeCalledTimes(1);
197+
expect(svc.graphqlHandler).toBeCalledTimes(1);
198+
expect(svc.sendResponse).toBeCalledTimes(1);
199+
expect(svc.sendResponse).toBeCalledWith(
200+
fakeReq,
201+
fakeRes,
202+
{ status: "fail", schema: false },
203+
{ responseType: "application/health+json" }
156204
);
157205

158206
await stop();

0 commit comments

Comments
 (0)