Skip to content

Commit

Permalink
Provide headers with sendIntrospectionQuery (#21)
Browse files Browse the repository at this point in the history
* Add ability to pass headers when introspecting GraphQL services

* Add yargs as a dependency

* Change 'request' to 'query' for consistent language

* Add additional example
  • Loading branch information
Marcel Cutts authored and mhallin committed Feb 7, 2018
1 parent 5d3511b commit 63f2655
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 23 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"postinstall": "if [ ! -e ./ppx ]; then ln -fs \"$(opam config var graphql_ppx:bin)/graphql_ppx.native\" ./ppx; fi"
},
"dependencies": {
"request": "^2.82.0"
"request": "^2.82.0",
"yargs": "^11.0.0"
}
}
67 changes: 45 additions & 22 deletions sendIntrospectionQuery.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
#!/usr/bin/env node
var argv = require("yargs")
.usage('Usage: $0 <url> [--headers "key:value"]')
.command("url", "URL of the GraphQL endpoint", { alias: "url" })
.required(1, "URL is required")
.option("H", {
alias: "headers",
describe: "Additional Headers to send with introspection query",
type: "array",
coerce: arg => {
let additionalHeaders = {};
for (const header of arg) {
const separator = header.indexOf(":");
const name = header.substring(0, separator).trim();
const value = header.substring(separator + 1).trim();
if (!(name && value)) {
throw new Error('Headers should be specified as "Name: Value"');
}
additionalHeaders[name] = value;
}
return additionalHeaders;
}
})
.help("?")
.alias("?", "help")
.example("$0 https://example.com/graphql", "Get GraphQL Schema")
.example(`$0 https://example.com/graphql --headers "Authorisation: <token>"`, "Get GraphQL Schema with Authorisation header").argv;

var request = require('request');
var fs = require('fs');

var request = require("request");
var fs = require("fs");
var introspectionQuery = `
query IntrospectionQuery {
__schema {
Expand Down Expand Up @@ -93,26 +118,24 @@ query IntrospectionQuery {
}
}`;

if (process.argv.length !== 3) {
console.error(`Usage: ${process.argv[1]} <API_URL>`);
process.exit(1);
}
const requestOptions = {
json: true,
body: { query: introspectionQuery },
headers: { "user-agent": "node.js", ...argv.headers }
};

request.post(
process.argv[2],
{ method: 'POST', json: true, body: { query: introspectionQuery } },
function (error, response, body) {
if (error) {
console.error('Could not send introspection query: ', error);
process.exit(1);
}
request.post(argv._[0], requestOptions, function(error, response, body) {
if (error) {
console.error("Could not send introspection query: ", error);
process.exit(1);
}

if (response.statusCode !== 200) {
console.error('Non-ok status code from API: ', response.statusCode, response.statusMessage);
process.exit(1);
}
if (response.statusCode !== 200) {
console.error("Non-ok status code from API: ", response.statusCode, response.statusMessage);
process.exit(1);
}

var result = JSON.stringify(body, null, 2);
var result = JSON.stringify(body, null, 2);

fs.writeFileSync('graphql_schema.json', result, { encoding: 'utf-8' });
});
fs.writeFileSync("graphql_schema.json", result, { encoding: "utf-8" });
});
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3120,6 +3120,12 @@ yargs-parser@^8.1.0:
dependencies:
camelcase "^4.1.0"

yargs-parser@^9.0.2:
version "9.0.2"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-9.0.2.tgz#9ccf6a43460fe4ed40a9bb68f48d43b8a68cc077"
dependencies:
camelcase "^4.1.0"

yargs@^10.0.3:
version "10.1.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-10.1.1.tgz#5fe1ea306985a099b33492001fa19a1e61efe285"
Expand All @@ -3137,6 +3143,23 @@ yargs@^10.0.3:
y18n "^3.2.1"
yargs-parser "^8.1.0"

yargs@^11.0.0:
version "11.0.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-11.0.0.tgz#c052931006c5eee74610e5fc0354bedfd08a201b"
dependencies:
cliui "^4.0.0"
decamelize "^1.1.1"
find-up "^2.1.0"
get-caller-file "^1.0.1"
os-locale "^2.0.0"
require-directory "^2.1.1"
require-main-filename "^1.0.1"
set-blocking "^2.0.0"
string-width "^2.0.0"
which-module "^2.0.0"
y18n "^3.2.1"
yargs-parser "^9.0.2"

yargs@~3.10.0:
version "3.10.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1"
Expand Down

0 comments on commit 63f2655

Please sign in to comment.