Skip to content

Commit

Permalink
Merge pull request #10 from davemcorwin/master
Browse files Browse the repository at this point in the history
add support for custom headers
  • Loading branch information
schickling authored Jul 24, 2017
2 parents 1a64dfc + ebcc404 commit 6894131
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ npm install -g get-graphql-schema
## Usage

```sh
Usage: get-graphql-schema ENDPOINT_URL > schema.graphql
Usage: get-graphql-schema [OPTIONS] ENDPOINT_URL > schema.graphql

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

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

Expand Down
26 changes: 21 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { printSchema } from 'graphql/utilities/schemaPrinter'
import * as minimist from 'minimist'
import * as chalk from 'chalk'

const {version} = require('../package.json')
const { version } = require('../package.json')

const usage = ` Usage: get-graphql-schema ENDPOINT_URL > schema.graphql
${chalk.bold('Fetch and print the GraphQL schema from a GraphQL HTTP endpoint')}
${chalk.bold(
'Fetch and print the GraphQL schema from a GraphQL HTTP endpoint',
)}
(Outputs schema in IDL syntax by default)
Options:
--header, -h Add a custom header (ex. 'X-API-KEY=ABC123'), can be used multiple times
--json, -j Output in JSON format (based on introspection query)
--version, -v Print version of get-graphql-schema
`
Expand All @@ -34,11 +37,21 @@ async function main() {

const endpoint = argv._[0]

const defaultHeaders = {
'Content-Type': 'application/json',
}

const headers = toArray(argv['header'])
.concat(toArray(argv['h']))
.reduce((obj, header: string) => {
const [key, value] = header.split('=')
obj[key] = value
return obj
}, defaultHeaders)

const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
headers: headers,
body: JSON.stringify({ query: introspectionQuery }),
})

Expand All @@ -54,7 +67,10 @@ async function main() {
const schema = buildClientSchema(data)
console.log(printSchema(schema))
}
}

function toArray(value = []) {
return Array.isArray(value) ? value : [value]
}

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

0 comments on commit 6894131

Please sign in to comment.