-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
61 lines (58 loc) · 1.95 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
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { stitchSchemas } = require('@graphql-tools/stitch');
const infoSchema = require('./services/info/schema');
const inventorySchema = require('./services/inventory/schema');
const createInventoryResolver = require('./services/inventory/resolve');
const pricingSchema = require('./services/pricing/schema');
const createPricingResolver = require('./services/pricing/resolve');
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: infoSchema,
merge: {
Product: {
selectionSet: '{ id }',
fieldName: 'productsInfo',
key: ({ id }) => id,
argsFromKeys: (ids) => ({ whereIn: ids }),
valuesFromResults: (results, keys) => {
const valuesByKey = Object.create(null);
for (const val of results) valuesByKey[val.id] = val;
return keys.map(key => valuesByKey[key] || null);
},
},
},
},
{
schema: inventorySchema,
merge: {
Product: {
selectionSet: '{ id }',
key: ({ id }) => id,
resolve: createInventoryResolver({
fieldName: 'productsInventory',
argsFromKeys: (ids) => ({ ids }),
}),
},
},
},
{
schema: pricingSchema,
merge: {
Product: {
selectionSet: '{ id }',
key: ({ id }) => id,
resolve: createPricingResolver(),
},
},
},
]
});
}
const app = express();
app.use('/graphql', graphqlHTTP({ schema: makeGatewaySchema(), graphiql: true }));
app.listen(4000, () => console.log('gateway running at http://localhost:4000/graphql'));