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.
},
"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",
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'
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'
"Test::More" => 0,
"lib" => 0
},
- "VERSION" => "0.603",
+ "VERSION" => "0.604",
"test" => {
"TESTS" => "t/*.t"
}
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
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:
use GraphQL::Client::CLI;
-our $VERSION = '0.603'; # VERSION
+our $VERSION = '0.604'; # VERSION
GraphQL::Client::CLI->main(@ARGV);
=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
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:
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(@_) }
=head1 VERSION
-version 0.603
+version 0.604
=head1 SYNOPSIS
use Text::ParseWords;
use namespace::clean;
-our $VERSION = '0.603'; # VERSION
+our $VERSION = '0.604'; # VERSION
my $JSON = JSON::MaybeXS->new(canonical => 1);
*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);
'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);
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') {
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;
}
}
=head1 VERSION
-version 0.603
+version 0.604
=head1 DESCRIPTION
use HTTP::AnyUA;
use namespace::clean;
-our $VERSION = '0.603'; # VERSION
+our $VERSION = '0.604'; # VERSION
sub _croak { require Carp; goto &Carp::croak }
=head1 VERSION
-version 0.603
+version 0.604
=head1 SYNOPSIS
use parent 'GraphQL::Client::http';
-our $VERSION = '0.603'; # VERSION
+our $VERSION = '0.604'; # VERSION
sub new {
my $class = shift;
=head1 VERSION
-version 0.603
+version 0.604
=head1 DESCRIPTION
},
'suggests' => {
'Data::Dumper' => '0',
+ 'JSON::Path::Evaluator' => '0',
'Text::CSV' => '0',
'Text::Table::Any' => '0',
'YAML' => '0'
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,
$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 {