Skip to content

Commit 0086521

Browse files
author
Bryan Cross
authored
Merge pull request #164 from github/refactor-to-node
Refactor to node, add support for variables
2 parents 854c8bd + 0c680ab commit 0086521

15 files changed

+307
-28
lines changed

graphql/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.idea
3+
package-lock.json

graphql/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This repository holds query samples for the GitHub GraphQL API. It's an easy way
55
### How to use the included script
66

77
1. Generate a [personal access token](https://help.github.com/articles/creating-an-access-token-for-command-line-use/) for use with these queries.
8-
1. Run `bundle install`.
9-
1. Pick the name of one of the included queries like `viewer.graphql`.
10-
1. Run `TOKEN=<OAuth Token> bin/run-query viewer.graphql`. Replace `<OAuth Token>` with your personal access token.
8+
1. Run `npm install`
9+
1. Pick the name of one of the included queries in the `/queries` directory, such as `viewer.graphql`.
10+
1. Run `bin/run-query <query_file> <token>`
11+
12+
To change variable values, modify the variables in the `.graphql` file.

graphql/bin/run-query

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,76 @@
1-
#!/usr/bin/env ruby
2-
3-
require "httparty"
4-
5-
query_file_name = ARGV[0]
6-
unless query_file_name
7-
print "Please provide a file name from 'queries'."
8-
exit
9-
end
10-
11-
query_file_path = File.expand_path(File.join("queries", query_file_name))
12-
query = File.read(query_file_path)
13-
14-
response = HTTParty.post("https://api.github.com/graphql",
15-
:headers => {
16-
"User-Agent" => "github/graphql-samples",
17-
"Authorization" => "token #{ENV["TOKEN"]}",
18-
"Content-Type" => "application/json"
19-
},
20-
:body => {:query => query}.to_json
21-
)
22-
23-
puts "=== Results from #{File.basename(query_file_name)}"
24-
puts JSON.pretty_generate(response)
25-
puts
1+
#!/usr/bin/env node
2+
'use strict';
3+
const fs = require('fs');
4+
const request = require('request');
5+
const queryObj = {};
6+
const program = require('commander');
7+
const variablesRegex = /variables([\s\S]*)}/gm;
8+
9+
program
10+
.version('0.0.1')
11+
.usage('<file> <token>')
12+
.arguments('<file> <token>')
13+
.action(function(file, token){
14+
//console.log("Running query: " + file);
15+
runQuery(file, token);
16+
})
17+
.description('Execute specified GraphQL query.');
18+
19+
program.on('--help', function(){
20+
console.log('');
21+
console.log(' Arguments:');
22+
console.log('');
23+
console.log(' file : Path to file containing GraphQL');
24+
console.log(' token: Properly scoped GitHub PAT');
25+
console.log('');
26+
});
27+
28+
//Commander doesn't seem to do anything when both required arguments are missing
29+
//So we'll check the old-fashioned way
30+
31+
if(process.argv.length == 2)
32+
{
33+
console.log("Usage: run-query <file> <token>");
34+
process.exitCode = 1;
35+
}
36+
else
37+
{
38+
program.parse(process.argv);
39+
}
40+
41+
42+
43+
function runQuery(file, token) {
44+
if(typeof file === undefined || typeof token === undefined)
45+
{
46+
console.log('Usage: ./index.js ' + program.usage());
47+
process.exit(1);
48+
}
49+
try {
50+
var queryText = fs.readFileSync(file, "utf8");
51+
}
52+
catch (e) {
53+
console.log("Problem opening query file: " + e.message);
54+
process.exit(1);
55+
}
56+
57+
//If there is a variables section, extract the values and add them to the query JSON object.
58+
queryObj.variables = variablesRegex.test(queryText) ? JSON.parse(queryText.match(variablesRegex)[0].split("variables ")[1]) : {}
59+
//Remove the variables section from the query text, whether it exists or not
60+
queryObj.query = queryText.replace(variablesRegex, '');
61+
62+
request({
63+
url: "https://api.github.com/graphql"
64+
, method: "POST"
65+
, headers: {
66+
'authorization': 'bearer ' + token
67+
, 'content-type': 'application/json'
68+
, 'user-agent': 'platform-samples'
69+
}
70+
, json: true
71+
, body: queryObj
72+
}, function (error, response, body) {
73+
//Make some effort to pretty-print any output
74+
console.log(JSON.stringify(body, null, 2));
75+
});
76+
};

graphql/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "graphql",
3+
"version": "1.0.0",
4+
"description": "Simple command line utility for running GraphQL queries",
5+
"main": "index.js",
6+
"bin": {
7+
"run-query": "./index.js"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"author": "[email protected]",
13+
"license": "ISC",
14+
"dependencies": {
15+
"commander": "^2.12.2"
16+
}
17+
}

graphql/queries/1-org-members.graphql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
query {
2+
organization(login:"github") {
3+
login
4+
name
5+
members(first:100) {
6+
edges {
7+
node {
8+
login
9+
location
10+
}
11+
}
12+
}
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
query getRepoIssue($orgName: String!, $repoName: String!)
2+
{
3+
repository(owner: $orgName, name: $repoName){
4+
issues(last: 1){
5+
edges{
6+
node{
7+
number
8+
id
9+
body
10+
}
11+
}
12+
}
13+
}
14+
}
15+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mutation {
2+
addComment (
3+
input: {
4+
body: "Added by GraphQL",
5+
subjectId:"<RETURNED_BY_PREVIOUS_CALL>"
6+
})
7+
8+
{
9+
clientMutationId
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
query ($orgLogin:String!) {
2+
organization(login: $orgLogin) {
3+
login
4+
name
5+
members(first:100) {
6+
edges {
7+
node {
8+
login
9+
location
10+
}
11+
}
12+
}
13+
}
14+
}
15+
16+
variables {
17+
"orgLogin": "bidnessforb"
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
organization(login: "github") {
3+
login
4+
name
5+
members(first: 100) {
6+
edges {
7+
node {
8+
login
9+
location
10+
}
11+
}
12+
edges {
13+
node {
14+
commitComments(first: 3) {
15+
edges {
16+
node {
17+
id
18+
body
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
query {
2+
organization(login: "github") {
3+
repositories {
4+
...repoFrag
5+
}
6+
}
7+
}
8+
9+
fragment repoFrag on RepositoryConnection {
10+
totalCount
11+
totalDiskUsage
12+
}

0 commit comments

Comments
 (0)