Skip to content

Commit 4c2fb46

Browse files
johannespfeifferexogen
authored andcommitted
Introduce header argument (#40)
* Introduce header argument * Add simple test for loadOptions * Allow = character in header value
1 parent 55616ac commit 4c2fb46

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ function printHelp(console) {
3939
create (if the file does not exist)
4040
--require <module> If importing the schema from a module, require the specified
4141
module first (useful for e.g. babel-register)
42+
--header <name=value> Additional header(s) to use in GraphQL request
43+
e.g. --header "Authorization=Bearer ey..."
4244
--version Print version and exit
4345
`)
4446
}
@@ -63,7 +65,13 @@ function run(
6365
}
6466
}
6567
const schemaPath = args._[0]
66-
loadSchemaJSON(schemaPath).then(schema => {
68+
const headers = [].concat(args['header'] || []).reduce((obj, header) => {
69+
const [key, ...value] = String(header).split('=')
70+
obj[key] = value.join('=')
71+
return obj
72+
}, {})
73+
const loadOptions = { headers }
74+
loadSchemaJSON(schemaPath, loadOptions).then(schema => {
6775
const options = {
6876
title: args.title,
6977
skipTitle: false,

src/loadSchemaJSON.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function fetchSchemaJSON(url, options) {
3232
method: 'POST',
3333
headers: {
3434
Accept: 'application/json',
35-
'Content-Type': 'application/json'
35+
'Content-Type': 'application/json',
36+
...options.headers
3637
},
3738
body: JSON.stringify({ query: graphql.introspectionQuery })
3839
})
@@ -90,9 +91,9 @@ async function requireSchema(schemaPath) {
9091
)
9192
}
9293

93-
function loadSchemaJSON(schemaPath) {
94+
function loadSchemaJSON(schemaPath, loadOptions) {
9495
if (schemaPath.indexOf('://') >= 0) {
95-
return fetchSchemaJSON(schemaPath)
96+
return fetchSchemaJSON(schemaPath, loadOptions)
9697
} else if (schemaPath.match(/\.g(raph)?ql$/)) {
9798
return parseSchemaGraphQL(schemaPath).then(schemaToJSON)
9899
}

test/__snapshots__/index.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ Object {
477477
create (if the file does not exist)
478478
--require <module> If importing the schema from a module, require the specified
479479
module first (useful for e.g. babel-register)
480+
--header <name=value> Additional header(s) to use in GraphQL request
481+
e.g. --header \\"Authorization=Bearer ey...\\"
480482
--version Print version and exit
481483
482484
",
@@ -510,6 +512,8 @@ Object {
510512
create (if the file does not exist)
511513
--require <module> If importing the schema from a module, require the specified
512514
module first (useful for e.g. babel-register)
515+
--header <name=value> Additional header(s) to use in GraphQL request
516+
e.g. --header \\"Authorization=Bearer ey...\\"
513517
--version Print version and exit
514518
515519
",

test/index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
const fs = require('fs')
22
const path = require('path')
33
const tempy = require('tempy')
4+
const resolveFrom = require('resolve-from')
45
const {
56
run,
67
loadSchemaJSON,
78
renderSchema,
89
updateSchema
910
} = require('../src/index')
1011

12+
jest.mock('node-fetch')
13+
const fetch = require('node-fetch')
14+
1115
function createPrinter() {
1216
const printer = chunk => {
1317
printer.output += `${chunk}\n`
@@ -84,6 +88,24 @@ describe('loadSchemaJSON()', () => {
8488
)
8589
expect(graphqlFileSchema.__schema.queryType.name).toBe('Query')
8690
})
91+
92+
it('can call fetch with correct parameters', async () => {
93+
fetch.mockImplementation(() =>
94+
Promise.resolve({
95+
json: () => resolveFrom('.', 'graphbrainz/schema.json')
96+
})
97+
)
98+
99+
await loadSchemaJSON('http://example.com', {
100+
headers: { key1: 'value1' }
101+
})
102+
expect(fetch.mock.calls[0][0]).toBe('http://example.com')
103+
expect(fetch.mock.calls[0][1].headers).toEqual({
104+
Accept: 'application/json',
105+
'Content-Type': 'application/json',
106+
key1: 'value1'
107+
})
108+
})
87109
})
88110

89111
describe('updateSchema()', () => {

0 commit comments

Comments
 (0)