-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
62 lines (59 loc) · 1.75 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
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { stitchSchemas } = require('@graphql-tools/stitch');
const productsSchema = require('./services/products/schema');
const reviewsSchema = require('./services/reviews/schema');
const usersSchema = require('./services/users/schema');
function makeGatewaySchema() {
// For simplicity, all services run locally in this example.
// Any of these services could easily be turned into a remote server (see Example 1).
return stitchSchemas({
subschemas: [
{
schema: productsSchema,
batch: true,
merge: {
Product: {
selectionSet: '{ upc }',
fieldName: 'products',
key: ({ upc }) => upc,
argsFromKeys: (upcs) => ({ upcs }),
},
},
},
{
schema: reviewsSchema,
batch: true,
merge: {
Product: {
selectionSet: '{ upc }',
fieldName: '_products',
key: ({ upc }) => upc,
argsFromKeys: (upcs) => ({ upcs }),
},
User: {
selectionSet: '{ id }',
fieldName: '_users',
key: ({ id }) => id,
argsFromKeys: (ids) => ({ ids }),
},
},
},
{
schema: usersSchema,
batch: true,
merge: {
User: {
selectionSet: '{ id }',
fieldName: 'users',
key: ({ id }) => id,
argsFromKeys: (ids) => ({ ids }),
},
},
},
]
});
}
const app = express();
app.use('/graphql', graphqlHTTP({ schema: makeGatewaySchema(), graphiql: true }));
app.listen(4000, () => console.log('gateway running at http://localhost:4000/graphql'));