@cubicecho/graphql-mdns-discovery
v1.2.0
Published
Advertise and discover GraphQL APIs: a public services document at /.well-known/graphql-services.json, serving it over HTTP, and finding hosts on a LAN as _graphql._tcp over mDNS.
Maintainers
Readme
@cubicecho/graphql-mdns-discovery
Advertise and discover GraphQL APIs.
There is no standard way for a host to say "I serve a GraphQL API, here". With this package a host lists its
APIs in a public document at /.well-known/graphql-services.json: each one's endpoint, its subscription
transport, where to download its SDL, and the headers a client must supply (never their values). On a LAN the
host also announces itself as a _graphql._tcp mDNS service, so a client finds APIs without anyone typing in
URLs.
It is the GraphQL counterpart of @cubicecho/mcp-discovery, and
both are built on @cubicecho/discovery-core. One app can use
the two together.
Install
npm install @cubicecho/graphql-mdns-discovery zod
npm install bonjour-service # only for the LAN half, ./mdnsQuick start
Publish an Express app's API and announce it on the LAN (npm install express too):
import { hostname } from 'node:os';
import { graphqlDiscoveryMiddleware, hostSlug } from '@cubicecho/graphql-mdns-discovery';
import { createGraphqlAdvertiser } from '@cubicecho/graphql-mdns-discovery/mdns';
import express from 'express';
const port = 3000;
const app = express();
// Before any auth guard: the document is public.
app.use(
graphqlDiscoveryMiddleware(() => [
{
path: '/graphql', // where your GraphQL server is mounted
service: () => ({
name: `com.example.shop.${hostSlug(hostname())}/api`,
title: 'Shop',
headers: [{ name: 'Authorization', isRequired: true, isSecret: true }],
}),
},
]),
);
const advertiser = createGraphqlAdvertiser({
isWanted: () => true,
options: () => ({ name: `Shop on ${hostname()}`, port }),
log: console.log,
});
app.listen(port, '0.0.0.0', (error) => {
if (error) {
throw error;
}
void advertiser.sync();
});
process.once('SIGTERM', async () => {
await advertiser.close();
process.exit(0);
});http://localhost:3000/.well-known/graphql-services.json now lists the API, and any machine on the network can
find it. To read it from a client, see
Discovering APIs.
Documentation
- Install: peer dependencies and entry points
- Serving the document
- Discovering APIs over HTTP
- The services document: format, caching, what to keep out
- mDNS on the LAN: advertising, watching, Docker
- Beside mcp-discovery
- Common mistakes
- llms.txt: the index of all of it, with every export
- Changelog
