From abbbca5a839ef498e396b9036b95977ccada4dea Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Mon, 19 Sep 2022 16:58:59 -0700 Subject: [PATCH] Update handler name to match `startAndXYZ` convention (#9) Additionally, some minor cleanup and fix tests not shutting down correctly. --- packages/fastify/src/index.ts | 2 +- .../lambda/src/__tests__/integration.test.ts | 27 +++++------ packages/lambda/src/index.ts | 47 +++++++------------ 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/packages/fastify/src/index.ts b/packages/fastify/src/index.ts index 72a6648..051c72e 100644 --- a/packages/fastify/src/index.ts +++ b/packages/fastify/src/index.ts @@ -27,7 +27,7 @@ export interface FastifyHandlerOptions { export function fastifyHandler( server: ApolloServer, - options?: FastifyHandlerOptions, + options?: FastifyHandlerOptions ): RouteHandlerMethod; export function fastifyHandler( server: ApolloServer, diff --git a/packages/lambda/src/__tests__/integration.test.ts b/packages/lambda/src/__tests__/integration.test.ts index 273db6b..e3bf440 100644 --- a/packages/lambda/src/__tests__/integration.test.ts +++ b/packages/lambda/src/__tests__/integration.test.ts @@ -1,5 +1,4 @@ import { ApolloServer, ApolloServerOptions, BaseContext } from "@apollo/server"; -import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer"; import { CreateServerForIntegrationTestsOptions, defineIntegrationTestSuite, @@ -7,29 +6,21 @@ import { import { createServer, Server } from "http"; import type { AddressInfo } from "net"; import { format } from "url"; -import { lambdaHandler } from ".."; +import { startServerAndCreateLambdaHandler } from ".."; import { createMockServer as createAPIGatewayMockServer } from "./mockAPIGatewayServer"; -describe("lambdaHandler", () => { +describe("startServerAndCreateLambdaHandler", () => { defineIntegrationTestSuite( async function ( serverOptions: ApolloServerOptions, testOptions?: CreateServerForIntegrationTestsOptions, ) { const httpServer = createServer(); - const server = new ApolloServer({ - ...serverOptions, - plugins: [ - ...(serverOptions.plugins ?? []), - ApolloServerPluginDrainHttpServer({ - httpServer, - }), - ], - }); + const server = new ApolloServer(serverOptions); const handler = testOptions - ? lambdaHandler(server, testOptions) - : lambdaHandler(server); + ? startServerAndCreateLambdaHandler(server, testOptions) + : startServerAndCreateLambdaHandler(server); httpServer.addListener("request", createAPIGatewayMockServer(handler)); @@ -37,7 +28,13 @@ describe("lambdaHandler", () => { httpServer.listen({ port: 0 }, resolve); }); - return { server, url: urlForHttpServer(httpServer) }; + return { + server, + url: urlForHttpServer(httpServer), + extraCleanup: async () => { + httpServer.close(); + }, + }; }, { serverIsStartedInBackground: true, diff --git a/packages/lambda/src/index.ts b/packages/lambda/src/index.ts index 515c6a9..003a03c 100644 --- a/packages/lambda/src/index.ts +++ b/packages/lambda/src/index.ts @@ -25,15 +25,18 @@ type LambdaHandler = Handler< APIGatewayProxyStructuredResultV2 >; -export function lambdaHandler( +// Following the naming convention "startAndXYZ" for serverless handlers in the +// Apollo Server docs so that it's clear the server will be started when this +// function is called and the user should not call `start` themselves. +export function startServerAndCreateLambdaHandler( server: ApolloServer, options?: LambdaHandlerOptions, ): LambdaHandler; -export function lambdaHandler( +export function startServerAndCreateLambdaHandler( server: ApolloServer, options: WithRequired, "context">, ): LambdaHandler; -export function lambdaHandler( +export function startServerAndCreateLambdaHandler( server: ApolloServer, options?: LambdaHandlerOptions, ): LambdaHandler { @@ -54,33 +57,19 @@ export function lambdaHandler( return async function (event, context) { let parsedBody: object | string | undefined = undefined; - try { - if (!event.body) { - // assert there's a query string? - } else if (event.headers["content-type"] === "application/json") { - try { - parsedBody = JSON.parse(event.body); - } catch (e: unknown) { - return { - statusCode: 400, - body: (e as Error).message, - }; - } - } else if (event.headers["content-type"] === "text/plain") { - parsedBody = event.body; + if (!event.body) { + // assert there's a query string? + } else if (event.headers["content-type"] === "application/json") { + try { + parsedBody = JSON.parse(event.body); + } catch (e: unknown) { + return { + statusCode: 400, + body: (e as Error).message, + }; } - } catch (error: unknown) { - // The json body-parser *always* sets req.body to {} if it's unset (even - // if the Content-Type doesn't match), so if it isn't set, you probably - // forgot to set up body-parser. (Note that this may change in the future - // body-parser@2.) - // return { - // statusCode: 500, - // body: - // '`event.body` is not set; this probably means you forgot to set up the ' + - // '`body-parser` middleware before the Apollo Server middleware.', - // }; - throw error; + } else if (event.headers["content-type"] === "text/plain") { + parsedBody = event.body; } const headers = new Map();