Skip to content

Commit 0dfaf28

Browse files
author
Bryan Cross
committed
refactor to node, add support for variables
1 parent 854c8bd commit 0dfaf28

14 files changed

+282
-0
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/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.parse(process.argv);
20+
21+
if (!process.argv.slice(2).length)
22+
{
23+
console.log("Missing query file and/or token argument");
24+
process.exitCode = 1;
25+
}
26+
27+
function runQuery(file, token) {
28+
29+
try {
30+
var queryText = fs.readFileSync(process.argv[2], "utf8");
31+
}
32+
catch (e) {
33+
console.log("Problem opening query file: " + e.message);
34+
process.exit(1);
35+
}
36+
37+
//If there is a variables section, extract the values and add them to the query JSON object. Otherwise, add and empty object
38+
queryObj.variables = variablesRegex.test(queryText) ? JSON.parse(queryText.match(variablesRegex)[0].split("variables ")[1]) : {}
39+
//Remove the variables section from the query text, whether it exists or not
40+
queryObj.query = queryText.replace(variablesRegex, '');
41+
42+
request({
43+
url: "https://api.github.com/graphql"
44+
, method: "POST"
45+
, headers: {
46+
'authorization': 'bearer ' + token
47+
, 'content-type': 'application/json'
48+
, 'user-agent': 'octoweenie'
49+
}
50+
, json: true
51+
, body: queryObj
52+
//,body: testQuery
53+
}, function (error, response, body) {
54+
console.log(JSON.stringify(body, null, 2));
55+
});
56+
};

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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
query {
2+
organization(login: "github") {
3+
...orgFrag
4+
repositories {
5+
...repoFrag
6+
}
7+
}
8+
}
9+
10+
fragment repoFrag on RepositoryConnection {
11+
totalCount
12+
totalDiskUsage
13+
}
14+
15+
fragment orgFrag on Organization{
16+
login
17+
name
18+
}

0 commit comments

Comments
 (0)