@@ -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