-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgateway.js
64 lines (54 loc) · 1.66 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
62
63
64
const { ApolloServer } = require('apollo-server')
const { ApolloGateway } = require('@apollo/gateway')
const { post } = require('httpie')
const { parse } = require('graphql')
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,
typeDefs: parse(svc.typeDefs),
}
})
}
async function startServer() {
const gateway = new ApolloGateway({
// fetch for schema updates every 30s
experimental_pollInterval: 30000,
async experimental_updateServiceDefinitions() {
return {
isNewSchema: true,
serviceDefinitions: await fetchServices(),
}
},
// Experimental: Enabling this enables the query plan view in Playground.
__exposeQueryPlanExperimental: false,
})
const server = new ApolloServer({
gateway,
// Apollo Graph Manager (previously known as Apollo Engine)
// When enabled and an `ENGINE_API_KEY` is set in the environment,
// provides metrics, schema management and trace reporting.
engine: false,
// Subscriptions are unsupported but planned for a future Gateway version.
subscriptions: false,
})
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})
}
startServer().catch((err) => {
console.error(err)
process.exit(1)
})