diff --git a/src/index.js b/src/index.js index e46172f3..cf20a92c 100755 --- a/src/index.js +++ b/src/index.js @@ -39,6 +39,8 @@ function printHelp(console) { create (if the file does not exist) --require If importing the schema from a module, require the specified module first (useful for e.g. babel-register) + --header Additional header(s) to use in GraphQL request + e.g. --header "Authorization=Bearer ey..." --version Print version and exit `) } @@ -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, diff --git a/src/loadSchemaJSON.js b/src/loadSchemaJSON.js index 6425c490..80c3bbc4 100644 --- a/src/loadSchemaJSON.js +++ b/src/loadSchemaJSON.js @@ -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 }) }) @@ -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) } diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index e5022502..0cee8c52 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -477,6 +477,8 @@ Object { create (if the file does not exist) --require If importing the schema from a module, require the specified module first (useful for e.g. babel-register) + --header Additional header(s) to use in GraphQL request + e.g. --header \\"Authorization=Bearer ey...\\" --version Print version and exit ", @@ -510,6 +512,8 @@ Object { create (if the file does not exist) --require If importing the schema from a module, require the specified module first (useful for e.g. babel-register) + --header Additional header(s) to use in GraphQL request + e.g. --header \\"Authorization=Bearer ey...\\" --version Print version and exit ", diff --git a/test/index.test.js b/test/index.test.js index 2f313739..585bee06 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,6 +1,7 @@ const fs = require('fs') const path = require('path') const tempy = require('tempy') +const resolveFrom = require('resolve-from') const { run, loadSchemaJSON, @@ -8,6 +9,9 @@ const { updateSchema } = require('../src/index') +jest.mock('node-fetch') +const fetch = require('node-fetch') + function createPrinter() { const printer = chunk => { printer.output += `${chunk}\n` @@ -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()', () => {