Skip to content

Commit

Permalink
feat(graphql): add "--header/-H" CLI option to introspect command
Browse files Browse the repository at this point in the history
  • Loading branch information
ZauberNerd committed Mar 26, 2018
1 parent e819bdb commit 671193d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
8 changes: 7 additions & 1 deletion packages/graphql/__tests__/graphql.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
19 changes: 17 additions & 2 deletions packages/graphql/commands/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})
Expand Down
24 changes: 16 additions & 8 deletions packages/graphql/lib/fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 671193d

Please sign in to comment.