-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
63 lines (49 loc) · 1.45 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
const fs = require('fs');
const gql = require('graphql-tag');
const GraphQLTools = require('graphql-tools');
const HttpLink = require('apollo-link-http').HttpLink;
const fetch = require('node-fetch');
module.exports = function (args) {
const schemas = [];
this.linkSchema = `
type Query {
_: Boolean
}
type Mutation {
_: Boolean
}
type Subscription {
_: Boolean
}
`;
this.addScalar = function (scalar) {
this.linkSchema = this.linkSchema.replace(/^/, ` scalar ${scalar}\n`);
};
this.loadRemoteSchema = async function (uri, moreOpts) {
let opts = { uri, fetch };
const link = new HttpLink({ ...opts, ...moreOpts, });
const schema = GraphQLTools.makeRemoteExecutableSchema({
schema: await GraphQLTools.introspectSchema(link),
link
});
schemas.push(schema);
return schema;
};
this.loadLocalSchema = function (dir) {
const typeDefs = [gql(this.linkSchema)];
const resolvers = [];
fs.readdirSync(dir).forEach((file) => {
if (file !== 'index.js') {
const GraphQLSchema = new (require(`${dir}/${file}`))(args);
typeDefs.push(GraphQLSchema.getSchema());
resolvers.push(GraphQLSchema.getResolver());
}
});
const schema = GraphQLTools.makeExecutableSchema({ typeDefs, resolvers, });
schemas.push(schema);
return schema;
};
this.stitch = function () {
return GraphQLTools.mergeSchemas({ schemas });
};
};