-
Notifications
You must be signed in to change notification settings - Fork 9
/
bin.js
executable file
·81 lines (67 loc) · 1.83 KB
/
bin.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
#!/usr/bin/env node
const argv = require('minimist')(process.argv.slice(2))
const colors = require('chalk')
const fs = require('fs')
const getStdin = require('get-stdin')
const GraphQL = require('graphql')
const { graphql } = GraphQL
const util = require('util')
const schema = require('./build')
const package = require('./package.json')
const usage = `graphql-scraper v${package.version}
Usage: graphql-scraper [query-file] [--json] [--<variable-name>=value]
Reads a GraphQL query from query-file, and prints the result.
If query-file is not given, reads a query from stdin.
Options:
\t--json\tSerialize the result as parseable JSON.
Example:
> graphql-scraper query.graphql --json --url="https://news.ycombinator.com/" --page=2
`
function die() {
console.log(usage)
process.exit(1)
}
const formatters = {
json: function(result) {
console.log(JSON.stringify(result))
},
pretty: function(result) {
if (result.data) {
console.log(colors.green('Data:'))
printObject(result.data)
}
if (Array.isArray(result.errors)) {
console.log(colors.red('Errors:'))
result.errors.forEach(error => {
printObject(error)
})
console.log(colors.red('Your query has errors, see above.'))
}
function printObject(obj) {
console.log(util.inspect(obj, { depth: null, colors: true }))
}
},
}
async function main() {
if (argv.help) {
console.log(usage)
return
}
const formatter = argv.json ? 'json' : 'pretty'
delete argv.json
let query
if (argv._.length === 0) {
query = await getStdin()
if (query === '') {
die()
}
} else if (argv._.length === 1) {
query = fs.readFileSync(argv._[0], { encoding: 'utf8' })
} else {
die()
}
delete argv._
const result = await graphql(schema, query, {}, {}, argv)
formatters[formatter](result)
}
main()