-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issues in get-schema script and added --json flag
- Loading branch information
Showing
3 changed files
with
6,592 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,74 @@ | ||
var fetch = require("node-fetch") | ||
var fs = require("fs") | ||
var fetch = require('node-fetch') | ||
var fs = require('fs') | ||
|
||
const { | ||
buildClientSchema, | ||
introspectionQuery, | ||
printSchema | ||
} = require("graphql/utilities") | ||
const chalk = require("chalk") | ||
} = require('graphql/utilities') | ||
const chalk = require('chalk') | ||
|
||
function isURL(str) { | ||
var urlRegex = | ||
"^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$" | ||
var url = new RegExp(urlRegex, "i") | ||
'^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$' | ||
var url = new RegExp(urlRegex, 'i') | ||
return str.length < 2083 && url.test(str) | ||
} | ||
|
||
const url = process.argv[2] | ||
let url = process.argv[3] || process.argv[2] | ||
|
||
const saveJson = process.argv[3] === 'json' | ||
console.log(url) | ||
var argv = require('minimist')(process.argv.slice(2), { boolean: true }) | ||
|
||
argv._.forEach(command => { | ||
if (isURL(command)) url = command | ||
}) | ||
|
||
if (url === 'get-schema') url = null | ||
|
||
const saveJson = argv.json === true | ||
|
||
if (!url) { | ||
console.log("Usage: get-schema " + chalk.green("url")) | ||
console.log(" " + chalk.green("url") + " is your graphql server address") | ||
console.log( | ||
'Usage: get-schema ' + chalk.yellow('[--json] ') + chalk.green('url') | ||
) | ||
console.log(' ' + chalk.green('url') + ' is your graphql server address') | ||
console.log( | ||
' ' + | ||
chalk.yellow('--json') + | ||
' is an optional flag that will download your schema in json' | ||
) | ||
process.exit() | ||
} | ||
|
||
if (!isURL(url)) { | ||
console.log(chalk.red(url) + " is not a valid url") | ||
console.log(chalk.red(url) + ' is not a valid url') | ||
process.exit(1) | ||
} | ||
|
||
console.log("Downloading for url: " + chalk.green(url)) | ||
console.log('Downloading for url: ' + chalk.green(url)) | ||
|
||
fetch(url, { | ||
method: "POST", | ||
method: 'POST', | ||
headers: { | ||
Accept: "application/json", | ||
"Content-Type": "application/json" | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ query: introspectionQuery }) | ||
}) | ||
.then(res => res.json()) | ||
.then(res => { | ||
console.log(res) | ||
console.log("schema.graphql has downloaded and saved") | ||
if(saveJson) { | ||
const jsonString = JSON.stringify(res.data) | ||
console.log("schema.json has been saved") | ||
fs.writeFileSync("schema.json", jsonString) | ||
console.log('schema.graphql has downloaded and saved') | ||
if (saveJson) { | ||
const jsonString = JSON.stringify(res.data) | ||
console.log('schema.json has been saved') | ||
fs.writeFileSync('schema.json', jsonString) | ||
} | ||
const schemaString = printSchema(buildClientSchema(res.data)) | ||
fs.writeFileSync("schema.graphql", schemaString) | ||
fs.writeFileSync('schema.graphql', schemaString) | ||
}) | ||
.catch(e => { | ||
console.log(chalk.red("\nError:")) | ||
console.log(chalk.red('\nError:')) | ||
console.error(e) | ||
process.exit(1) | ||
}) |
Oops, something went wrong.