-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
74 lines (68 loc) · 2.3 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
69
70
71
72
73
74
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
const { stitchSchemas } = require('@graphql-tools/stitch');
const { stitchingDirectives } = require('@graphql-tools/stitching-directives');
const { stitchingDirectivesTransformer } = stitchingDirectives();
const SchemaRegistry = require('./lib/schema_registry');
const makeRemoteExecutor = require('./lib/make_remote_executor');
const makeRegistrySchema = require('./services/registry/schema');
const ENV = process.env.NODE_ENV || 'development';
let repo;
try {
repo = require('./repo.json');
} catch (err) {
throw 'Make a local "repo.json" file based on "repo.template.json"';
}
const registry = new SchemaRegistry({
env: ENV,
repo,
endpoints: [
{
name: 'inventory',
url: {
development: 'http://localhost:4001/graphql',
production: 'http://localhost:4001/graphql?env=production',
}
},
{
name: 'products',
url: {
development: 'http://localhost:4002/graphql',
production: 'http://localhost:4002/graphql?env=production',
}
}
],
buildSchema: async (services) => {
const subschemas = services.map(({ url, sdl }) => ({
schema: buildSchema(sdl),
executor: makeRemoteExecutor(url, { timeout: 5000 }),
batch: true,
}));
if (ENV === 'development') {
subschemas.push({ schema: makeRegistrySchema(registry) });
}
return stitchSchemas({
subschemaConfigTransforms: [stitchingDirectivesTransformer],
subschemas,
// Includes a "reload" mutation directly in the gateway proxy layer...
// allows a reload to be triggered manually rather than just by polling
// (filter this mutation out of public APIs!)
typeDefs: 'type Mutation { _reloadGateway: Boolean! }',
resolvers: {
Mutation: {
_reloadGateway: async () => !!await registry.reload()
}
}
});
}
});
registry.reload().then(() => {
const port = ENV === 'development' ? 4000 : 4444;
const app = express();
app.use('/graphql', graphqlHTTP(() => ({ schema: registry.schema, graphiql: true })));
app.listen(port, () => console.log(`${ENV} gateway running http://localhost:${port}/graphql`));
if (ENV === 'production') {
registry.autoRefresh();
}
});