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 = / v a r i a b l e s ( [ \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
+ } ;
0 commit comments