-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
54 lines (49 loc) · 1.78 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
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { stitchSchemas } = require('@graphql-tools/stitch');
const { stitchingDirectives } = require('@graphql-tools/stitching-directives');
const { stitchingDirectivesTransformer } = stitchingDirectives();
const { buildSchema, print } = require('graphql');
const { fetch } = require('cross-fetch');
function makeRemoteExecutor(url) {
return async ({ document, variables }) => {
const query = typeof document === 'string' ? document : print(document);
const fetchResult = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables }),
});
return fetchResult.json();
};
}
async function fetchRemoteSchema(executor) {
const { data } = await executor({ document: '{ _sdl }' });
return buildSchema(data._sdl);
}
async function makeGatewaySchema() {
const accountsExec = makeRemoteExecutor('http://localhost:4001/graphql');
const productsExec = makeRemoteExecutor('http://localhost:4002/graphql');
const reviewsExec = makeRemoteExecutor('http://localhost:4003/graphql');
return stitchSchemas({
subschemaConfigTransforms: [stitchingDirectivesTransformer],
subschemas: [
{
schema: await fetchRemoteSchema(accountsExec),
executor: accountsExec,
},
{
schema: await fetchRemoteSchema(productsExec),
executor: productsExec,
},
{
schema: await fetchRemoteSchema(reviewsExec),
executor: reviewsExec,
}
]
});
}
makeGatewaySchema().then(schema => {
const app = express();
app.use('/graphql', graphqlHTTP({ schema, graphiql: true }));
app.listen(4000, () => console.log('gateway running at http://localhost:4000/graphql'));
});