Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Await graphql handler to avoid unhandled promise rejections #123

Merged
merged 1 commit into from
Feb 6, 2024
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
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
Loading