-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
68 lines (59 loc) · 2.17 KB
/
index.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
const waitOn = require("wait-on");
const express = require("express");
const { introspectSchema } = require("@graphql-tools/wrap");
const { stitchSchemas } = require("@graphql-tools/stitch");
const { GraphQLUpload: GatewayGraphQLUpload } = require("@graphql-tools/links");
const { graphqlUploadExpress } = require("graphql-upload");
const { ApolloServer } = require("apollo-server-express");
const makeRemoteExecutor = require("./lib/make_remote_executor");
const localSchema = require("./services/local/schema");
async function makeGatewaySchema() {
// Make remote executors:
// these are simple functions that query a remote GraphQL API for JSON.
const productsExec = makeRemoteExecutor("http://localhost:4001/graphql");
const adminContext = { authHeader: "Bearer my-app-to-app-token" };
return stitchSchemas({
subschemas: [
{
// 1. Introspect a remote schema. Simple, but there are caveats:
// - Remote server must enable introspection.
// - Custom directives are not included in introspection.
schema: await introspectSchema(productsExec, adminContext),
executor: productsExec,
},
{
// 4. Incorporate a locally-executable subschema.
// No need for a remote executor!
// Note that that the gateway still proxies through
// to this same underlying executable schema instance.
schema: localSchema,
},
],
resolvers: {
Upload: GatewayGraphQLUpload,
},
});
}
async function startApolloServer() {
const schema = await makeGatewaySchema();
const server = new ApolloServer({
schema,
uploads: false,
});
await server.start();
const app = express();
// Additional middleware can be mounted at this point to run before Apollo.
app.use(
graphqlUploadExpress({
maxFileSize: 10000000, // 10 MB
maxFiles: 5,
})
);
// Mount Apollo middleware here.
server.applyMiddleware({ app, path: "/", cors: false });
await new Promise((resolve) => app.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
}
waitOn({ resources: ["tcp:4001"] }, async () => {
startApolloServer();
});