diff --git a/packages/graphql/__tests__/graphql.spec.js b/packages/graphql/__tests__/graphql.spec.js index b8d95ed9e..7276b9788 100644 --- a/packages/graphql/__tests__/graphql.spec.js +++ b/packages/graphql/__tests__/graphql.spec.js @@ -20,7 +20,13 @@ var mockResponse = { describe('graphql schema introspection', function() { function generateFragmentTypes() { - return require('../lib/fragments')(); + var hopsConfig = require('hops-config'); + var options = { + schemaFile: hopsConfig.graphqlSchemaFile, + graphqlUri: hopsConfig.graphqlUri, + headers: [], + }; + return require('../lib/fragments')(options); } function getFragmentsFile() { diff --git a/packages/graphql/commands/graphql.js b/packages/graphql/commands/graphql.js index 36759c6e3..3e44f0beb 100755 --- a/packages/graphql/commands/graphql.js +++ b/packages/graphql/commands/graphql.js @@ -12,9 +12,24 @@ module.exports = function defineGraphQLCommand(args) { .command({ command: 'introspect', describe: 'Fetches GraphQL schema information for introspection', - builder: {}, + builder: { + header: { + alias: 'H', + type: 'array', + default: [], + describe: + 'Additional HTTP headers to be used when executing the schema ' + + 'introspection on the remote server. Specify this multiple ' + + 'times to add more headers.\nFormat: "Header: Value"', + }, + }, handler: function graphqlHandler(argv) { - require('../lib/fragments')() + var hopsConfig = require('hops-config'); + require('../lib/fragments')({ + graphqlUri: hopsConfig.graphqlUri, + schemaFile: hopsConfig.graphqlSchemaFile, + headers: argv.header, + }) .then(function() { console.log('Fetched and saved GraphQL fragments'); }) diff --git a/packages/graphql/lib/fragments.js b/packages/graphql/lib/fragments.js index 9941887fc..b126e783b 100644 --- a/packages/graphql/lib/fragments.js +++ b/packages/graphql/lib/fragments.js @@ -7,8 +7,6 @@ var pify = require('pify'); var graphql = require('graphql').graphql; var makeExecutableSchema = require('graphql-tools').makeExecutableSchema; -var hopsConfig = require('hops-config'); - var fragmentsFile = require('./util').getFragmentsFile(); function writeFragmentTypesFile(result) { @@ -18,10 +16,20 @@ function writeFragmentTypesFile(result) { return pify(fs.writeFile)(fragmentsFile, JSON.stringify(result.data)); } -function executeRemoteQuery(graphqlUri, query) { +function executeRemoteQuery(graphqlUri, headers, query) { + var combinedHeaders = (headers || []).reduce( + function(headers, header) { + var parts = header.split(':'); + headers[parts.shift()] = parts.join(':'); + return headers; + }, + { + 'Content-Type': 'application/json', + } + ); return fetch(graphqlUri, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers: combinedHeaders, body: JSON.stringify({ query: query }), }).then(function(result) { return result.json(); @@ -52,14 +60,14 @@ var query = [ '}', ].join('\n'); -module.exports = function generateFragmentTypes() { - return pify(fs.access)(hopsConfig.graphqlSchemaFile) +module.exports = function generateFragmentTypes(options) { + return pify(fs.access)(options.schemaFile) .then( function() { - return executeLocalQuery(hopsConfig.graphqlSchemaFile, query); + return executeLocalQuery(options.schemaFile, query); }, function() { - return executeRemoteQuery(hopsConfig.graphqlUri, query); + return executeRemoteQuery(options.graphqlUri, options.headers, query); } ) .then(writeFragmentTypesFile);