From 4df3f6c276a2ee1d697d2199e186c187436333d8 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Sun, 22 Mar 2020 02:03:00 -0600 Subject: [PATCH] Version 0.603 --- Changes | 4 ++++ META.json | 13 +++++++------ META.yml | 13 +++++++------ Makefile.PL | 4 +++- README | 8 +++++--- bin/graphql | 9 +++++---- lib/GraphQL/Client.pm | 4 ++-- lib/GraphQL/Client/CLI.pm | 33 ++++++++++++++++++++++++++------- lib/GraphQL/Client/http.pm | 6 +++--- lib/GraphQL/Client/https.pm | 4 ++-- t/00-report-prereqs.dd | 1 + t/http.t | 2 +- 12 files changed, 66 insertions(+), 35 deletions(-) diff --git a/Changes b/Changes index 24d3189..b20f2a9 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for GraphQL-Client. +0.603 2020-03-22 02:01:32-06:00 MST7MDT + * Allow deep formatting with csv,tsv,table. + * Fix Unicode issues. + 0.602 2020-03-17 18:27:46-06:00 MST7MDT * Add support for GRAPHQL_CLIENT_OPTIONS environment variable. * Fix a slew of CLI option problems. diff --git a/META.json b/META.json index c98c070..b8bb85f 100644 --- a/META.json +++ b/META.json @@ -72,6 +72,7 @@ }, "requires" : { "Carp" : "0", + "Encode" : "0", "Getopt::Long" : "2.39", "HTTP::AnyUA" : "0", "HTTP::AnyUA::Util" : "0", @@ -115,23 +116,23 @@ "provides" : { "GraphQL::Client" : { "file" : "lib/GraphQL/Client.pm", - "version" : "0.602" + "version" : "0.603" }, "GraphQL::Client::CLI" : { "file" : "lib/GraphQL/Client/CLI.pm", - "version" : "0.602" + "version" : "0.603" }, "GraphQL::Client::Error" : { "file" : "lib/GraphQL/Client.pm", - "version" : "0.602" + "version" : "0.603" }, "GraphQL::Client::http" : { "file" : "lib/GraphQL/Client/http.pm", - "version" : "0.602" + "version" : "0.603" }, "GraphQL::Client::https" : { "file" : "lib/GraphQL/Client/https.pm", - "version" : "0.602" + "version" : "0.603" } }, "release_status" : "stable", @@ -146,7 +147,7 @@ "web" : "https://github.com/chazmcgarvey/graphql-client" } }, - "version" : "0.602", + "version" : "0.603", "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 95a9518..1c5959c 100644 --- a/META.yml +++ b/META.yml @@ -33,24 +33,25 @@ no_index: provides: GraphQL::Client: file: lib/GraphQL/Client.pm - version: '0.602' + version: '0.603' GraphQL::Client::CLI: file: lib/GraphQL/Client/CLI.pm - version: '0.602' + version: '0.603' GraphQL::Client::Error: file: lib/GraphQL/Client.pm - version: '0.602' + version: '0.603' GraphQL::Client::http: file: lib/GraphQL/Client/http.pm - version: '0.602' + version: '0.603' GraphQL::Client::https: file: lib/GraphQL/Client/https.pm - version: '0.602' + version: '0.603' recommends: HTTP::Tiny: '0' Pod::Usage: '0' requires: Carp: '0' + Encode: '0' Getopt::Long: '2.39' HTTP::AnyUA: '0' HTTP::AnyUA::Util: '0' @@ -68,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.602' +version: '0.603' 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 2c7c27a..3524dad 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -21,6 +21,7 @@ my %WriteMakefileArgs = ( "NAME" => "GraphQL::Client", "PREREQ_PM" => { "Carp" => 0, + "Encode" => 0, "Getopt::Long" => "2.39", "HTTP::AnyUA" => 0, "HTTP::AnyUA::Util" => 0, @@ -47,7 +48,7 @@ my %WriteMakefileArgs = ( "Test::More" => 0, "lib" => 0 }, - "VERSION" => "0.602", + "VERSION" => "0.603", "test" => { "TESTS" => "t/*.t" } @@ -56,6 +57,7 @@ my %WriteMakefileArgs = ( my %FallbackPrereqs = ( "Carp" => 0, + "Encode" => 0, "ExtUtils::MakeMaker" => 0, "File::Spec" => 0, "FindBin" => 0, diff --git a/README b/README index 8d89255..6129b24 100644 --- a/README +++ b/README @@ -4,7 +4,7 @@ NAME VERSION - version 0.602 + version 0.603 SYNOPSIS @@ -122,9 +122,11 @@ OPTIONS 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. + as-is and the program exits 1. See "EXAMPLES" to see what this looks + like in practice. - See "EXAMPLES". + Use --no-unpack to disable if unpack mode was enabled via + GRAPHQL_CLIENT_OPTIONS. FORMAT diff --git a/bin/graphql b/bin/graphql index e5c6784..00bc959 100755 --- a/bin/graphql +++ b/bin/graphql @@ -10,7 +10,7 @@ use strict; use GraphQL::Client::CLI; -our $VERSION = '0.602'; # VERSION +our $VERSION = '0.603'; # VERSION GraphQL::Client::CLI->main(@ARGV); @@ -26,7 +26,7 @@ graphql - Command-line GraphQL client =head1 VERSION -version 0.602 +version 0.603 =head1 SYNOPSIS @@ -138,9 +138,10 @@ By default, the response structure is printed as-is from the server, and the pro 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. +structure is printed as-is and the program exits 1. See L to see what this looks like in +practice. -See L. +Use C<--no-unpack> to disable if unpack mode was enabled via C. =head1 FORMAT diff --git a/lib/GraphQL/Client.pm b/lib/GraphQL/Client.pm index c4a6119..32c41f8 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.602'; # VERSION +our $VERSION = '0.603'; # 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.602 +version 0.603 =head1 SYNOPSIS diff --git a/lib/GraphQL/Client/CLI.pm b/lib/GraphQL/Client/CLI.pm index bb2ab03..d28634c 100644 --- a/lib/GraphQL/Client/CLI.pm +++ b/lib/GraphQL/Client/CLI.pm @@ -4,13 +4,16 @@ package GraphQL::Client::CLI; use warnings; use strict; -use Text::ParseWords; +use Encode qw(decode); use Getopt::Long 2.39 qw(GetOptionsFromArray); use GraphQL::Client; use JSON::MaybeXS; +use Text::ParseWords; use namespace::clean; -our $VERSION = '0.602'; # VERSION +our $VERSION = '0.603'; # VERSION + +my $JSON = JSON::MaybeXS->new(canonical => 1); sub _croak { require Carp; goto &Carp::croak } @@ -67,6 +70,7 @@ sub main { if ($query eq '-') { print STDERR "Interactive mode engaged! Waiting for a query on ...\n" if -t STDIN; ## no critic (InputOutput::ProhibitInteractiveTest) + binmode(STDIN, 'encoding(UTF-8)'); $query = do { local $/; }; } @@ -80,6 +84,7 @@ sub main { *STDOUT = $out; } + binmode(STDOUT, 'encoding(UTF-8)'); _print_data($data, $format); exit($unpack && $err ? 1 : 0); @@ -91,6 +96,9 @@ sub _get_options { unshift @args, shellwords($ENV{GRAPHQL_CLIENT_OPTIONS} || ''); + # assume UTF-8 args if non-ASCII + @args = map { decode('UTF-8', $_) } @args if grep { /\P{ASCII}/ } @args; + my %options = ( format => 'json:pretty', unpack => 0, @@ -124,18 +132,29 @@ sub _get_options { die "Two or more --variable keys are incompatible.\n" if $@; } elsif ($options{variables}) { - $options{variables} = eval { JSON::MaybeXS->new->decode($options{variables}) }; + $options{variables} = eval { $JSON->decode($options{variables}) }; die "The --variables JSON does not parse.\n" if $@; } return \%options; } +sub _stringify { + my ($item) = @_; + if (ref($item) eq 'ARRAY') { + my $first = @$item && $item->[0]; + return join(',', @$item) if !ref($first); + return join(',', map { $JSON->encode($_) } @$item); + } + return $JSON->encode($item) if ref($item) eq 'HASH'; + return $item; +} + sub _print_data { my ($data, $format) = @_; $format = lc($format || 'json:pretty'); if ($format eq 'json' || $format eq 'json:pretty') { - my %opts = (allow_nonref => 1, canonical => 1, utf8 => 1); + my %opts = (allow_nonref => 1, canonical => 1); $opts{pretty} = 1 if $format eq 'json:pretty'; print JSON::MaybeXS->new(%opts)->encode($data); } @@ -160,12 +179,12 @@ sub _print_data { if ($first && ref $first eq 'HASH') { @columns = sort keys %$first; $rows = [ - map { [@{$_}{@columns}] } @$val + map { [map { _stringify($_) } @{$_}{@columns}] } @$val ]; } elsif ($first) { @columns = keys %$unpacked; - $rows = [map { [$_] } @$val]; + $rows = [map { [map { _stringify($_) } $_] } @$val]; } } } @@ -309,7 +328,7 @@ GraphQL::Client::CLI - Implementation of the graphql CLI program =head1 VERSION -version 0.602 +version 0.603 =head1 DESCRIPTION diff --git a/lib/GraphQL/Client/http.pm b/lib/GraphQL/Client/http.pm index 667e44e..48f68e8 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.602'; # VERSION +our $VERSION = '0.603'; # VERSION sub _croak { require Carp; goto &Carp::croak } @@ -42,7 +42,7 @@ sub execute { my $encoded_data = $self->json->encode($data); $options->{content} = $encoded_data; $options->{headers}{'content-length'} = length $encoded_data; - $options->{headers}{'content-type'} = 'application/json'; + $options->{headers}{'content-type'} = 'application/json;charset=UTF-8'; } return $self->_handle_response($self->any_ua->request($method, $url, $options)); @@ -171,7 +171,7 @@ GraphQL::Client::http - GraphQL over HTTP =head1 VERSION -version 0.602 +version 0.603 =head1 SYNOPSIS diff --git a/lib/GraphQL/Client/https.pm b/lib/GraphQL/Client/https.pm index 6df0df9..54d2acf 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.602'; # VERSION +our $VERSION = '0.603'; # VERSION sub new { my $class = shift; @@ -27,7 +27,7 @@ GraphQL::Client::https - GraphQL over HTTPS =head1 VERSION -version 0.602 +version 0.603 =head1 DESCRIPTION diff --git a/t/00-report-prereqs.dd b/t/00-report-prereqs.dd index fa4b219..82d5ee6 100644 --- a/t/00-report-prereqs.dd +++ b/t/00-report-prereqs.dd @@ -48,6 +48,7 @@ do { my $x = { }, 'requires' => { 'Carp' => '0', + 'Encode' => '0', 'Getopt::Long' => '2.39', 'HTTP::AnyUA' => '0', 'HTTP::AnyUA::Util' => '0', diff --git a/t/http.t b/t/http.t index 61a9ccf..cad84fd 100755 --- a/t/http.t +++ b/t/http.t @@ -57,7 +57,7 @@ subtest 'POST request' => sub { is($req->[0], 'POST', 'method is POST'); is($req->[2]{content}, '{"query":"{hello}"}', 'encoded body as JSON'); - is($req->[2]{headers}{'content-type'}, 'application/json', 'set content-type to json'); + is($req->[2]{headers}{'content-type'}, 'application/json;charset=UTF-8', 'set content-type to json'); }; subtest 'GET request' => sub { -- 2.44.0