]> Dogcows Code - chaz/graphql-client/blobdiff - bin/graphql
add tests and many fixes
[chaz/graphql-client] / bin / graphql
index a94aa018a147d6f5100015e644c49246bda348e1..6afa7711c768a527dd7a0de1d10666921736e6f7 100755 (executable)
@@ -13,6 +13,9 @@ use JSON::MaybeXS;
 
 our $VERSION = '999.999'; # VERSION
 
+my $version;
+my $help;
+my $manual;
 my $url;
 my $transport       = {};
 my $query           = '-';
@@ -21,22 +24,19 @@ my $operation_name;
 my $format          = 'json:pretty';
 my $unpack          = 0;
 my $outfile;
-my $version;
-my $help;
-my $manual;
 GetOptions(
+    'version'               => \$version,
+    'help|h|?'              => \$help,
+    'manual|man'            => \$manual,
     'url|u=s'               => \$url,
     'query|mutation=s'      => \$query,
     'variables|vars|V=s'    => \$variables,
     'variable|var|d=s%'     => \$variables,
-    'operation-name=s'      => \$operation_name,
+    'operation-name|n=s'    => \$operation_name,
     'transport|t=s%'        => \$transport,
     'format|f=s'            => \$format,
     'unpack!'               => \$unpack,
     'output|o=s'            => \$outfile,
-    'version'               => \$version,
-    'help|h|?'              => \$help,
-    'manual|man'            => \$manual,
 ) or pod2usage(2);
 
 if ($version) {
@@ -53,6 +53,11 @@ if ($manual) {
 $url    = shift if !$url;
 $query  = shift if !$query || $query eq '-';
 
+if (!$url) {
+    print STDERR "The <URL> or --url option argument is required.\n";
+    pod2usage(2);
+}
+
 $transport = expand_vars($transport);
 
 if (ref $variables) {
@@ -63,7 +68,14 @@ else {
 }
 
 my $client = GraphQL::Client->new(url => $url);
-$client->transport;     # just make sure we can load the transport
+
+eval { $client->transport };
+if (my $err = $@) {
+    warn $err if $ENV{GRAPHQL_CLIENT_DEBUG};
+    print STDERR "Could not construct a transport for URL: $url\n";
+    print STDERR "Is this URL correct?\n";
+    pod2usage(2);
+}
 
 if (!$query || $query eq '-') {
     print STDERR "Interactive mode engaged! Waiting for a query on <STDIN>...\n"
@@ -71,7 +83,7 @@ if (!$query || $query eq '-') {
     $query = do { local $/; <> };
 }
 
-my $resp = $client->request($query, $variables, $operation_name, $transport);
+my $resp = $client->execute($query, $variables, $operation_name, $transport);
 my $err  = $resp->{errors};
 $unpack = 0 if $err;
 my $data = $unpack ? $resp->{data} : $resp;
@@ -206,8 +218,12 @@ END
 
 =head1 SYNOPSIS
 
-    graphql <URL> <QUERY> [--var key=value]... [--transport key=value]...
-            [--[no-]unpack] [--format json|json:pretty|yaml|csv|tsv]
+    graphql <URL> <QUERY> [ [--variables JSON] | [--variable KEY=VALUE]... ]
+            [--operation-name NAME] [--transport KEY=VALUE]...
+            [--[no-]unpack] [--format json|json:pretty|yaml|perl|csv|tsv|table]
+            [--output FILE]
+
+    graphql --version|--help|--manual
 
 =head1 DESCRIPTION
 
@@ -239,36 +255,37 @@ To hack on the code, clone the repo instead:
 
 =head1 OPTIONS
 
-=head2 --url STR
+=head2 C<--url URL>
 
 The URL of the GraphQL server endpoint.
 
 If no C<--url> option is given, the first argument is assumed to be the URL.
 
+This option is required.
+
 Alias: C<-u>
 
-=head2 --query STR
+=head2 C<--query STR>
 
 The query or mutation to execute.
 
-If no C<--query> option is given, the first argument (after URL) is assumed to be the query.
+If no C<--query> option is given, the next argument (after URL) is assumed to be the query.
 
-If the value is C<-> (which is the default), the query will be read from C<STDIN>.
+If the value is "-" (which is the default), the query will be read from C<STDIN>.
 
 See: L<https://graphql.org/learn/queries/>
 
 Alias: C<--mutation>
 
-=head2 --variables JSON
+=head2 C<--variables JSON>
 
 Provide the variables as a JSON object.
 
 Aliases: C<--vars>, C<-V>
 
-=head2 --variable KEY=VALUE
+=head2 C<--variable KEY=VALUE>
 
-An alternative way to provide variables individually. Repeat this option to provide multiple
-variables.
+An alternative way to provide variables. Repeat this option to provide multiple variables.
 
 If used in combination with L</"--variables JSON">, this option is silently ignored.
 
@@ -276,25 +293,37 @@ See: L<https://graphql.org/learn/queries/#variables>
 
 Aliases: C<--var>, C<-d>
 
-=head2 --transport KEY=VALUE
+=head2 C<--operation-name NAME>
+
+Inform the server which query/mutation to execute.
+
+Alias: C<-n>
+
+=head2 C<--output FILE>
+
+Write the response to a file instead of STDOUT.
+
+Alias: C<-o>
+
+=head2 C<--transport KEY=VALUE>
 
 Key-value pairs for configuring the transport (usually HTTP).
 
 Alias: C<-t>
 
-=head2 --format STR
+=head2 C<--format STR>
 
 Specify the output format to use. See L</FORMAT>.
 
 Alias: C<-f>
 
-=head2 --unpack
+=head2 C<--unpack>
 
-Enables C<unpack> mode.
+Enables unpack mode.
 
 By default, the response structure is printed as-is from the server, and the program exits 0.
 
-When C<unpack> mode is enabled, if the response completes with no errors, only the data section of
+When unpack mode is enabled, if the response completes with no errors, only the data section of
 the response is printed and the program exits 0. If the response has errors, the whole response
 structure is printed as-is and the program exits 1.
 
@@ -306,7 +335,7 @@ The C<--format> argument can be one of:
 
 =for :list
 * C<csv> - Comma-separated values (requires L<Text::CSV>)
-* C<json:pretty> - Pretty JSON (default)
+* C<json:pretty> - Human-readable JSON (default)
 * C<json> - JSON
 * C<perl> - Perl code (requires L<Data::Dumper>)
 * C<table> - Table (requires L<Text::Table::Any>)
@@ -379,11 +408,13 @@ Execute a query with variables:
     > }
     > END
 
+    graphql http://myserver/graphql --vars '{"episode":"JEDI"}'
+
 Configure the transport:
 
     graphql http://myserver/graphql {hello} -t headers.authorization='Basic s3cr3t'
 
-This example shows the effect of L<--unpack>:
+This example shows the effect of L</--unpack>:
 
     graphql http://myserver/graphql {hello}
 
@@ -407,5 +438,16 @@ Some environment variables affect the way C<graphql> behaves:
 
 =for :list
 * C<GRAPHQL_CLIENT_DEBUG> - Set to 1 to print diagnostic messages to STDERR.
+* C<GRAPHQL_CLIENT_HTTP_USER_AGENT> - Set the HTTP user agent string.
 * C<PERL_TEXT_TABLE> - Set table format backend; see L</FORMAT>.
 
+=head1 EXIT STATUS
+
+Here is a consolidated summary of what exit statuses mean:
+
+=for :list
+* C<0> - Success
+* C<1> - Client or server errors
+* C<2> - Option usage is wrong
+* C<3> - Could not format the response as requested
+
This page took 0.024015 seconds and 4 git commands to generate.