Skip to content

Commit

Permalink
support async lambda handler
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari authored and glasser committed Mar 9, 2021
1 parent 6378cfe commit 5a72050
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 14 additions & 4 deletions packages/apollo-server-lambda/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ export class ApolloServer extends ApolloServerBase {
context: LambdaContext,
callback: APIGatewayProxyCallback,
) => {

const callbackOverride: APIGatewayProxyCallback = (error, result) => {
if (error === null) {
return result;
}

throw error;
};

callback = callback || callbackOverride;

// We re-load the headers into a Fetch API-compatible `Headers`
// interface within `graphqlLambda`, but we still need to respect the
// case-insensitivity within this logic here, so we'll need to do it
Expand Down Expand Up @@ -183,7 +194,7 @@ export class ApolloServer extends ApolloServerBase {
},
};
if (onHealthCheck) {
onHealthCheck(event)
return onHealthCheck(event)
.then(() => {
return callback(null, successfulResponse);
})
Expand All @@ -197,7 +208,6 @@ export class ApolloServer extends ApolloServerBase {
},
});
});
return;
} else {
return callback(null, successfulResponse);
}
Expand Down Expand Up @@ -230,7 +240,7 @@ export class ApolloServer extends ApolloServerBase {
const response = new Writable() as ServerResponse;
const callbackFilter: APIGatewayProxyCallback = (error, result) => {
response.end();
callback(
return callback(
error,
result && {
...result,
Expand Down Expand Up @@ -273,7 +283,7 @@ export class ApolloServer extends ApolloServerBase {
}
};

fileUploadHandler(() => graphqlLambda(async () => {
return fileUploadHandler(() => graphqlLambda(async () => {
// In a world where this `createHandler` was async, we might avoid this
// but since we don't want to introduce a breaking change to this API
// (by switching it to `async`), we'll leverage the
Expand Down
8 changes: 4 additions & 4 deletions packages/apollo-server-lambda/src/lambdaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function graphqlLambda(
event,
context,
callback,
): void => {
): any => {
context.callbackWaitsForEmptyEventLoop = false;
let { body, headers, isBase64Encoded } = event;
let query: Record<string, any> | Record<string, any>[];
Expand Down Expand Up @@ -58,7 +58,7 @@ export function graphqlLambda(
query = event.queryStringParameters || {};
}

runHttpQuery([event, context], {
return runHttpQuery([event, context], {
method: event.httpMethod,
options: options,
query,
Expand All @@ -69,15 +69,15 @@ export function graphqlLambda(
},
}).then(
({ graphqlResponse, responseInit }) => {
callback(null, {
return callback(null, {
body: graphqlResponse,
statusCode: 200,
headers: responseInit.headers,
});
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) return callback(error);
callback(null, {
return callback(null, {
body: error.message,
statusCode: error.statusCode,
headers: error.headers,
Expand Down

0 comments on commit 5a72050

Please sign in to comment.