-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgateway.js
61 lines (52 loc) · 1.4 KB
/
gateway.js
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
const Fastify = require('fastify')
const mercurius = require('mercurius')
const { post } = require('httpie')
async function fetchServices() {
const res = await post(`http://localhost:3000/schema/compose`, {
body: {
graphName: 'my_graph',
services: [
{ name: 'accounts', version: 'current' },
{ name: 'inventory', version: 'current' },
{ name: 'products', version: 'current' },
{ name: 'reviews', version: 'current' },
],
},
})
return res.data.data.map((svc) => {
return {
name: svc.serviceName,
url: svc.routingUrl,
schema: svc.typeDefs,
mandatory: true,
}
})
}
async function startServer(services) {
const server = Fastify()
server.register(mercurius, {
graphiql: 'playground',
federationMetadata: true,
gateway: {
services: await fetchServices(),
},
})
server.listen(3002, (err, address) => {
if (err) throw err
console.log(`Server is now listening on ${address}/playground`)
})
setTimeout(async () => {
const services = await fetchServices()
for (const svc of services) {
server.graphql.gateway.serviceMap[svc.name].setSchema(svc.schema)
}
const schema = await server.graphql.gateway.refresh()
if (schema !== null) {
server.graphql.replaceSchema(schema)
}
}, 30000)
}
startServer().catch((err) => {
console.error(err)
process.exit(1)
})