Skip to content

Commit 6894131

Browse files
authored
Merge pull request #10 from davemcorwin/master
add support for custom headers
2 parents 1a64dfc + ebcc404 commit 6894131

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ npm install -g get-graphql-schema
1111
## Usage
1212

1313
```sh
14-
Usage: get-graphql-schema ENDPOINT_URL > schema.graphql
14+
Usage: get-graphql-schema [OPTIONS] ENDPOINT_URL > schema.graphql
1515

1616
Fetch and print the GraphQL schema from a GraphQL HTTP endpoint
1717
(Outputs schema in IDL syntax by default)
1818

1919
Options:
20+
--header, -h Add a custom header (ex. 'X-API-KEY=ABC123'), can be used multiple times
2021
--json, -j Output in JSON format (based on introspection query)
2122
--version, -v Print version of get-graphql-schema
2223

src/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import { printSchema } from 'graphql/utilities/schemaPrinter'
77
import * as minimist from 'minimist'
88
import * as chalk from 'chalk'
99

10-
const {version} = require('../package.json')
10+
const { version } = require('../package.json')
1111

1212
const 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

6076
main().catch(e => console.error(e))

0 commit comments

Comments
 (0)