Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce header argument #40

Merged
merged 3 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ function printHelp(console) {
create (if the file does not exist)
--require <module> If importing the schema from a module, require the specified
module first (useful for e.g. babel-register)
--header <name=value> Additional header(s) to use in GraphQL request
e.g. --header "Authorization=Bearer ey..."
--version Print version and exit
`)
}
Expand All @@ -63,7 +65,13 @@ function run(
}
}
const schemaPath = args._[0]
loadSchemaJSON(schemaPath).then(schema => {
const headers = [].concat(args['header'] || []).reduce((obj, header) => {
const [key, ...value] = String(header).split('=')
obj[key] = value.join('=')
return obj
}, {})
const loadOptions = { headers }
loadSchemaJSON(schemaPath, loadOptions).then(schema => {
const options = {
title: args.title,
skipTitle: false,
Expand Down
7 changes: 4 additions & 3 deletions src/loadSchemaJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function fetchSchemaJSON(url, options) {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
'Content-Type': 'application/json',
...options.headers
},
body: JSON.stringify({ query: graphql.introspectionQuery })
})
Expand Down Expand Up @@ -90,9 +91,9 @@ async function requireSchema(schemaPath) {
)
}

function loadSchemaJSON(schemaPath) {
function loadSchemaJSON(schemaPath, loadOptions) {
if (schemaPath.indexOf('://') >= 0) {
return fetchSchemaJSON(schemaPath)
return fetchSchemaJSON(schemaPath, loadOptions)
} else if (schemaPath.match(/\.g(raph)?ql$/)) {
return parseSchemaGraphQL(schemaPath).then(schemaToJSON)
}
Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ Object {
create (if the file does not exist)
--require <module> If importing the schema from a module, require the specified
module first (useful for e.g. babel-register)
--header <name=value> Additional header(s) to use in GraphQL request
e.g. --header \\"Authorization=Bearer ey...\\"
--version Print version and exit

",
Expand Down Expand Up @@ -510,6 +512,8 @@ Object {
create (if the file does not exist)
--require <module> If importing the schema from a module, require the specified
module first (useful for e.g. babel-register)
--header <name=value> Additional header(s) to use in GraphQL request
e.g. --header \\"Authorization=Bearer ey...\\"
--version Print version and exit

",
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const fs = require('fs')
const path = require('path')
const tempy = require('tempy')
const resolveFrom = require('resolve-from')
const {
run,
loadSchemaJSON,
renderSchema,
updateSchema
} = require('../src/index')

jest.mock('node-fetch')
const fetch = require('node-fetch')

function createPrinter() {
const printer = chunk => {
printer.output += `${chunk}\n`
Expand Down Expand Up @@ -84,6 +88,24 @@ describe('loadSchemaJSON()', () => {
)
expect(graphqlFileSchema.__schema.queryType.name).toBe('Query')
})

it('can call fetch with correct parameters', async () => {
fetch.mockImplementation(() =>
Promise.resolve({
json: () => resolveFrom('.', 'graphbrainz/schema.json')
})
)

await loadSchemaJSON('http://example.com', {
headers: { key1: 'value1' }
})
expect(fetch.mock.calls[0][0]).toBe('http://example.com')
expect(fetch.mock.calls[0][1].headers).toEqual({
Accept: 'application/json',
'Content-Type': 'application/json',
key1: 'value1'
})
})
})

describe('updateSchema()', () => {
Expand Down