Comparing version 0.0.8 to 0.0.9
68
index.js
@@ -8,7 +8,7 @@ var fs = require('fs'); | ||
exports.generateSchemaAst = function (schemaResource, outputFileName) { | ||
if (isUrl(schemaResource)) { | ||
throw new Error('URLs not supported yet for schema AST generation.'); | ||
} | ||
var promise = isUrl(schemaResource) ? | ||
fetchIntrospectionSchema(schemaResource).then(fromIntrospectionToSchemaAst) : | ||
Promise.resolve(graphql.parse(read(schemaResource))); | ||
createWriter(outputFileName)(graphql.parse(read(schemaResource))); | ||
writeToFile(promise, 'schema AST', outputFileName); | ||
}; | ||
@@ -21,22 +21,39 @@ | ||
promise | ||
.then(createWriter(outputFileName)) | ||
.catch(throwError); | ||
writeToFile(promise, 'introspection schema', outputFileName); | ||
}; | ||
exports.generateSchemaLanguage = function (schemaResource, outputFileName) { | ||
if (!isUrl(schemaResource)) { | ||
throw new Error('Only URLs are supported for schema language generation.'); | ||
} | ||
var promise = isUrl(schemaResource) ? | ||
fetchIntrospectionSchema(schemaResource).then(fromIntrospectionToSchemaLanguage) : | ||
Promise.resolve(read(schemaResource)); | ||
fetchIntrospectionSchema(schemaResource) | ||
.then(toSchemaLanguage) | ||
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 toSchemaLanguage(introspectionQueryResponse) { | ||
return graphql.printSchema(graphql.buildClientSchema(introspectionQueryResponse.data)); | ||
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) { | ||
@@ -50,6 +67,6 @@ var headers = { | ||
return fetch(endpointUrl, {method: 'POST', body: body, headers: headers}) | ||
.then(checkStatus) | ||
.then(checkHttpStatus) | ||
.then(toJson); | ||
function checkStatus(response) { | ||
function checkHttpStatus(response) { | ||
if (response.status !== 200) { | ||
@@ -67,17 +84,12 @@ return Promise.reject('HTTP error: ' + response.status + ', ' + response.statusText); | ||
function read(fileName) { | ||
return fs.readFileSync(fileName, 'utf-8'); | ||
function fromIntrospectionToSchemaLanguage(introspection) { | ||
return graphql.printSchema(graphql.buildClientSchema(introspection.data)); | ||
} | ||
function createWriter(fileName) { | ||
return function (content) { | ||
var stringContent = typeof content !== 'string' ? JSON.stringify(content, null, 2) : content; | ||
return fs.writeFileSync(fileName, stringContent); | ||
} | ||
function fromIntrospectionToSchemaAst(introspection) { | ||
return graphql.parse(fromIntrospectionToSchemaLanguage(introspection)); | ||
} | ||
function throwError(error) { | ||
console.error(error); | ||
process.exit(1); | ||
function read(fileName) { | ||
return fs.readFileSync(fileName, 'utf-8'); | ||
} |
{ | ||
"name": "gql-tools", | ||
"description": "GraphQL Tools for schema handling.", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"author": "Alberto Mijares <almilo@almilo.es>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
10687
178