Skip to content

Commit

Permalink
feat(cli/http): customize health check endpoint (#6903)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Apr 29, 2024
1 parent 6fee296 commit 6044b7f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .changeset/forty-drinks-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@graphql-mesh/types": patch
"@graphql-mesh/http": patch
"@graphql-mesh/cli": patch
---

Previously GraphQL Yoga also had a health check endpoint in `/health` path while Mesh's health check endpoint is `/healthcheck`. Now they are both aligned.
Also now you can customize the health check endpoint within Mesh Configuration using `serve.healthCheckEndpoint` key. Default value is `/healthcheck.

```yaml
serve:
healthCheckEndpoint: /health
```
**Action Required:**
If you are using GraphQL Yoga's endpoint `/health`, instead of `/healthcheck`, you should update your health check endpoint to `/health` in the configuration like above to keep the behavior.
4 changes: 4 additions & 0 deletions packages/legacy/cli/src/commands/serve/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ type ServeConfig @md {
Enable and define a limit for [Request Batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)
"""
batchingLimit: Int
"""
Endpoint for [Health Check](https://the-guild.dev/graphql/yoga-server/docs/features/health-check)
"""
healthCheckEndpoint: String
}

union Port = Int | String
Expand Down
3 changes: 3 additions & 0 deletions packages/legacy/http/src/graphqlHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ export const graphqlHandler = ({
graphqlEndpoint,
corsConfig,
batchingLimit,
healthCheckEndpoint = '/healthcheck',
}: {
getBuiltMesh: () => Promise<MeshInstance>;
playgroundTitle: string;
playgroundEnabled: boolean;
graphqlEndpoint: string;
corsConfig: CORSOptions;
batchingLimit?: number;
healthCheckEndpoint?: string;
}) => {
const getYogaForMesh = memoize1(function getYogaForMesh(mesh: MeshInstance) {
return createYoga({
Expand All @@ -39,6 +41,7 @@ export const graphqlHandler = ({
graphqlEndpoint,
landingPage: false,
batching: batchingLimit ? { limit: batchingLimit } : false,
healthCheckEndpoint,
});
});
return (request: Request, ctx: any) =>
Expand Down
3 changes: 2 additions & 1 deletion packages/legacy/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function createMeshHTTPHandler<TServerContext>({
playground: playgroundEnabled = process.env.NODE_ENV !== 'production',
endpoint: graphqlPath = '/graphql',
batchingLimit,
healthCheckEndpoint = '/healthcheck',
// TODO
// trustProxy = 'loopback',
} = rawServeConfig;
Expand Down Expand Up @@ -55,7 +56,7 @@ export function createMeshHTTPHandler<TServerContext>({
{
onRequest({ request, url, endResponse }): void | Promise<void> {
switch (url.pathname) {
case '/healthcheck':
case healthCheckEndpoint:
endResponse(
new Response(null, {
status: 200,
Expand Down
37 changes: 37 additions & 0 deletions packages/legacy/http/test/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,41 @@ describe('http', () => {
afterEach(() => {
mesh.destroy();
});
describe('health check', () => {
it('should return 200', async () => {
const httpHandler = createMeshHTTPHandler({
baseDir: __dirname,
getBuiltMesh: async () => mesh,
});
const response = await httpHandler.fetch('http://localhost:4000/healthcheck');
expect(response.status).toBe(200);
});
it('should return 503 when not ready', async () => {
let resolve: VoidFunction;
const readyPromise = new Promise<void>(r => {
resolve = r;
});
const httpHandler = createMeshHTTPHandler({
baseDir: __dirname,
getBuiltMesh: async () => {
await readyPromise;
return mesh;
},
});
const response = await httpHandler.fetch('http://localhost:4000/readiness');
expect(response.status).toBe(503);
resolve();
});
it('should be able to customize health check endpoint', async () => {
const httpHandler = createMeshHTTPHandler({
baseDir: __dirname,
getBuiltMesh: async () => mesh,
rawServeConfig: {
healthCheckEndpoint: '/custom-health-check',
},
});
const response = await httpHandler.fetch('http://localhost:4000/custom-health-check');
expect(response.status).toBe(200);
});
});
});
4 changes: 4 additions & 0 deletions packages/legacy/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@
"batchingLimit": {
"type": "integer",
"description": "Enable and define a limit for [Request Batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)"
},
"healthCheckEndpoint": {
"type": "string",
"description": "Endpoint for [Health Check](https://the-guild.dev/graphql/yoga-server/docs/features/health-check)"
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions packages/legacy/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export interface ServeConfig {
* Enable and define a limit for [Request Batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)
*/
batchingLimit?: number;
/**
* Endpoint for [Health Check](https://the-guild.dev/graphql/yoga-server/docs/features/health-check)
*/
healthCheckEndpoint?: string;
}
/**
* Configuration for CORS
Expand Down
3 changes: 2 additions & 1 deletion website/src/generated-markdown/ServeConfig.generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ This feature can be disabled by passing `false` One of:
* `playgroundTitle` (type: `String`) - Title of GraphiQL Playground
* `trustProxy` (type: `String`) - Configure Express Proxy Handling
[Learn more](https://expressjs.com/en/guide/behind-proxies.html)
* `batchingLimit` (type: `Int`) - Enable and define a limit for [Request Batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)
* `batchingLimit` (type: `Int`) - Enable and define a limit for [Request Batching](https://github.com/graphql/graphql-over-http/blob/main/rfcs/Batching.md)
* `healthCheckEndpoint` (type: `String`) - Endpoint for [Health Check](https://the-guild.dev/graphql/yoga-server/docs/features/health-check)

0 comments on commit 6044b7f

Please sign in to comment.