-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
46 lines (41 loc) · 1.53 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
const waitOn = require('wait-on');
const { stitchSchemas } = require('@graphql-tools/stitch');
const { stitchingDirectives } = require('@graphql-tools/stitching-directives');
const { buildSchema } = require('graphql');
const makeServer = require('./lib/make_server');
const makeRemoteExecutor = require('./lib/make_remote_executor');
const { stitchingDirectivesTransformer } = stitchingDirectives();
async function makeGatewaySchema() {
const accountsExec = makeRemoteExecutor('http://localhost:4001/graphql');
const inventoryExec = makeRemoteExecutor('http://localhost:4002/graphql');
const productsExec = makeRemoteExecutor('http://localhost:4003/graphql');
const reviewsExec = makeRemoteExecutor('http://localhost:4004/graphql');
return stitchSchemas({
subschemaConfigTransforms: [stitchingDirectivesTransformer],
subschemas: [
{
schema: await fetchRemoteSchema(accountsExec),
executor: accountsExec,
},
{
schema: await fetchRemoteSchema(inventoryExec),
executor: inventoryExec,
},
{
schema: await fetchRemoteSchema(productsExec),
executor: productsExec,
},
{
schema: await fetchRemoteSchema(reviewsExec),
executor: reviewsExec,
}
]
});
}
async function fetchRemoteSchema(executor) {
const { data } = await executor({ document: '{ _sdl }' });
return buildSchema(data._sdl);
}
waitOn({ resources: [4001, 4002, 4003, 4004].map(p => `tcp:${p}`) }, async () => {
makeServer(await makeGatewaySchema(), 'gateway', 4000);
});