Skip to content

Commit

Permalink
lambda: Implement onHealthCheck on createHandler. (#3458)
Browse files Browse the repository at this point in the history
  • Loading branch information
glenthomas authored and abernix committed Nov 13, 2019
1 parent df84187 commit 84e1aa5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 31 additions & 1 deletion packages/apollo-server-lambda/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface CreateHandlerOptions {
credentials?: boolean;
maxAge?: number;
};
onHealthCheck?: (req: APIGatewayProxyEvent) => Promise<any>;
}

export class ApolloServer extends ApolloServerBase {
Expand All @@ -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.
Expand Down Expand Up @@ -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')) {
Expand Down

0 comments on commit 84e1aa5

Please sign in to comment.