Skip to content

Commit abbbca5

Browse files
Update handler name to match startAndXYZ convention (#9)
Additionally, some minor cleanup and fix tests not shutting down correctly.
1 parent 7b7a01d commit abbbca5

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

packages/fastify/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface FastifyHandlerOptions<TContext extends BaseContext> {
2727

2828
export function fastifyHandler(
2929
server: ApolloServer<BaseContext>,
30-
options?: FastifyHandlerOptions<BaseContext>,
30+
options?: FastifyHandlerOptions<BaseContext>
3131
): RouteHandlerMethod;
3232
export function fastifyHandler<TContext extends BaseContext>(
3333
server: ApolloServer<TContext>,

packages/lambda/src/__tests__/integration.test.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
import { ApolloServer, ApolloServerOptions, BaseContext } from "@apollo/server";
2-
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
32
import {
43
CreateServerForIntegrationTestsOptions,
54
defineIntegrationTestSuite,
65
} from "@apollo/server-integration-testsuite";
76
import { createServer, Server } from "http";
87
import type { AddressInfo } from "net";
98
import { format } from "url";
10-
import { lambdaHandler } from "..";
9+
import { startServerAndCreateLambdaHandler } from "..";
1110
import { createMockServer as createAPIGatewayMockServer } from "./mockAPIGatewayServer";
1211

13-
describe("lambdaHandler", () => {
12+
describe("startServerAndCreateLambdaHandler", () => {
1413
defineIntegrationTestSuite(
1514
async function (
1615
serverOptions: ApolloServerOptions<BaseContext>,
1716
testOptions?: CreateServerForIntegrationTestsOptions,
1817
) {
1918
const httpServer = createServer();
20-
const server = new ApolloServer({
21-
...serverOptions,
22-
plugins: [
23-
...(serverOptions.plugins ?? []),
24-
ApolloServerPluginDrainHttpServer({
25-
httpServer,
26-
}),
27-
],
28-
});
19+
const server = new ApolloServer(serverOptions);
2920

3021
const handler = testOptions
31-
? lambdaHandler(server, testOptions)
32-
: lambdaHandler(server);
22+
? startServerAndCreateLambdaHandler(server, testOptions)
23+
: startServerAndCreateLambdaHandler(server);
3324

3425
httpServer.addListener("request", createAPIGatewayMockServer(handler));
3526

3627
await new Promise<void>((resolve) => {
3728
httpServer.listen({ port: 0 }, resolve);
3829
});
3930

40-
return { server, url: urlForHttpServer(httpServer) };
31+
return {
32+
server,
33+
url: urlForHttpServer(httpServer),
34+
extraCleanup: async () => {
35+
httpServer.close();
36+
},
37+
};
4138
},
4239
{
4340
serverIsStartedInBackground: true,

packages/lambda/src/index.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ type LambdaHandler = Handler<
2525
APIGatewayProxyStructuredResultV2
2626
>;
2727

28-
export function lambdaHandler(
28+
// Following the naming convention "startAndXYZ" for serverless handlers in the
29+
// Apollo Server docs so that it's clear the server will be started when this
30+
// function is called and the user should not call `start` themselves.
31+
export function startServerAndCreateLambdaHandler(
2932
server: ApolloServer<BaseContext>,
3033
options?: LambdaHandlerOptions<BaseContext>,
3134
): LambdaHandler;
32-
export function lambdaHandler<TContext extends BaseContext>(
35+
export function startServerAndCreateLambdaHandler<TContext extends BaseContext>(
3336
server: ApolloServer<TContext>,
3437
options: WithRequired<LambdaHandlerOptions<TContext>, "context">,
3538
): LambdaHandler;
36-
export function lambdaHandler<TContext extends BaseContext>(
39+
export function startServerAndCreateLambdaHandler<TContext extends BaseContext>(
3740
server: ApolloServer<TContext>,
3841
options?: LambdaHandlerOptions<TContext>,
3942
): LambdaHandler {
@@ -54,33 +57,19 @@ export function lambdaHandler<TContext extends BaseContext>(
5457

5558
return async function (event, context) {
5659
let parsedBody: object | string | undefined = undefined;
57-
try {
58-
if (!event.body) {
59-
// assert there's a query string?
60-
} else if (event.headers["content-type"] === "application/json") {
61-
try {
62-
parsedBody = JSON.parse(event.body);
63-
} catch (e: unknown) {
64-
return {
65-
statusCode: 400,
66-
body: (e as Error).message,
67-
};
68-
}
69-
} else if (event.headers["content-type"] === "text/plain") {
70-
parsedBody = event.body;
60+
if (!event.body) {
61+
// assert there's a query string?
62+
} else if (event.headers["content-type"] === "application/json") {
63+
try {
64+
parsedBody = JSON.parse(event.body);
65+
} catch (e: unknown) {
66+
return {
67+
statusCode: 400,
68+
body: (e as Error).message,
69+
};
7170
}
72-
} catch (error: unknown) {
73-
// The json body-parser *always* sets req.body to {} if it's unset (even
74-
// if the Content-Type doesn't match), so if it isn't set, you probably
75-
// forgot to set up body-parser. (Note that this may change in the future
76-
// body-parser@2.)
77-
// return {
78-
// statusCode: 500,
79-
// body:
80-
// '`event.body` is not set; this probably means you forgot to set up the ' +
81-
// '`body-parser` middleware before the Apollo Server middleware.',
82-
// };
83-
throw error;
71+
} else if (event.headers["content-type"] === "text/plain") {
72+
parsedBody = event.body;
8473
}
8574

8675
const headers = new Map<string, string>();

0 commit comments

Comments
 (0)