Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apollo-server-lambda: Implement onHealthCheck on createHandler. #3458

Merged
merged 6 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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