From: Charles McGarvey Date: Sun, 22 Mar 2020 09:46:13 +0000 (-0600) Subject: Version 0.604 X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fgraphql-client;a=commitdiff_plain;h=26b24f42605c8646a9c21ef2b94f1ceeec6cf634 Version 0.604 --- diff --git a/Changes b/Changes index b20f2a9..72f7693 100644 --- 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. diff --git a/META.json b/META.json index b8bb85f..c19e737 100644 --- 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" @@ -116,23 +117,23 @@ "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", @@ -147,7 +148,7 @@ "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", diff --git a/META.yml b/META.yml index 1c5959c..6353c3f 100644 --- 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' diff --git a/Makefile.PL b/Makefile.PL index 3524dad..4199bc6 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -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 6129b24..b6afcd6 100644 --- a/README +++ b/README @@ -4,14 +4,14 @@ NAME VERSION - version 0.603 + version 0.604 SYNOPSIS graphql [ [--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: diff --git a/bin/graphql b/bin/graphql index 00bc959..db8e7c5 100755 --- a/bin/graphql +++ b/bin/graphql @@ -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 [ [--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. +=head2 C<--filter JSONPATH> + +Filter the response based on a L expression. + +Requires L. + +Alias: C<-p> + =head1 FORMAT The argument for L can be one of: diff --git a/lib/GraphQL/Client.pm b/lib/GraphQL/Client.pm index 32c41f8..8fd2368 100644 --- a/lib/GraphQL/Client.pm +++ b/lib/GraphQL/Client.pm @@ -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 diff --git a/lib/GraphQL/Client/CLI.pm b/lib/GraphQL/Client/CLI.pm index d28634c..902fc4c 100644 --- a/lib/GraphQL/Client/CLI.pm +++ b/lib/GraphQL/Client/CLI.pm @@ -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 diff --git a/lib/GraphQL/Client/http.pm b/lib/GraphQL/Client/http.pm index 48f68e8..79a5f2a 100644 --- a/lib/GraphQL/Client/http.pm +++ b/lib/GraphQL/Client/http.pm @@ -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 diff --git a/lib/GraphQL/Client/https.pm b/lib/GraphQL/Client/https.pm index 54d2acf..937f9ad 100644 --- a/lib/GraphQL/Client/https.pm +++ b/lib/GraphQL/Client/https.pm @@ -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 diff --git a/t/00-report-prereqs.dd b/t/00-report-prereqs.dd index 82d5ee6..e8d2e0c 100644 --- a/t/00-report-prereqs.dd +++ b/t/00-report-prereqs.dd @@ -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 2ae980e..a32af83 100755 --- 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 {