@@ -7,14 +7,17 @@ import { printSchema } from 'graphql/utilities/schemaPrinter'
77import * as minimist from 'minimist'
88import * as chalk from 'chalk'
99
10- const { version} = require ( '../package.json' )
10+ const { version } = require ( '../package.json' )
1111
1212const usage = ` Usage: get-graphql-schema ENDPOINT_URL > schema.graphql
1313
14- ${ chalk . bold ( 'Fetch and print the GraphQL schema from a GraphQL HTTP endpoint' ) }
14+ ${ chalk . bold (
15+ 'Fetch and print the GraphQL schema from a GraphQL HTTP endpoint' ,
16+ ) }
1517 (Outputs schema in IDL syntax by default)
1618
1719 Options:
20+ --header, -h Add a custom header (ex. 'X-API-KEY=ABC123'), can be used multiple times
1821 --json, -j Output in JSON format (based on introspection query)
1922 --version, -v Print version of get-graphql-schema
2023`
@@ -34,11 +37,21 @@ async function main() {
3437
3538 const endpoint = argv . _ [ 0 ]
3639
40+ const defaultHeaders = {
41+ 'Content-Type' : 'application/json' ,
42+ }
43+
44+ const headers = toArray ( argv [ 'header' ] )
45+ . concat ( toArray ( argv [ 'h' ] ) )
46+ . reduce ( ( obj , header : string ) => {
47+ const [ key , value ] = header . split ( '=' )
48+ obj [ key ] = value
49+ return obj
50+ } , defaultHeaders )
51+
3752 const response = await fetch ( endpoint , {
3853 method : 'POST' ,
39- headers : {
40- 'Content-Type' : 'application/json' ,
41- } ,
54+ headers : headers ,
4255 body : JSON . stringify ( { query : introspectionQuery } ) ,
4356 } )
4457
@@ -54,7 +67,10 @@ async function main() {
5467 const schema = buildClientSchema ( data )
5568 console . log ( printSchema ( schema ) )
5669 }
70+ }
5771
72+ function toArray ( value = [ ] ) {
73+ return Array . isArray ( value ) ? value : [ value ]
5874}
5975
6076main ( ) . catch ( e => console . error ( e ) )
0 commit comments