-
Notifications
You must be signed in to change notification settings - Fork 55
/
cli.js
executable file
·192 lines (178 loc) · 5.1 KB
/
cli.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env node
var fetch = require('cross-fetch')
var meow = require('meow')
var fs = require('fs')
var getStdin = require('get-stdin')
var GraphQL = require('graphql')
var graphqlviz = require('./')
var cli = meow(
`
Options:
-t --theme path to theme overrides
--print-theme print default theme to stdout
-v --verbose print introspection result
-a --auth set Authorization header for graphql server
Usage:
$ graphqlviz [url]
Renders dot schema from [url] endpoint
Examples:
$ graphqlviz https://localhost:3000 | dot -Tpng -o graph.png
$ graphqlviz https://example.com/graphql -a "Bearer xxxxx" | dot -Tpng -o graph.png
$ graphqlviz https://swapi.apis.guru | dot -Tpng | open -f -a Preview
$ graphqlviz path/to/schema.json | dot -Tpng | open -f -a Preview
$ graphqlviz path/to/schema.graphql | dot -Tpng | open -f -a Preview
$ graphqlviz --print-theme > theme.json
$ graphqlviz https://localhost:3000 -t theme.json | dot -Tpng | open -f -a Preview
$ graphqlviz schema.json --theme.header.invert=true | dot -Tpng > schema.png
`,
{
flags: {
verbose: {
type: 'boolean',
alias: 'v'
},
theme: {
type: 'string',
alias: 't'
},
graphql: {
type: 'boolean',
alias: 'g'
},
auth: {
type: 'string',
alias: 'a'
}
}
}
)
if (cli.flags.theme && typeof cli.flags.theme === 'string') {
cli.flags.theme = JSON.parse(fs.readFileSync(cli.flags.theme))
}
// displays help and exits
function terminate () {
console.error(cli.help)
process.exit(1)
}
// logs the error and exits
function fatal (e, text) {
console.error('ERROR processing input. Use --verbose flag to see output.')
console.error(e.message)
if (cli.flags.verbose) {
console.error(text)
console.error(e.stack)
}
process.exit(1)
}
// given a "GraphQL schema language" text file, converts into introspection JSON
function introspect (text) {
return new Promise(function (resolve, reject) {
try {
var astDocument = GraphQL.parse(text)
var schema = GraphQL.buildASTSchema(astDocument)
GraphQL.graphql({ schema, source: graphqlviz.query })
.then(function (data) {
resolve(data)
})
.catch(function (e) {
reject('Fatal error, exiting.')
fatal(e, text)
})
} catch (e) {
reject('Fatal error, exiting.')
fatal(e, text)
}
})
}
if (cli.input[0] === 'query') {
process.stdout.write(JSON.stringify({ query: graphqlviz.query }) + '\n')
} else if (cli.flags.printTheme) {
process.stdout.write(JSON.stringify(graphqlviz.theme, null, 2) + '\n')
} else {
var p
if (!process.stdin.isTTY) {
// stdin
p = getStdin().then(function (stdin) {
if (stdin.trim() === '') {
console.error('ERROR: When run without a TTY; data must be provided in stdin')
return terminate()
}
return stdin
});
} else if (cli.input.length === 1) {
if (cli.input[0].slice(0, 4) === 'http') {
// otherwise http(s)
var headers = {
Accept: 'application/json',
'Content-Type': 'application/json'
}
if (cli.flags.auth) {
headers.Authorization = cli.flags.auth
}
p = fetch(cli.input[0], {
method: 'POST',
headers: headers,
body: JSON.stringify({ query: graphqlviz.query })
}).then(function (res) {
if (!res.ok && cli.flags.verbose) {
console.log(
'Request for schema failed w/ ' +
res.status +
' (' +
res.statusText +
')'
)
}
return res.text()
})
} else {
// if not http, try local file
p = new Promise(function (resolve, reject) {
fs.readFile(cli.input[0], { encoding: 'utf8' }, function (err, data) {
if (err) {
reject(err)
fatal(err, data)
} else {
resolve(data)
}
})
})
}
} else {
terminate()
}
// after getting text, try to parse as JSON and process, or use graphql to process a "graphql schema language" file
var introspectionPromise = p.then(function (text) {
if (!text) {
return terminate()
}
var returnedPromise
const first = text.match(/[^\s]/)
if (!first) {
throw new Error('No text to parse')
}
if (first[0] === "{") {
try {
returnedPromise = Promise.resolve(JSON.parse(text))
} catch (e) {
fatal(e, text)
}
} else {
returnedPromise = introspect(text)
}
return returnedPromise
})
introspectionPromise.then(function (executionResult) {
// undocumented outputJSON can be used to convert graphql schema to JSON (useful for generating test data)
if (cli.flags.outputJSON) {
console.log(JSON.stringify(executionResult, null, 2, 2))
process.exit(1)
} else {
try {
console.log(graphqlviz.render(executionResult, cli.flags))
} catch (e) {
fatal(e, JSON.stringify(executionResult, null, 2, 2))
}
}
})
}