Client.pm

filter: Statementstmt 99.0% 99 / 100 Branchbran 82.1% 23 / 28 Conditioncond 68.4% 13 / 19 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 26 / 26 Podpod 100% 6 / 6 totaltotal 90.0% 171 / 190 CC 23 SCAR 11.0
CC
23
Cov
92%
transport
6.2
execute
6.0
__ANON__
4.0
CRAP 3.0
« Client/http.pm ↑ Summary Client/https.pm »
1 package GraphQL::Client;
2 # ABSTRACT: A GraphQL client
3  
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Module::Load qw(load);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.605'; # VERSION
12  
13 1 ▶sub _croak { require Carp; goto &Carp::croak }
Subroutine _croak: called CC=1 SCAR=0.0
14 2 ▶sub _throw { GraphQL::Client::Error->throw(@_) }
Subroutine _throw: called CC=1 SCAR=0.0
15  
16 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17 6 my $class = shift;
18 6 bless {@_}, $class;
19 }
20  
21 13 ▶sub execute {
Subroutine execute: called CC=6 SCAR=17.9
Pod documented
22 13 my $self = shift;
23 13 my ($query, $variables, $operation_name, $options) = @_;
24  
25 13 ▶ if ((reftype($operation_name) || '') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($operation_name) || "") eq "HASH") 1 12 13
Condition100%2/2
builtin::reftype($operation_name) || ''
l!l
112
MC/DC100%1/1
builtin::reftype($operation_name) || ''
builtin::reftype($operation_name)
Truth table100%2/2
A
A= builtin::reftype($operation_name)
Aresult
11
00
26 1 $options = $operation_name;
27 1 $operation_name = undef;
28 }
29  
30 13 ▶ my $request = {
Branch: 2 branches
BranchTrueFalseTotal
$variables && %$variables ? : 4 9 13
$operation_name ? : 2 11 13
Condition66%2/3
$variables and %$variables
!ll&&!rl&&r
904
MC/DC50%1/2
$variables and %$variables
$variables %$variables
Truth table66%2/3
A and B
A= $variablesB= %$variables
ABresult
0X0
100
111
31 query => $query,
32 ($variables && %$variables) ? (variables => $variables) : (),
33 $operation_name ? (operationName => $operation_name) : (),
34 };
35  
36 13 return $self->_handle_result($self->transport->execute($request, $options));
37 }
38  
39 13 ▶sub _handle_result {
Subroutine _handle_result: called CC=2 SCAR=6.9
40 13 my $self = shift;
41 13 my ($result) = @_;
42  
43 13 ▶ my $handle_result = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
44 13 my $result = shift;
45 13 my $resp = $result->{response};
46 13 ▶ if (my $exception = $result->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exception = $result->{"error"}) 2 11 13
47 2 unshift @{$resp->{errors}}, {
48 message => "$exception",
49 };
50 }
51 13 ▶ if ($self->unpack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unpack) 4 9 13
52 4 ▶ if ($resp->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"errors"}) 2 2 4
53 _throw $resp->{errors}[0]{message}, {
54 type => 'graphql',
55 response => $resp,
56 details => $result->{details},
57 2 };
58 }
59 2 return $resp->{data};
60 }
61 9 return $resp;
62 13 };
63  
64 13 ▶ if (eval { $result->isa('Future') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $result->isa('Future') } }) { } 2 11 13
65 return $result->transform(
66 2 ▶ done => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
67 2 my $result = shift;
68 2 my $resp = eval { $handle_result->($result) };
69 2 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 2 2
70 0 Future::Exception->throw("$err", $err->{type}, $err->{response}, $err->{details});
71 }
72 2 return $resp;
73 },
74 2 );
75 }
76 else {
77 11 return $handle_result->($result);
78 }
79 }
80  
81 1 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
Pod documented
82 1 my $self = shift;
83 1 $self->{url};
84 }
85  
86 4 ▶sub transport_class {
Subroutine transport_class: called CC=1 SCAR=0.0
Pod documented
87 4 my $self = shift;
88 4 $self->{transport_class};
89 }
90  
91 16 ▶sub transport {
Subroutine transport: called CC=6 SCAR=18.3
Pod documented
92 16 my $self = shift;
93 16 ▶ $self->{transport} //= do {
Condition66%2/3
$self->{"transport"} //= do { my $class = $self->_autodetermine_transport_class; eval { do { &load($class) } }; if (my $err = $@ or not $class->can("execute")) { $err ||= "Loaded $class, but it doesn't look like a proper transport.\n"; warn $err if $ENV{"GRAPHQL_CLIENT_DEBUG"}; _croak(qq[Failed to load transport for "$class"]); } ; $class->new(%$self) }
l!l&&r!l&&!r
1330
MC/DC0%0/2
$self->{"transport"} //= do { my $class = $self->_autodetermine_transport_class; eval { do { &load($class) } }; if (my $err = $@ or not $class->can("execute")) { $err ||= "Loaded $class, but it doesn't look like a proper transport.\n"; warn $err if $ENV{"GRAPHQL_CLIENT_DEBUG"}; _croak(qq[Failed to load transport for "$class"]); } ; $class->new(%$self) }
$self->{"transport"} do { my $class = $self->_autodetermine_transport_class; eval { do { &load($class) } }; if (my $err = $@ or not $class->can("execute")) { $err ||= "Loaded $class, but it doesn't look like a proper transport.\n"; warn $err if $ENV{"GRAPHQL_CLIENT_DEBUG"}; _croak(qq[Failed to load transport for "$class"]); } ; $class->new(%$self) }
Truth table66%2/3
A //= B
A= $self->{"transport"}B= do { my $class = $self->_autodetermine_transport_class; eval { do { &load($class) } }; if (my $err = $@ or not $class->can("execute")) { $err ||= "Loaded $class, but it doesn't look like a proper transport.\n"; warn $err if $ENV{"GRAPHQL_CLIENT_DEBUG"}; _croak(qq[Failed to load transport for "$class"]); } ; $class->new(%$self) }
ABresult
1X1
011
000
94 3 my $class = $self->_autodetermine_transport_class;
95 3 eval { load $class };
96 3 ▶ if ((my $err = $@) || !$class->can('execute')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@ or not $class->can("execute")) 1 2 3
Condition66%2/3
my $err = $@ or not $class->can("execute")
l!l&&r!l&&!r
102
MC/DC50%1/2
my $err = $@ or not $class->can("execute")
my $err = $@ not $class->can("execute")
Truth table66%2/3
A or not(B)
A= my $err = $@B= not $class->can("execute")
ABresult
1X1
011
000
97 1 ▶ $err ||= "Loaded $class, but it doesn't look like a proper transport.\n";
Condition50%1/2
$err ||= "Loaded $class, but it doesn't look like a proper transport.\n"
l!l
10
MC/DC0%0/1
$err ||= "Loaded $class, but it doesn't look like a proper transport.\n"
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
98 1 ▶ warn $err if $ENV{GRAPHQL_CLIENT_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GRAPHQL_CLIENT_DEBUG"} 0 1 1
99 1 _croak "Failed to load transport for \"${class}\"";
100 }
101 2 $class->new(%$self);
102 };
103 }
104  
105 13 ▶sub unpack {
Subroutine unpack: called CC=2 SCAR=6.9
Pod documented
106 13 my $self = shift;
107 13 ▶ $self->{unpack} //= 0;
Condition100%2/2
$self->{"unpack"} //= 0
l!l
103
MC/DC100%1/1
$self->{"unpack"} //= 0
$self->{"unpack"}
Truth table100%2/2
A
A= $self->{"unpack"}
Aresult
11
00
108 }
109  
110 1 ▶sub _url_protocol {
Subroutine _url_protocol: called CC=1 SCAR=0.0
111 1 my $self = shift;
112  
113 1 my $url = $self->url;
114 1 my ($protocol) = $url =~ /^([^+:]+)/;
115  
116 1 return $protocol;
117 }
118  
119 3 ▶sub _autodetermine_transport_class {
Subroutine _autodetermine_transport_class: called CC=3 SCAR=11.0
120 3 my $self = shift;
121  
122 3 my $class = $self->transport_class;
123 3 ▶ return _expand_class($class) if $class;
Branch: 1 branch
BranchTrueFalseTotal
if $class 2 1 3
124  
125 1 my $protocol = $self->_url_protocol;
126 1 ▶ _croak 'Failed to determine transport from URL' if !$protocol;
Branch: 1 branch
BranchTrueFalseTotal
unless $protocol 0 1 1
127  
128 1 $class = lc($protocol);
129 1 $class =~ s/[^a-z]/_/g;
130  
131 1 return _expand_class($class);
132 }
133  
134 3 ▶sub _expand_class {
Subroutine _expand_class: called CC=2 SCAR=7.1
135 3 my $class = shift;
136 3 ▶ $class = "GraphQL::Client::$class" unless $class =~ s/^\+//;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ s/^\+// 3 0 3
137 3 $class;
138 }
139  
140 {
141 package GraphQL::Client::Error;
142  
143 4 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 4 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 4 ▶ use overload '""' => \&error, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 2 ▶ sub new { bless {%{$_[2] || {}}, error => $_[1] || 'Something happened'}, $_[0] }
Subroutine new: called CC=3 SCAR=12.0
Condition50%2/4
$_[2] || {}
l!l
20
$_[1] || 'Something happened'
l!l
20
MC/DC0%0/1
$_[2] || {}
$_[2]
MC/DC0%0/1
$_[1] || 'Something happened'
$_[1]
Truth table50%1/2
A
A= $_[2]
Aresult
11
00
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
149  
150 3 ▶ sub error { "$_[0]->{error}" }
Subroutine error: called CC=1 SCAR=0.0
151 1 ▶ sub type { "$_[0]->{type}" }
Subroutine type: called CC=1 SCAR=0.0
152  
153 2 ▶ sub throw {
Subroutine throw: called CC=2 SCAR=7.1
154 2 my $self = shift;
155 2 ▶ die $self if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 2 2
156 2 die $self->new(@_);
157 }
158 }
159  
160 4 1;
161  
162 __END__
« Client/http.pm ↑ Summary Client/https.pm »