From 84e1aa5ad3ee2acf410ca8984cfc68e90d4f8a66 Mon Sep 17 00:00:00 2001 From: Glen Thomas Date: Wed, 13 Nov 2019 12:15:17 +0000 Subject: [PATCH] lambda: Implement `onHealthCheck` on `createHandler`. (#3458) --- CHANGELOG.md | 1 + .../apollo-server-lambda/src/ApolloServer.ts | 32 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46058a407de..3380fdace43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The version headers in this history reflect the versions of Apollo Server itself > The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the the appropriate changes within that release will be moved into the new section. - `apollo-server-core`: Don't try parsing `variables` and `extensions` as JSON if they are defined but empty strings. [PR #3501](https://github.com/apollographql/apollo-server/pull/3501) +- `apollo-server-lambda`: Introduce `onHealthCheck` on `createHandler` in the same fashion as implemented in other integrations. [PR #3458](https://github.com/apollographql/apollo-server/pull/3458) ### v2.9.8 diff --git a/packages/apollo-server-lambda/src/ApolloServer.ts b/packages/apollo-server-lambda/src/ApolloServer.ts index 679c0616fa3..181de51b061 100644 --- a/packages/apollo-server-lambda/src/ApolloServer.ts +++ b/packages/apollo-server-lambda/src/ApolloServer.ts @@ -21,6 +21,7 @@ export interface CreateHandlerOptions { credentials?: boolean; maxAge?: number; }; + onHealthCheck?: (req: APIGatewayProxyEvent) => Promise; } export class ApolloServer extends ApolloServerBase { @@ -47,7 +48,7 @@ export class ApolloServer extends ApolloServerBase { return super.graphQLServerOptions({ event, context }); } - public createHandler({ cors }: CreateHandlerOptions = { cors: undefined }) { + public createHandler({ cors, onHealthCheck }: CreateHandlerOptions = { cors: undefined, onHealthCheck: undefined }) { // We will kick off the `willStart` event once for the server, and then // await it before processing any requests by incorporating its `await` into // the GraphQLServerOptions function which is called before each request. @@ -159,6 +160,35 @@ export class ApolloServer extends ApolloServerBase { }); } + if (event.path === '/.well-known/apollo/server-health') { + const successfulResponse = { + body: JSON.stringify({ status: 'pass' }), + statusCode: 200, + headers: { + 'Content-Type': 'application/json', + ...requestCorsHeadersObject, + }, + }; + if (onHealthCheck) { + onHealthCheck(event) + .then(() => { + return callback(null, successfulResponse); + }) + .catch(() => { + return callback(null, { + body: JSON.stringify({ status: 'fail' }), + statusCode: 503, + headers: { + 'Content-Type': 'application/json', + ...requestCorsHeadersObject, + }, + }); + }); + } else { + return callback(null, successfulResponse); + } + } + if (this.playgroundOptions && event.httpMethod === 'GET') { const acceptHeader = event.headers['Accept'] || event.headers['accept']; if (acceptHeader && acceptHeader.includes('text/html')) {