-
Notifications
You must be signed in to change notification settings - Fork 62
/
gateway.js
58 lines (49 loc) · 1.82 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
// Open Telemetry (optional)
const { ApolloOpenTelemetry } = require('supergraph-demo-opentelemetry');
if (process.env.APOLLO_OTEL_EXPORTER_TYPE) {
new ApolloOpenTelemetry({
type: 'router',
name: 'gateway',
exporter: {
type: process.env.APOLLO_OTEL_EXPORTER_TYPE, // console, zipkin, collector
host: process.env.APOLLO_OTEL_EXPORTER_HOST,
port: process.env.APOLLO_OTEL_EXPORTER_PORT,
}
}).setupInstrumentation();
}
// Main
const { ApolloServer } = require('@apollo/server');
const { ApolloServerPluginUsageReporting } = require('@apollo/server/plugin/usageReporting');
const { startStandaloneServer } = require('@apollo/server/standalone')
const { ApolloGateway } = require('@apollo/gateway');
const { readFileSync } = require('fs');
const port = process.env.APOLLO_PORT || 4000;
const embeddedSchema = process.env.APOLLO_SCHEMA_CONFIG_EMBEDDED == "true" ? true : false;
const config = {};
const plugins = [];
if (embeddedSchema){
const supergraph = "/etc/config/supergraph.graphql"
config['supergraphSdl'] = readFileSync(supergraph).toString();
console.log('Starting Apollo Gateway in local mode ...');
console.log(`Using local: ${supergraph}`)
} else {
console.log('Starting Apollo Gateway in managed mode ...');
plugins.push(
ApolloServerPluginUsageReporting({
fieldLevelInstrumentation: 0.01
}));
}
const gateway = new ApolloGateway(config);
async function startApolloServer() {
const server = new ApolloServer({ gateway,
debug: true,
// Subscriptions are unsupported but planned for a future Gateway version.
subscriptions: false,
plugins});
const { url } = await startStandaloneServer(server, {
context: async ({ req }) => ({ token: req.headers.token }),
listen: { port: 4000 },
});
console.log(`🚀 Server ready at ${url}`);
}
startApolloServer();