Skip to content

Commit 04a6b10

Browse files
authored
Merge pull request #95 from shawnmcknight/onAfterCall-support
Add onAfterCall support
2 parents f80ac10 + cf8c87f commit 04a6b10

3 files changed

Lines changed: 58 additions & 28 deletions

File tree

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ declare module "moleculer-apollo-server" {
8181
fallbackResponse?: any;
8282
};
8383
onBeforeCall?: (ctx: Context, route: any, req: any, res: any) => Promise<any>;
84-
onAfterCall?: (ctx: Context, route: any, req: any, res: any) => Promise<any>;
84+
onAfterCall?: (ctx: Context, route: any, req: any, res: any, data: any) => Promise<any>;
8585
}
8686

8787
export interface ApolloServiceOptions {

src/ApolloServer.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ const { renderPlaygroundPage } = require("@apollographql/graphql-playground-html
66
const accept = require("@hapi/accept");
77
const moleculerApollo = require("./moleculerApollo");
88

9-
function send(req, res, statusCode, data, responseType = "application/json") {
9+
async function send(req, res, statusCode, data, responseType = "application/json") {
1010
res.statusCode = statusCode;
1111

1212
const ctx = res.$ctx;
1313
if (!ctx.meta.$responseType) {
1414
ctx.meta.$responseType = responseType;
1515
}
1616

17+
const route = res.$route;
18+
if (route.onAfterCall) {
19+
data = await route.onAfterCall.call(this, ctx, route, req, res, data);
20+
}
21+
1722
const service = res.$service;
1823
service.sendResponse(req, res, data);
1924
}
@@ -63,22 +68,22 @@ class ApolloServer extends ApolloServerBase {
6368
endpoint: this.graphqlPath,
6469
subscriptionEndpoint: this.subscriptionsPath,
6570
},
66-
this.playgroundOptions,
71+
this.playgroundOptions
6772
);
6873
return send(
6974
req,
7075
res,
7176
200,
7277
renderPlaygroundPage(middlewareOptions),
73-
"text/html",
78+
"text/html"
7479
);
7580
}
7681
}
7782

7883
// Handle incoming GraphQL requests using Apollo Server.
7984
const graphqlHandler = moleculerApollo(() => this.createGraphQLServerOptions(req, res));
8085
const responseData = await graphqlHandler(req, res);
81-
send(req, res, 200, responseData);
86+
return send(req, res, 200, responseData);
8287
};
8388
}
8489

@@ -96,10 +101,10 @@ class ApolloServer extends ApolloServerBase {
96101
onHealthCheck = onHealthCheck || (() => undefined);
97102
try {
98103
const result = await onHealthCheck(req);
99-
send(req, res, 200, { status: "pass", result }, "application/health+json");
104+
return send(req, res, 200, { status: "pass", result }, "application/health+json");
100105
} catch (error) {
101106
const result = error instanceof Error ? error.toString() : error;
102-
send(req, res, 503, { status: "fail", result }, "application/health+json");
107+
return send(req, res, 503, { status: "fail", result }, "application/health+json");
103108
}
104109
}
105110
}

test/unit/ApolloServer.spec.js

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ const ApolloServer = require("../../src/ApolloServer").ApolloServer;
1717
//ApolloServerCore.convertNodeHttpToRequest.mockImplementation(() => "convertedRequest");
1818

1919
describe("Test ApolloServer", () => {
20-
it("should support Uploads", () => {
20+
test("should support Uploads", () => {
2121
const apolloServer = new ApolloServer({});
2222
expect(apolloServer.supportsUploads()).toBe(true);
2323
});
2424

25-
it("should support subscriptions", () => {
25+
test("should support subscriptions", () => {
2626
const apolloServer = new ApolloServer({});
2727
expect(apolloServer.supportsSubscriptions()).toBe(true);
2828
});
2929

30-
it("should call super graphQLServerOptions", () => {
30+
test("should call super graphQLServerOptions", () => {
3131
const apolloServer = new ApolloServer({});
3232
ApolloServerBase.prototype.graphQLServerOptions = jest.fn();
3333

@@ -54,9 +54,10 @@ describe("Test ApolloServer", () => {
5454
const fakeRes = {
5555
$ctx: fakeCtx,
5656
$service: fakeService,
57+
$route: {},
5758
};
5859

59-
it("should return 200 'pass'", async () => {
60+
test("should return 200 'pass'", async () => {
6061
const onHealthCheck = jest.fn(() => "Everything OK");
6162

6263
await apolloServer.handleHealthCheck({ req: fakeReq, res: fakeRes, onHealthCheck });
@@ -75,7 +76,7 @@ describe("Test ApolloServer", () => {
7576
expect(fakeCtx.meta.$responseType).toBe("application/health+json");
7677
});
7778

78-
it("should return 503 'fail'", async () => {
79+
test("should return 503 'fail'", async () => {
7980
fakeService.sendResponse.mockClear();
8081

8182
const onHealthCheck = jest.fn(() => Promise.reject(new Error("Something wrong")));
@@ -96,7 +97,7 @@ describe("Test ApolloServer", () => {
9697
expect(fakeCtx.meta.$responseType).toBe("application/health+json");
9798
});
9899

99-
it("should call an empty healthcheck function", async () => {
100+
test("should call an empty healthcheck function", async () => {
100101
fakeService.sendResponse.mockClear();
101102
fakeCtx.meta.$responseType = "application/json";
102103

@@ -130,15 +131,21 @@ describe("Test ApolloServer", () => {
130131
sendResponse: jest.fn(),
131132
};
132133

133-
let fakeReq = {
134-
headers: {},
135-
};
136-
let fakeRes = {
137-
$ctx: fakeCtx,
138-
$service: fakeService,
139-
};
134+
let fakeReq;
135+
let fakeRes;
140136

141-
it("should handle as a request", async () => {
137+
beforeEach(() => {
138+
fakeReq = {
139+
headers: {},
140+
};
141+
fakeRes = {
142+
$ctx: fakeCtx,
143+
$service: fakeService,
144+
$route: {},
145+
};
146+
});
147+
148+
test("should handle as a request", async () => {
142149
const handler = apolloServer.createHandler();
143150

144151
await handler(fakeReq, fakeRes);
@@ -155,7 +162,7 @@ describe("Test ApolloServer", () => {
155162
expect(fakeService.sendResponse).toBeCalledWith(
156163
fakeReq,
157164
fakeRes,
158-
"GraphQL Response Data",
165+
"GraphQL Response Data"
159166
);
160167

161168
expect(fakeCtx.meta.$responseType).toBe("application/json");
@@ -166,7 +173,25 @@ describe("Test ApolloServer", () => {
166173
expect(apolloServer.createGraphQLServerOptions).toBeCalledWith(fakeReq, fakeRes);
167174
});
168175

169-
it("should handle as a file upload request", async () => {
176+
test("should call onAfterCall function", async () => {
177+
const handler = apolloServer.createHandler();
178+
const onAfterCall = jest.fn();
179+
const $route = { onAfterCall };
180+
fakeRes.$route = $route;
181+
182+
await handler(fakeReq, fakeRes);
183+
184+
expect(onAfterCall).toHaveBeenCalledTimes(1);
185+
expect(onAfterCall).toHaveBeenCalledWith(
186+
fakeCtx,
187+
$route,
188+
fakeReq,
189+
fakeRes,
190+
"GraphQL Response Data"
191+
);
192+
});
193+
194+
test("should handle as a file upload request", async () => {
170195
// Clear mocks
171196
moleculerApollo.mockClear();
172197
fakeGraphqlHandler.mockClear();
@@ -190,7 +215,7 @@ describe("Test ApolloServer", () => {
190215
expect(GraphqlUpload.processRequest).toBeCalledWith(
191216
fakeReq,
192217
fakeRes,
193-
apolloServer.uploadsConfig,
218+
apolloServer.uploadsConfig
194219
);
195220

196221
expect(moleculerApollo).toBeCalledTimes(1);
@@ -205,13 +230,13 @@ describe("Test ApolloServer", () => {
205230
expect(fakeService.sendResponse).toBeCalledWith(
206231
fakeReq,
207232
fakeRes,
208-
"GraphQL Response Data",
233+
"GraphQL Response Data"
209234
);
210235

211236
expect(fakeCtx.meta.$responseType).toBe("application/json");
212237
});
213238

214-
it("should handle as health-check request", async () => {
239+
test("should handle as health-check request", async () => {
215240
// Clear mocks
216241
moleculerApollo.mockClear();
217242
fakeGraphqlHandler.mockClear();
@@ -241,7 +266,7 @@ describe("Test ApolloServer", () => {
241266
expect(moleculerApollo).toBeCalledTimes(0);
242267
});
243268

244-
it("should not handle as health-check request if disabled", async () => {
269+
test("should not handle as health-check request if disabled", async () => {
245270
// Clear mocks
246271
moleculerApollo.mockClear();
247272
fakeGraphqlHandler.mockClear();
@@ -265,7 +290,7 @@ describe("Test ApolloServer", () => {
265290
expect(moleculerApollo).toBeCalledTimes(1);
266291
});
267292

268-
it("should handle as playground request", async () => {
293+
test("should handle as playground request", async () => {
269294
// Clear mocks
270295
moleculerApollo.mockClear();
271296
fakeService.sendResponse.mockClear();

0 commit comments

Comments
 (0)