On this page

Self-Hosted Supergraph Mirror

Keep a copy of your supergraph on your own infrastructure, updated by webhooks, so your router does not depend on the Hive CDN.

The CDN mirrors protect you against an outage of a single provider. Some teams need more than that, for example because their gateways must not depend on any infrastructure outside their own, or because outbound traffic from the gateway network is restricted.

For Federation projects, you can keep a copy of the supergraph on your own infrastructure. Hive notifies you through a webhook when a new schema version is published, and you copy the supergraph from the CDN to your own object storage (for example Amazon S3, Google Cloud Storage, Azure Blob Storage, Cloudflare R2 or MinIO). Your gateway then loads the supergraph from your storage, either as an additional mirror or as its only source, and does not need to reach the Hive CDN at all.

Federation RouterYour Object StorageHive CDNWebhook ListenerHive ConsoleFederation RouterYour Object StorageHive CDNWebhook ListenerHive ConsolePOST schema changed notificationGET /artifacts/v1/TARGET_ID/supergraphSupergraphStore supergraphPoll supergraph

Create an HTTP webhook alert

In your project’s Alerts tab, create an HTTP Webhook alert channel that points to your webhook listener. Then create a Schema Changed Notification alert for the target your gateway uses, with that channel.

Hive sends this notification every time a valid, composable schema is published to the target.

Copy the supergraph to your storage

When the listener receives a notification, it downloads the supergraph from the CDN with a CDN access token and writes it to your storage.

Use the notification only as a trigger and always download the latest supergraph (/artifacts/v1/TARGET_ID/supergraph), instead of the version referenced in the payload. Failed deliveries are retried, so notifications can arrive late or more than once. Downloading the latest supergraph makes the listener idempotent.

Several syncs can still run at the same time (for example a webhook and a scheduled run), and the one that downloaded an older supergraph might write last. To prevent that, use the Last-Modified header of the CDN response, which holds the time the supergraph was published. Store it next to the supergraph and skip the write when your storage already holds a newer one. A conditional write (for example If-Match on Amazon S3) makes this check safe across several listener instances.

Store the supergraph under a key that ends in /supergraph, such as artifacts/v1/TARGET_ID/supergraph. This keeps the same layout as the Hive CDN, which lets you use your storage as a drop-in mirror (see below).

Here is a minimal listener written in Node.js that stores the supergraph in Amazon S3:

webhook-listener.ts
import { createServer } from "node:http";
import { HeadObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";

const CDN_ENDPOINTS = [
  "https://cdn.graphql-hive.com",
  "https://cdn-mirror.graphql-hive.com",
];
const CDN_ACCESS_TOKEN = process.env.HIVE_CDN_ACCESS_TOKEN!;
const TARGET_ID = process.env.HIVE_TARGET_ID!;
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET!;
const BUCKET = process.env.SUPERGRAPH_BUCKET!;

const s3 = new S3Client({});

async function fetchSupergraph() {
  for (const endpoint of CDN_ENDPOINTS) {
    try {
      const response = await fetch(
        `${endpoint}/artifacts/v1/${TARGET_ID}/supergraph`,
        { headers: { "X-Hive-CDN-Key": CDN_ACCESS_TOKEN } },
      );
      if (response.ok) {
        return {
          supergraph: await response.text(),
          versionId: response.headers.get("x-hive-schema-version-id"),
          // The time the supergraph was published
          lastModified: response.headers.get("last-modified"),
        };
      }
    } catch {
      // try the next endpoint
    }
  }
  throw new Error("Failed to load the supergraph from the Hive CDN");
}

async function syncSupergraph() {
  const { supergraph, versionId, lastModified } = await fetchSupergraph();
  const key = `artifacts/v1/${TARGET_ID}/supergraph`;

  const stored = await s3
    .send(new HeadObjectCommand({ Bucket: BUCKET, Key: key }))
    .catch((error) => {
      if (error.name === "NotFound") return null;
      throw error;
    });
  const storedLastModified = stored?.Metadata?.["hive-last-modified"];
  if (storedLastModified && Date.parse(storedLastModified) > Date.parse(lastModified)) {
    // Another sync already stored a newer supergraph.
    return;
  }

  await s3.send(
    new PutObjectCommand({
      Bucket: BUCKET,
      Key: key,
      Body: supergraph,
      ContentType: "text/plain",
      Metadata: {
        "hive-last-modified": lastModified,
        ...(versionId ? { "hive-schema-version-id": versionId } : {}),
      },
      // Fails if another sync wrote in the meantime. The webhook delivery is then retried.
      ...(stored ? { IfMatch: stored.ETag } : { IfNoneMatch: "*" }),
    }),
  );
}

createServer(async (req, res) => {
  const url = new URL(req.url ?? "/", "http://localhost");
  // The webhook payload is only a trigger, so a shared secret in the webhook URL is enough
  // to keep others from triggering the sync.
  if (req.method !== "POST" || url.searchParams.get("secret") !== WEBHOOK_SECRET) {
    res.writeHead(404).end();
    return;
  }
  try {
    await syncSupergraph();
    res.writeHead(200).end();
  } catch (error) {
    console.error(error);
    // A non-2xx response makes Hive retry the delivery.
    res.writeHead(500).end();
  }
}).listen(4000);

If your project uses contracts, copy each contract’s supergraph from /artifacts/v1/TARGET_ID/contracts/CONTRACT_NAME/supergraph the same way.

Load the supergraph from your storage

There are two ways to point your gateway at your copy.

As an additional mirror. Serve the bucket over HTTP (for example with Amazon CloudFront or a public R2 bucket) and add it to the list of CDN endpoints. Hive Router appends /supergraph to every endpoint and tries them in order, so with the layout above it works like the Hive CDN mirrors:

router.config.yaml
supergraph:
  source: hive
  endpoint:
    - https://cdn.graphql-hive.com/artifacts/v1/TARGET_ID
    - https://cdn-mirror.graphql-hive.com/artifacts/v1/TARGET_ID
    - https://supergraph.example.com/artifacts/v1/TARGET_ID # your own mirror
  key: hvABCD

To make the router use your own infrastructure first, and the Hive CDN only as a fallback, list your endpoint first. To never contact the Hive CDN, make it the only endpoint.

As the only source. Hive Router can also read the supergraph directly from S3-compatible storage with the storage source, without exposing the bucket over HTTP:

router.config.yaml
storages:
  artifacts:
    type: s3
    bucket: my-supergraph-bucket
    region: eu-west-1

supergraph:
  source: storage
  storage_id: artifacts
  location: artifacts/v1/TARGET_ID/supergraph
  poll_interval: 10s

Other gateways can load the supergraph from your storage as well, either through the same HTTP endpoint or with their own file or object storage loaders.