-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
91 lines (72 loc) · 2.79 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var fs = require('fs');
var graphql = require('graphql');
var mockServer = require('graphql-tools').mockServer;
var isUrl = require('is-url');
var fetch = require('node-fetch');
exports.generateSchemaAst = function (schemaResource, outputFileName) {
var promise = isUrl(schemaResource) ?
fetchIntrospectionSchema(schemaResource).then(fromIntrospectionToSchemaAst) :
Promise.resolve(graphql.parse(read(schemaResource)));
writeToFile(promise, 'schema AST', outputFileName);
};
exports.generateIntrospectedSchema = function (schemaResource, outputFileName) {
var promise = isUrl(schemaResource) ?
fetchIntrospectionSchema(schemaResource) :
mockServer(read(schemaResource)).query(graphql.introspectionQuery);
writeToFile(promise, 'introspection schema', outputFileName);
};
exports.generateSchemaLanguage = function (schemaResource, outputFileName) {
var promise = isUrl(schemaResource) ?
fetchIntrospectionSchema(schemaResource).then(fromIntrospectionToSchemaLanguage) :
Promise.resolve(read(schemaResource));
writeToFile(promise, 'schema file', outputFileName);
};
function writeToFile(promise, what, outputFileName) {
promise
.then(createWriter(outputFileName))
.then(createLogger('GraphQL ' + what + ' written to file: "' + outputFileName + '".'))
.catch(throwError);
function createWriter(fileName) {
return function (content) {
var stringContent = typeof content !== 'string' ? JSON.stringify(content, null, 2) : content;
return fs.writeFileSync(fileName, stringContent);
}
}
function createLogger(message) {
return function () {
console.log(message);
}
}
function throwError(error) {
console.error(error);
process.exit(1);
}
}
function fetchIntrospectionSchema(endpointUrl) {
var headers = {
'content-type': 'application/json',
'accept': 'application/json'
};
var body = JSON.stringify({query: graphql.introspectionQuery});
return fetch(endpointUrl, {method: 'POST', body: body, headers: headers})
.then(checkHttpStatus)
.then(toJson);
function checkHttpStatus(response) {
if (response.status !== 200) {
return Promise.reject('HTTP error: ' + response.status + ', ' + response.statusText);
}
return response;
}
function toJson(response) {
return response.json();
}
}
function fromIntrospectionToSchemaLanguage(introspection) {
return graphql.printSchema(graphql.buildClientSchema(introspection.data));
}
function fromIntrospectionToSchemaAst(introspection) {
return graphql.parse(fromIntrospectionToSchemaLanguage(introspection));
}
function read(fileName) {
return fs.readFileSync(fileName, 'utf-8');
}