DEV Community

Vigilmon
Vigilmon

Posted on

How to Monitor GraphQL APIs and Subscriptions with Vigilmon

GraphQL APIs need monitoring just like REST APIs — but there are some GraphQL-specific patterns worth knowing. This guide covers uptime monitoring for GraphQL APIs and heartbeat monitoring for subscriptions.

Step 1: Add a Health Endpoint to Your GraphQL Server

Don't monitor /graphql directly — add a dedicated health endpoint:

Apollo Server (Node.js)

import express from 'express';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';

const app = express();

app.get('/health', (req, res) => {
  res.json({ status: 'ok', service: 'graphql-api', timestamp: Date.now() });
});

const server = new ApolloServer({ typeDefs, resolvers });
await server.start();
app.use('/graphql', expressMiddleware(server));
Enter fullscreen mode Exit fullscreen mode

GraphQL Yoga

import { createYoga } from 'graphql-yoga';
import { createServer } from 'node:http';

const yoga = createYoga({ schema });

const server = createServer((req, res) => {
  if (req.url === '/health') {
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ status: 'ok' }));
    return;
  }
  yoga(req, res);
});
Enter fullscreen mode Exit fullscreen mode

Hasura

Hasura has a built-in health endpoint at /healthz — monitor it directly.

Step 2: Create the Vigilmon Monitor

In Vigilmon:

  • URL: https://api.yoursite.com/health
  • Method: GET
  • Check interval: 1 minute
  • Multi-region: enabled

Step 3: Monitor GraphQL Subscriptions (WebSocket)

GraphQL subscriptions run over WebSocket, which Vigilmon can't ping directly. Use heartbeat monitoring:

const subscriptionServer = createServer({ schema }, { server: httpServer });

const HEARTBEAT_URL = process.env.VIGILMON_HEARTBEAT_URL;

if (HEARTBEAT_URL) {
  setInterval(async () => {
    try { await fetch(HEARTBEAT_URL); } catch {}
  }, 60_000);
}

httpServer.listen(4000);
Enter fullscreen mode Exit fullscreen mode

Create a Vigilmon heartbeat monitor with a 2-minute period. If the subscription server stops, the heartbeat stops and you get an alert.

Step 4: Test Your GraphQL Schema is Healthy

// src/api/graphql-health.ts (Next.js API route)
export default async function handler(req, res) {
  try {
    const response = await fetch('http://localhost:4000/graphql', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ query: '{ __typename }' })
    });

    const data = await response.json();
    if (data.errors) return res.status(503).json({ status: 'error', errors: data.errors });
    res.json({ status: 'ok' });
  } catch (error) {
    res.status(503).json({ status: 'error', message: error.message });
  }
}
Enter fullscreen mode Exit fullscreen mode

What Vigilmon Catches for GraphQL APIs

What it catches How
Server is down HTTP health endpoint returns non-200
Server crashed Connection refused
Subscription server died Heartbeat stops arriving
API unreachable from certain regions Multi-region cross-check

Summary

  1. Add a /health endpoint to your GraphQL server
  2. Monitor it with Vigilmon (1-minute intervals, multi-region)
  3. Use heartbeat monitoring for WebSocket/subscription servers
  4. Set up alerts to your team's Slack or Discord

Vigilmon — API uptime monitoring for GraphQL, REST, and gRPC. Free plan available.

Top comments (0)