]> Dogcows Code - chaz/graphql-client/commitdiff
Version 0.604
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 22 Mar 2020 09:46:13 +0000 (03:46 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 22 Mar 2020 09:46:13 +0000 (03:46 -0600)
12 files changed:
Changes
META.json
META.yml
Makefile.PL
README
bin/graphql
lib/GraphQL/Client.pm
lib/GraphQL/Client/CLI.pm
lib/GraphQL/Client/http.pm
lib/GraphQL/Client/https.pm
t/00-report-prereqs.dd
t/cli.t

diff --git a/Changes b/Changes
index b20f2a96e043cec596de6a380448464881473531..72f769399531aecf726c965a5cc5a0494894ede4 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for GraphQL-Client.
 
+0.604     2020-03-22 03:44:44-06:00 MST7MDT
+  * Add --filter JSONPATH argument to CLI.
+
 0.603     2020-03-22 02:01:32-06:00 MST7MDT
   * Allow deep formatting with csv,tsv,table.
   * Fix Unicode issues.
index b8bb85f0cd7f95e4dabb4000a7e1560e7c6f5253..c19e73782f2c8f5263fa3eceae266123c758d75d 100644 (file)
--- a/META.json
+++ b/META.json
@@ -89,6 +89,7 @@
          },
          "suggests" : {
             "Data::Dumper" : "0",
+            "JSON::Path::Evaluator" : "0",
             "Text::CSV" : "0",
             "Text::Table::Any" : "0",
             "YAML" : "0"
    "provides" : {
       "GraphQL::Client" : {
          "file" : "lib/GraphQL/Client.pm",
-         "version" : "0.603"
+         "version" : "0.604"
       },
       "GraphQL::Client::CLI" : {
          "file" : "lib/GraphQL/Client/CLI.pm",
-         "version" : "0.603"
+         "version" : "0.604"
       },
       "GraphQL::Client::Error" : {
          "file" : "lib/GraphQL/Client.pm",
-         "version" : "0.603"
+         "version" : "0.604"
       },
       "GraphQL::Client::http" : {
          "file" : "lib/GraphQL/Client/http.pm",
-         "version" : "0.603"
+         "version" : "0.604"
       },
       "GraphQL::Client::https" : {
          "file" : "lib/GraphQL/Client/https.pm",
-         "version" : "0.603"
+         "version" : "0.604"
       }
    },
    "release_status" : "stable",
          "web" : "https://github.com/chazmcgarvey/graphql-client"
       }
    },
-   "version" : "0.603",
+   "version" : "0.604",
    "x_authority" : "cpan:CCM",
    "x_generated_by_perl" : "v5.28.0",
    "x_serialization_backend" : "Cpanel::JSON::XS version 4.15",
index 1c5959c9cf7255520edbaa05b6483c53628c8913..6353c3fe7e2d625d314bc450c85d66c2a50ab152 100644 (file)
--- a/META.yml
+++ b/META.yml
@@ -33,19 +33,19 @@ no_index:
 provides:
   GraphQL::Client:
     file: lib/GraphQL/Client.pm
-    version: '0.603'
+    version: '0.604'
   GraphQL::Client::CLI:
     file: lib/GraphQL/Client/CLI.pm
-    version: '0.603'
+    version: '0.604'
   GraphQL::Client::Error:
     file: lib/GraphQL/Client.pm
-    version: '0.603'
+    version: '0.604'
   GraphQL::Client::http:
     file: lib/GraphQL/Client/http.pm
-    version: '0.603'
+    version: '0.604'
   GraphQL::Client::https:
     file: lib/GraphQL/Client/https.pm
-    version: '0.603'
+    version: '0.604'
 recommends:
   HTTP::Tiny: '0'
   Pod::Usage: '0'
@@ -69,7 +69,7 @@ resources:
   bugtracker: https://github.com/chazmcgarvey/graphql-client/issues
   homepage: https://github.com/chazmcgarvey/graphql-client
   repository: https://github.com/chazmcgarvey/graphql-client.git
-version: '0.603'
+version: '0.604'
 x_authority: cpan:CCM
 x_generated_by_perl: v5.28.0
 x_serialization_backend: 'YAML::Tiny version 1.73'
index 3524dadced3e51b4d7f7ac94d1b7946246721c94..4199bc6375833bc58dc9ec332bdb5ae7d16a9796 100644 (file)
@@ -48,7 +48,7 @@ my %WriteMakefileArgs = (
     "Test::More" => 0,
     "lib" => 0
   },
-  "VERSION" => "0.603",
+  "VERSION" => "0.604",
   "test" => {
     "TESTS" => "t/*.t"
   }
diff --git a/README b/README
index 6129b243d46e70617a880910acde732eaf309f09..b6afcd6bebca8854cd35b2a03aef4c77bac3bd56 100644 (file)
--- a/README
+++ b/README
@@ -4,14 +4,14 @@ NAME
 
 VERSION
 
-    version 0.603
+    version 0.604
 
 SYNOPSIS
 
         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]
+                [--[no-]unpack] [--filter JSONPATH]
+                [--format json|json:pretty|yaml|perl|csv|tsv|table] [--output FILE]
     
         graphql --version|--help|--manual
 
@@ -128,6 +128,14 @@ OPTIONS
     Use --no-unpack to disable if unpack mode was enabled via
     GRAPHQL_CLIENT_OPTIONS.
 
+ --filter JSONPATH
+
+    Filter the response based on a JSONPath expression.
+
+    Requires JSON::Path.
+
+    Alias: -p
+
 FORMAT
 
     The argument for "--format STR" can be one of:
index 00bc95962c33ac3e4d28431a93205a46ddfbdd72..db8e7c52a824bd101c9ea315de81a69d0750258f 100755 (executable)
@@ -10,7 +10,7 @@ use strict;
 
 use GraphQL::Client::CLI;
 
-our $VERSION = '0.603'; # VERSION
+our $VERSION = '0.604'; # VERSION
 
 GraphQL::Client::CLI->main(@ARGV);
 
@@ -26,14 +26,14 @@ graphql - Command-line GraphQL client
 
 =head1 VERSION
 
-version 0.603
+version 0.604
 
 =head1 SYNOPSIS
 
     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]
+            [--[no-]unpack] [--filter JSONPATH]
+            [--format json|json:pretty|yaml|perl|csv|tsv|table] [--output FILE]
 
     graphql --version|--help|--manual
 
@@ -143,6 +143,14 @@ practice.
 
 Use C<--no-unpack> to disable if unpack mode was enabled via C<GRAPHQL_CLIENT_OPTIONS>.
 
+=head2 C<--filter JSONPATH>
+
+Filter the response based on a L<JSONPath|JSON::Path/SYNOPSIS> expression.
+
+Requires L<JSON::Path>.
+
+Alias: C<-p>
+
 =head1 FORMAT
 
 The argument for L</"--format STR"> can be one of:
index 32c41f8b52f1a42ee52a67dcf78e2264fd559e15..8fd2368420fbc5197e2ea0baeec815ce5da96133 100644 (file)
@@ -8,7 +8,7 @@ use Module::Load qw(load);
 use Scalar::Util qw(reftype);
 use namespace::clean;
 
-our $VERSION = '0.603'; # VERSION
+our $VERSION = '0.604'; # VERSION
 
 sub _croak { require Carp; goto &Carp::croak }
 sub _throw { GraphQL::Client::Error->throw(@_) }
@@ -171,7 +171,7 @@ GraphQL::Client - A GraphQL client
 
 =head1 VERSION
 
-version 0.603
+version 0.604
 
 =head1 SYNOPSIS
 
index d28634cf17d26c364ac8b5be2bd842674b52412b..902fc4c1f97c3146d9e8a494e325ad78fd43e8df 100644 (file)
@@ -11,7 +11,7 @@ use JSON::MaybeXS;
 use Text::ParseWords;
 use namespace::clean;
 
-our $VERSION = '0.603'; # VERSION
+our $VERSION = '0.604'; # VERSION
 
 my $JSON = JSON::MaybeXS->new(canonical => 1);
 
@@ -84,6 +84,17 @@ sub main {
         *STDOUT = $out;
     }
 
+    if (my $filter = $options->{filter}) {
+        eval { require JSON::Path::Evaluator } or die "Missing dependency: JSON::Path\n";
+        my @values = JSON::Path::Evaluator::evaluate_jsonpath($data, $filter);
+        if (@values == 1) {
+            $data = $values[0];
+        }
+        else {
+            $data = \@values;
+        }
+    }
+
     binmode(STDOUT, 'encoding(UTF-8)');
     _print_data($data, $format);
 
@@ -115,6 +126,7 @@ sub _get_options {
         'operation-name|n=s'    => \$options{operation_name},
         'transport|t=s%'        => \$options{transport},
         'format|f=s'            => \$options{format},
+        'filter|p=s'            => \$options{filter},
         'unpack!'               => \$options{unpack},
         'output|o=s'            => \$options{outfile},
     ) or _pod2usage(2);
@@ -167,27 +179,42 @@ sub _print_data {
 
         my $unpacked = $data;
         # $unpacked = $data->{data} if !$unpack && !$err;
-        $unpacked = $data->{data} if $data && $data->{data};
+        $unpacked = $data->{data} if ref $data eq 'HASH' && $data->{data};
 
         # check the response to see if it can be formatted
         my @columns;
         my $rows = [];
-        if (keys %$unpacked == 1) {
-            my ($val) = values %$unpacked;
-            if (ref $val eq 'ARRAY') {
-                my $first = $val->[0];
-                if ($first && ref $first eq 'HASH') {
-                    @columns = sort keys %$first;
-                    $rows = [
-                        map { [map { _stringify($_) } @{$_}{@columns}] } @$val
-                    ];
-                }
-                elsif ($first) {
-                    @columns = keys %$unpacked;
-                    $rows = [map { [map { _stringify($_) } $_] } @$val];
+        if (ref $unpacked eq 'HASH') {
+            if (keys %$unpacked == 1) {
+                my ($val) = values %$unpacked;
+                if (ref $val eq 'ARRAY') {
+                    my $first = $val->[0];
+                    if ($first && ref $first eq 'HASH') {
+                        @columns = sort keys %$first;
+                        $rows = [
+                            map { [map { _stringify($_) } @{$_}{@columns}] } @$val
+                        ];
+                    }
+                    elsif ($first) {
+                        @columns = keys %$unpacked;
+                        $rows = [map { [map { _stringify($_) } $_] } @$val];
+                    }
                 }
             }
         }
+        elsif (ref $unpacked eq 'ARRAY') {
+            my $first = $unpacked->[0];
+            if ($first && ref $first eq 'HASH') {
+                @columns = sort keys %$first;
+                $rows = [
+                    map { [map { _stringify($_) } @{$_}{@columns}] } @$unpacked
+                ];
+            }
+            elsif ($first) {
+                @columns = qw(column);
+                $rows = [map { [map { _stringify($_) } $_] } @$unpacked];
+            }
+        }
 
         if (@columns) {
             if ($format eq 'table') {
@@ -214,13 +241,26 @@ sub _print_data {
             exit 3;
         }
     }
+    elsif ($format eq 'string') {
+        if (!ref $data) {
+            print $data, "\n";
+        }
+        elsif (ref $data eq 'ARRAY') {
+            print join("\n", @$data);
+        }
+        else {
+            _print_data($data);
+            print STDERR sprintf("Error: Response could not be formatted as %s.\n", $format);
+            exit 3;
+        }
+    }
     elsif ($format eq 'perl') {
         eval { require Data::Dumper } or die "Missing dependency: Data::Dumper\n";
         print Data::Dumper::Dumper($data);
     }
     else {
-        print STDERR "Error: Format not supported: $format\n";
         _print_data($data);
+        print STDERR "Error: Format not supported: $format\n";
         exit 3;
     }
 }
@@ -328,7 +368,7 @@ GraphQL::Client::CLI - Implementation of the graphql CLI program
 
 =head1 VERSION
 
-version 0.603
+version 0.604
 
 =head1 DESCRIPTION
 
index 48f68e8243aa2bac0764c9816adedc44ac76b6a7..79a5f2af67ca82595ffe8e1eb2a500b3844d3eea 100644 (file)
@@ -9,7 +9,7 @@ use HTTP::AnyUA::Util qw(www_form_urlencode);
 use HTTP::AnyUA;
 use namespace::clean;
 
-our $VERSION = '0.603'; # VERSION
+our $VERSION = '0.604'; # VERSION
 
 sub _croak { require Carp; goto &Carp::croak }
 
@@ -171,7 +171,7 @@ GraphQL::Client::http - GraphQL over HTTP
 
 =head1 VERSION
 
-version 0.603
+version 0.604
 
 =head1 SYNOPSIS
 
index 54d2acf75128a058e0664931d51f04fe0dc3a00f..937f9ada39cfa6ab8341e360344d7ab8909b930f 100644 (file)
@@ -6,7 +6,7 @@ use strict;
 
 use parent 'GraphQL::Client::http';
 
-our $VERSION = '0.603'; # VERSION
+our $VERSION = '0.604'; # VERSION
 
 sub new {
     my $class = shift;
@@ -27,7 +27,7 @@ GraphQL::Client::https - GraphQL over HTTPS
 
 =head1 VERSION
 
-version 0.603
+version 0.604
 
 =head1 DESCRIPTION
 
index 82d5ee631580a6fa35581690a893f88339c2ae31..e8d2e0cacd482d3a3b084a6780ec1134e1da7786 100644 (file)
@@ -65,6 +65,7 @@ do { my $x = {
                                     },
                       'suggests' => {
                                       'Data::Dumper' => '0',
+                                      'JSON::Path::Evaluator' => '0',
                                       'Text::CSV' => '0',
                                       'Text::Table::Any' => '0',
                                       'YAML' => '0'
diff --git a/t/cli.t b/t/cli.t
index 2ae980e4f0a76e55785ba01d8da9fd56ff1bd404..a32af83b333f6189b694c70185f0d36b0e2a0cc6 100755 (executable)
--- a/t/cli.t
+++ b/t/cli.t
@@ -8,9 +8,12 @@ use Test::More;
 
 use GraphQL::Client::CLI;
 
+delete $ENV{GRAPHQL_CLIENT_OPTIONS};
+
 subtest 'get_options' => sub {
     my $expected = {
         format          => 'json:pretty',
+        filter          => undef,
         help            => undef,
         manual          => undef,
         operation_name  => undef,
@@ -31,6 +34,14 @@ subtest 'get_options' => sub {
 
     $r = GraphQL::Client::CLI->_get_options(qw{foo bar});
     is_deeply($r, $expected, '--query is also optional') or diag explain $r;
+
+    {
+        local $ENV{GRAPHQL_CLIENT_OPTIONS} = '--url asdf --query "baz qux" --unpack';
+        local $expected->{query} = 'baz qux';
+        local $expected->{unpack} = 1;
+        $r = GraphQL::Client::CLI->_get_options(qw{--url foo});
+        is_deeply($r, $expected, 'options can come from GRAPHQL_CLIENT_OPTIONS') or diag explain $r;
+    }
 };
 
 subtest 'expand_vars' => sub {
This page took 0.042294 seconds and 4 git commands to generate.