Skip to content

Commit

Permalink
Fixed issues in get-schema script and added --json flag
Browse files Browse the repository at this point in the history
  • Loading branch information
lpalmes committed Aug 17, 2017
1 parent c436a06 commit 59ddf2e
Show file tree
Hide file tree
Showing 3 changed files with 6,592 additions and 26 deletions.
7 changes: 4 additions & 3 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "relay-modern-scripts",
"version": "1.0.11",
"version": "1.0.12",
"description": "Configuration and scripts for Relay React app.",
"repository" : "lpalmes/relay-modern-scripts",
"repository": "lpalmes/relay-modern-scripts",
"license": "BSD-3-Clause",
"engines": {
"node": ">=6"
Expand All @@ -25,8 +25,8 @@
"babel-core": "6.25.0",
"babel-eslint": "7.2.3",
"babel-jest": "20.0.3",
"babel-plugin-relay": "^1.0.1",
"babel-loader": "7.1.1",
"babel-plugin-relay": "^1.2.0",
"babel-preset-react-app": "^3.0.2",
"babel-runtime": "6.23.0",
"case-sensitive-paths-webpack-plugin": "2.1.1",
Expand All @@ -47,6 +47,7 @@
"graphql": "^0.9.6",
"html-webpack-plugin": "2.29.0",
"jest": "20.0.4",
"minimist": "^1.2.0",
"object-assign": "4.1.1",
"postcss-flexbugs-fixes": "3.2.0",
"postcss-loader": "2.0.6",
Expand Down
61 changes: 38 additions & 23 deletions packages/react-scripts/scripts/get-schema.js
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)
})
Loading

0 comments on commit 59ddf2e

Please sign in to comment.