Comparing version 0.0.3 to 0.0.4
@@ -12,3 +12,3 @@ #! /usr/bin/env node | ||
'', | ||
' Given a file with a GraphQL schema as text, generates the introspected schema as JSON file and / or the schema AST as JSON file.', | ||
' Given a file with a GraphQL schema in the schema language or the URL of a GraphQL endpoint, generates the introspected schema and / or the schema AST as JSON files.', | ||
' By default, only the introspected schema is generated.', | ||
@@ -15,0 +15,0 @@ ' To select which files to generate use the "-i" and "-o" options.' |
40
index.js
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 (schemaTextFileName, outputFileName) { | ||
var schemaText = fs.readFileSync(schemaTextFileName, 'utf-8'); | ||
exports.generateSchemaAst = function (schemaResource, outputFileName) { | ||
if (isUrl(schemaResource)) { | ||
throw new Error('URLs not supported yet for schema AST generation.'); | ||
} | ||
createWriter(outputFileName)(graphql.parse(schemaText)); | ||
createWriter(outputFileName)(graphql.parse(read(schemaResource))); | ||
}; | ||
exports.generateIntrospectedSchema = function (schemaTextFileName, outputFileName) { | ||
var schemaText = fs.readFileSync(schemaTextFileName, 'utf-8'); | ||
exports.generateIntrospectedSchema = function (schemaResource, outputFileName) { | ||
var promise = isUrl(schemaResource) ? | ||
fetchIntrospectionSchema(schemaResource) : | ||
mockServer(read(schemaResource)).query(graphql.introspectionQuery); | ||
mockServer(schemaText).query(graphql.introspectionQuery).then(createWriter(outputFileName)); | ||
promise.then(createWriter(outputFileName)); | ||
}; | ||
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(toJson); | ||
function toJson(response) { | ||
return response.json(); | ||
} | ||
} | ||
function read(fileName) { | ||
return fs.readFileSync(fileName, 'utf-8'); | ||
} | ||
function createWriter(fileName) { | ||
return content => fs.writeFileSync(fileName, JSON.stringify(content, null, 2)); | ||
return function (content) { | ||
return fs.writeFileSync(fileName, JSON.stringify(content, null, 2)); | ||
} | ||
} |
{ | ||
"name": "gql-tools", | ||
"description": "GraphQL Tools for schema handling.", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"author": "Alberto Mijares <almilo@almilo.es>", | ||
@@ -12,3 +12,5 @@ "license": "MIT", | ||
"commander": "^2.9.0", | ||
"graphql-tools": "^0.6.0" | ||
"graphql-tools": "^0.6.0", | ||
"is-url": "^1.2.2", | ||
"node-fetch": "^1.5.3" | ||
}, | ||
@@ -15,0 +17,0 @@ "peerDependencies": { |
@@ -25,3 +25,4 @@ # gql-tools | ||
### Usage | ||
* **gqlschema**: generates the introspected schema and the schema AST of a given GraphQL schema language. | ||
* **usage**: *gqlschema <schema.txt>* | ||
* **gqlschema**: generates the introspected schema and the schema AST of a given GraphQL schema either in the schema | ||
language (file) or from a GraphQL endpoint (URL). | ||
* **usage**: *gqlschema <schema.txt | http://example.com/graphql>* |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4221
65
28
5
1
+ Addedis-url@^1.2.2
+ Addednode-fetch@^1.5.3
+ Addedencoding@0.1.13(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedis-url@1.2.4(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedsafer-buffer@2.1.2(transitive)