-
Notifications
You must be signed in to change notification settings - Fork 3
/
app-manual-ssl.js
93 lines (76 loc) · 2.6 KB
/
app-manual-ssl.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const {
ApolloServer,
introspectSchema,
makeRemoteExecutableSchema,
transformSchema,
FilterRootFields,
} = require("apollo-server-express");
const { HttpLink } = require("apollo-link-http");
const fetch = require("node-fetch");
const express = require("express");
const https = require("https");
const fs = require("fs");
const httpProxy = require("http-proxy");
const { hiddenFields } = require("./config");
const MINA_GRAPHQL_HOST = process.env["MINA_GRAPHQL_HOST"] || "localhost";
const MINA_GRAPHQL_PORT = process.env["MINA_GRAPHQL_PORT"] || 3085;
const MINA_GRAPHQL_PATH = process.env["MINA_GRAPHQL_PATH"] || "/graphql";
async function getRemoteSchema({ uri }) {
const link = new HttpLink({ uri, fetch });
const schema = await introspectSchema(link);
const executableSchema = makeRemoteExecutableSchema({ schema, link });
return executableSchema;
}
function wrapSchema(originalSchema) {
const transformers = [
new FilterRootFields((operation, fieldName, field) => !field.isDeprecated),
new FilterRootFields(
(operation, fieldName, field) => hiddenFields.indexOf(fieldName) < 0
),
];
return transformSchema(originalSchema, transformers);
}
async function main() {
const graphqlUri = `${MINA_GRAPHQL_HOST}:${MINA_GRAPHQL_PORT}${MINA_GRAPHQL_PATH}`;
const remoteSchema = await getRemoteSchema({
uri: `http://${graphqlUri}`,
// subscriptionsUri: `ws://${graphqlUri}`,
});
const schema = wrapSchema(remoteSchema);
const app = express();
app.get("/", (req, res) => {
res.status(301).redirect("/graphql");
});
const server = new ApolloServer({
schema,
playground: true,
tracing: true,
introspection: true,
});
server.applyMiddleware({ app });
// we need the raw https server
const httpsServer = https.createServer(
{
key: fs.readFileSync("./ssl/private.key"),
cert: fs.readFileSync("./ssl/certificate.crt"),
ca: fs.readFileSync("./ssl/ca_bundle.crt"),
},
app
);
// server.installSubscriptionHandlers(httpsServer);
// Set up proxy server for websocket
const proxy = httpProxy.createProxyServer({
target: { host: MINA_GRAPHQL_HOST, port: MINA_GRAPHQL_PORT },
ws: true,
});
proxy.on("error", (err) => console.log("Error in proxy server:", err));
// Proxy websocket upgrades
httpsServer.on("upgrade", (req, socket, head) => proxy.ws(req, socket, head));
httpsServer.listen(443, () => {
console.log(`🚀 Server ready at https://localhost${server.graphqlPath}`);
console.log(
`🚀 Subscriptions ready at wss://localhost${server.subscriptionsPath}`
);
});
}
main().catch(console.log);