-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
36 lines (34 loc) · 1.19 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
const fs = require('fs');
const path = require('path');
const util = require('./util.js');
module.exports = ({input, output}, cb = () => {}) => {
// INPUT
if (!input){
throw new Error('You must provide an input path.');
}
const filepath = path.resolve(input);
const schema = require(filepath);
// Ugly hack. Since graphql uses instanceof everywhere, the schema we require is not considered a valid graphql Schema because it was not created with the graphql module installed in this module We have to retrieve the "original" graphql module
const loaded_module = module.children.find(mod => path.normalize(mod.id) === filepath);
const graphql = loaded_module.children.find(mod => mod.exports.graphql).exports;
// OUTPUT
if (output){
output = util.normalizePath(output);
}
// WRITE
return graphql
.graphql(schema, graphql.introspectionQuery)
.then(result => JSON.stringify(result, null, 2))
.then(json => {
if (output){
return new Promise((resolve, reject) => fs.writeFile(output, json, (err) => {
if (err){
return reject(err);
}
cb(json);
resolve(json);
}));
}
return json;
});
};