-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
82 lines (69 loc) · 2.42 KB
/
index.ts
File metadata and controls
82 lines (69 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* index.ts — Users subgraph entry point.
*
* Apollo Server v5 + Express 5 + Federation.
* Query/Mutation only — no subscriptions, no Kafka.
*
* Also contributes the `user` field to the Notification entity via @requires,
* allowing the Router to stitch user data onto subscription payloads from
* the notifications subgraph.
*
* Port: 4002 (matches supergraph-config.yaml routing URL)
*/
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@as-integrations/express5";
import { buildSubgraphSchema } from "@apollo/subgraph";
import gql from "graphql-tag";
import express from "express";
import cors from "cors";
import { resolvers } from "./resolvers.js";
// ── Load schema from .graphql file ──
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const schemaSource = readFileSync(join(__dirname, "schema.graphql"), "utf-8");
const typeDefs = (gql as unknown as typeof gql.default)(schemaSource);
// ── Build federated subgraph schema ──
const schema = buildSubgraphSchema({ typeDefs, resolvers });
// ── Create Apollo Server ──
const server = new ApolloServer({
schema,
introspection: true,
});
// ── Bootstrap ──
async function main() {
const PORT = parseInt(process.env.PORT ?? "4002", 10);
await server.start();
const app = express();
// Health check endpoint (for Docker/K8s probes)
app.get("/health", (_req, res) => {
res.json({ status: "ok" });
});
// Mount Apollo Server as Express middleware
app.use(
"/graphql",
cors<cors.CorsRequest>(),
express.json(),
expressMiddleware(server)
);
const httpServer = app.listen(PORT, () => {
console.log(`🚀 Users subgraph ready at http://localhost:${PORT}/graphql`);
console.log(`💚 Health check at http://localhost:${PORT}/health`);
});
// ── Graceful shutdown ──
const shutdown = async (signal: string) => {
console.log(`\n${signal} received — shutting down gracefully...`);
httpServer.close();
await server.stop();
console.log("👋 Users subgraph stopped.");
process.exit(0);
};
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("SIGINT", () => shutdown("SIGINT"));
}
main().catch((err) => {
console.error("❌ Failed to start users subgraph:", err);
process.exit(1);
});