blib/lib/GraphQL/Houtou/PSGI.pm

filter: Statementstmt 97.5% 119 / 122 Branchbran 79.4% 62 / 78 Conditioncond 58.7% 27 / 46 MC/DCmcdc 16.6% 4 / 24 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 3 totaltotal 78.9% 229 / 290 CC 58 SCAR 28.4
CC
58
Cov
85%
_handle_post
34.4
new
9.3
_read_body
7.9
CRAP 17.1
« blib/lib/GraphQL/Houtou/Role/HashMappable.pm ↑ Summary blib/lib/GraphQL/Houtou/Validation/DepthLimit.pm »
1 package GraphQL::Houtou::PSGI;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use GraphQL::Houtou ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = $GraphQL::Houtou::VERSION;
13  
14 3 my $JSON = JSON::MaybeXS->new->utf8;
15  
16 7 ▶sub new {
Subroutine new: called CC=9 SCAR=22.3
Pod undocumented
17 7 my ($class, %args) = @_;
18  
19 my $allow_introspection = exists $args{allow_introspection}
20 7 ▶ ? delete $args{allow_introspection} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'allow_introspection'} ? : 1 6 7
21  
22 7 my $runtime = delete $args{runtime};
23 7 ▶ if (!$runtime) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($runtime) 7 0 7
24 my $schema = delete $args{schema}
25 7 ▶ or die "GraphQL::Houtou::PSGI->new requires a schema or a runtime\n";
Branch: 1 branch
BranchTrueFalseTotal
my $schema = delete $args{"schema"} or die "GraphQL::Houtou::PSGI->new requires a schema or a runtime\n" 7 0 7
26 7 my %runtime_opts;
27 $runtime_opts{program_cache_max} = delete $args{program_cache_max}
28 7 ▶ if exists $args{program_cache_max};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"program_cache_max"} 0 7 7
29 7 ▶ $runtime_opts{async} = delete $args{async} if exists $args{async};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"async"} 1 6 7
30 7 ▶ $runtime_opts{allow_introspection} = $allow_introspection
Branch: 1 branch
BranchTrueFalseTotal
if defined $allow_introspection 1 6 7
31 if defined $allow_introspection;
32 7 $runtime = $schema->build_native_runtime(%runtime_opts);
33 }
34  
35 # Request body cap: an unauthenticated client
36 # must not be able to exhaust memory with a giant body. 1 MiB is far
37 # above any real GraphQL request. Pass max_body_size => 0 to disable.
38 my $max_body_size = exists $args{max_body_size}
39 ? delete $args{max_body_size}
40 7 ▶ : 1024 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'max_body_size'} ? : 1 6 7
41  
42 my $self = bless {
43 runtime => $runtime,
44 graphiql => delete $args{graphiql},
45 graphiql_path => delete $args{graphiql_path},
46 context => delete $args{context},
47 root_value => delete $args{root_value},
48 on_stall => delete $args{on_stall},
49 max_depth => delete $args{max_depth},
50 max_nodes => delete $args{max_nodes},
51 max_cost => delete $args{max_cost},
52 default_list_size => delete $args{default_list_size},
53 7 allow_introspection => $allow_introspection,
54 max_body_size => $max_body_size,
55 }, $class;
56  
57 7 ▶ if (my @unknown = sort keys %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unknown) = sort(keys %args)) 0 7 7
58 0 die "Unknown GraphQL::Houtou::PSGI options: @unknown\n";
59 }
60 7 return $self;
61 }
62  
63 7 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.0
Pod undocumented
64 7 my ($self) = @_;
65 7 ▶ return sub { $self->call($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
66 }
67  
68 23 ▶sub call {
Subroutine call: called CC=7 SCAR=19.7
Pod undocumented
69 23 my ($self, $env) = @_;
70 23 ▶ my $method = $env->{REQUEST_METHOD} || '';
Condition50%1/2
$env->{'REQUEST_METHOD'} || ''
l!l
230
MC/DC0%0/1
$env->{'REQUEST_METHOD'} || ''
$env->{'REQUEST_METHOD'}
Truth table50%1/2
A
A= $env->{'REQUEST_METHOD'}
Aresult
11
00
71  
72 23 ▶ if ($method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST") 20 3 23
73 20 return $self->_handle_post($env);
74 }
75 3 ▶ if ($method eq 'GET' && $self->{graphiql} && _accepts_html($env)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "GET" and $self->{"graphiql"} and _accepts_html($env)) 1 2 3
Condition83%5/6
$method eq "GET" and $self->{"graphiql"} and _accepts_html($env)
!ll&&!rl&&r
111
$method eq "GET" and $self->{"graphiql"}
!ll&&!rl&&r
102
MC/DC66%2/3
$method eq "GET" and $self->{"graphiql"} and _accepts_html($env)
$method eq "GET" $self->{"graphiql"} _accepts_html($env)
Truth table75%3/4
A and B and C
A= $method eq "GET"B= $self->{"graphiql"}C= _accepts_html($env)
ABCresult
0XX0
10X0
1100
1111
76 1 return _graphiql_response($self);
77 }
78 return _error_response($env, 405, 'GraphQL over HTTP requests must use POST',
79 2 ▶ [ Allow => $self->{graphiql} ? 'GET, POST' : 'POST' ]);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'graphiql'} ? : 2 0 2
80 }
81  
82 20 ▶sub _handle_post {
Subroutine _handle_post: called CC=30 SCAR=35.4
83 20 my ($self, $env) = @_;
84  
85 20 ▶ my $content_type = lc($env->{CONTENT_TYPE} || '');
Condition50%1/2
$env->{'CONTENT_TYPE'} || ''
l!l
200
MC/DC0%0/1
$env->{'CONTENT_TYPE'} || ''
$env->{'CONTENT_TYPE'}
Truth table50%1/2
A
A= $env->{'CONTENT_TYPE'}
Aresult
11
00
86 20 $content_type =~ s/\s*;.*//s;
87 20 ▶ if ($content_type ne 'application/json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type ne "application/json") 1 19 20
88 1 return _error_response($env, 415, 'Content-Type must be application/json');
89 }
90  
91 19 my $max_body = $self->{max_body_size};
92 19 ▶ if ($max_body && ($env->{CONTENT_LENGTH} || 0) > $max_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_body and ($env->{"CONTENT_LENGTH"} || 0) > $max_body) 1 18 19
Condition60%3/5
$max_body and ($env->{"CONTENT_LENGTH"} || 0) > $max_body
!ll&&!rl&&r
0181
$env->{'CONTENT_LENGTH'} || 0
l!l
190
MC/DC0%0/2
$max_body and ($env->{"CONTENT_LENGTH"} || 0) > $max_body
$max_body $env->{'CONTENT_LENGTH'}
Truth table33%1/3
A and B
A= $max_bodyB= $env->{'CONTENT_LENGTH'}
ABresult
0X0
100
111
93 1 return _error_response($env, 413, 'Request body is too large');
94 }
95  
96 18 my ($body, $too_large) = _read_body($env, $max_body);
97 18 ▶ return _error_response($env, 413, 'Request body is too large') if $too_large;
Branch: 1 branch
BranchTrueFalseTotal
if $too_large 0 18 18
98 18 my $payload = do {
99 18 local $@;
100 18 my $decoded = eval { $JSON->decode($body) };
101 18 ▶ $@ ? undef : $decoded;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 1 17 18
102 };
103 18 ▶ if (ref $payload ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $payload ne "HASH") 1 17 18
104 1 return _error_response($env, 400, 'Request body must be a JSON object');
105 }
106  
107 17 my $query = $payload->{query};
108 17 ▶ if (!defined $query || ref $query || $query !~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $query or ref $query or not $query =~ /\S/u) 1 16 17
Condition66%4/6
not defined $query or ref $query or not $query =~ /\S/u
l!l&&r!l&&!r
1016
not defined $query or ref $query
l!l&&r!l&&!r
1016
MC/DC33%1/3
not defined $query or ref $query or not $query =~ /\S/u
not defined $query ref $query not $query =~ /\S/u
Truth table50%2/4
not(A) or B or not(C)
A= not defined $queryB= ref $queryC= not $query =~ /\S/u
ABCresult
1XX1
01X1
0011
0000
109 1 return _error_response($env, 400, 'The "query" field is required');
110 }
111 16 my $variables = $payload->{variables};
112 16 ▶ if (defined $variables && ref $variables ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $variables and ref $variables ne "HASH") 0 16 16
Condition66%2/3
defined $variables and ref $variables ne "HASH"
!ll&&!rl&&r
1420
MC/DC0%0/2
defined $variables and ref $variables ne "HASH"
defined $variables ref $variables ne "HASH"
Truth table66%2/3
A and B
A= defined $variablesB= ref $variables ne "HASH"
ABresult
0X0
100
111
113 0 return _error_response($env, 400, 'The "variables" field must be a JSON object');
114 }
115 16 my $operation_name = $payload->{operationName};
116 16 ▶ if (defined $operation_name && (ref $operation_name || $operation_name !~ /\S/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $operation_name and ref $operation_name || !($operation_name =~ /\S/u)) 0 16 16
Condition50%3/6
defined $operation_name and ref $operation_name || !($operation_name =~ /\S/u)
!ll&&!rl&&r
1240
ref $operation_name or not $operation_name =~ /\S/u
l!l&&r!l&&!r
004
MC/DC0%0/3
defined $operation_name and ref $operation_name || !($operation_name =~ /\S/u)
defined $operation_name ref $operation_name not $operation_name =~ /\S/u
Truth table50%2/4
A and B or not(C)
A= defined $operation_nameB= ref $operation_nameC= not $operation_name =~ /\S/u
ABCresult
0XX0
1000
11X1
1011
117 0 return _error_response($env, 400, 'The "operationName" field must be a non-empty string');
118 }
119  
120 16 my ($context, $on_stall) = $self->_request_context($env);
121  
122 16 my %exec_opts;
123 16 ▶ $exec_opts{operation_name} = $operation_name if defined $operation_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $operation_name 4 12 16
124 16 ▶ $exec_opts{variables} = $variables if defined $variables;
Branch: 1 branch
BranchTrueFalseTotal
if defined $variables 2 14 16
125 16 ▶ $exec_opts{context} = $context if defined $context;
Branch: 1 branch
BranchTrueFalseTotal
if defined $context 10 6 16
126 16 ▶ $exec_opts{root_value} = $self->{root_value} if defined $self->{root_value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"root_value"} 0 16 16
127 16 ▶ $exec_opts{on_stall} = $on_stall if defined $on_stall;
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_stall 10 6 16
128 16 ▶ $exec_opts{max_depth} = $self->{max_depth} if defined $self->{max_depth};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"max_depth"} 0 16 16
129 16 ▶ $exec_opts{max_nodes} = $self->{max_nodes} if defined $self->{max_nodes};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"max_nodes"} 0 16 16
130 16 ▶ $exec_opts{max_cost} = $self->{max_cost} if defined $self->{max_cost};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"max_cost"} 0 16 16
131 $exec_opts{default_list_size} = $self->{default_list_size}
132 16 ▶ if defined $self->{default_list_size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"default_list_size"} 0 16 16
133 $exec_opts{allow_introspection} = $self->{allow_introspection}
134 16 ▶ if defined $self->{allow_introspection};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"allow_introspection"} 1 15 16
135  
136 16 my ($json, $error) = do {
137 16 local $@;
138 16 my $out = eval { $self->{runtime}->execute_document_to_json($query, %exec_opts) };
139 16 ▶ $@ ? (undef, $@) : ($out, undef);
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 1 15 16
140 };
141 16 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 1 15 16
142 # Request errors (syntax, validation, input coercion) come back as an
143 # errors-only envelope, not an exception, so a die here is a server
144 # problem - async misconfiguration, scheduler deadlock, an internal
145 # bug. GraphQL over HTTP calls for a 5xx, and the details belong in
146 # logs, not the response.
147 1 warn "GraphQL::Houtou::PSGI: execution failed: $error";
148 1 return _error_response($env, 500, 'Internal server error');
149 }
150  
151 # An errors-only envelope means the request itself was invalid (no field
152 # ever executed): GraphQL over HTTP maps that to a 400. Request errors
153 # always serialize as {"errors":...} with no data key, so the prefix
154 # check avoids decoding the response we just encoded.
155 15 ▶ my $status = index($json, '{"errors":') == 0 ? 400 : 200;
Branch: 1 branch
BranchTrueFalseTotal
index($json, '{"errors":') == 0 ? : 4 11 15
156  
157 return [
158 15 $status,
159 [ 'Content-Type' => _response_content_type($env), 'Content-Length' => length $json ],
160 [ $json ],
161 ];
162 }
163  
164 16 ▶sub _request_context {
Subroutine _request_context: called CC=3 SCAR=11.2
165 16 my ($self, $env) = @_;
166 16 my $context = $self->{context};
167 16 ▶ if (ref $context eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $context eq "CODE") 10 6 16
168 # The builder runs once per request and may return the context alone or
169 # a (context, on_stall) pair - the natural shape when per-request
170 # DataLoaders live inside the context.
171 10 my ($built, $on_stall) = $context->($env);
172 10 ▶ return ($built, $on_stall // $self->{on_stall});
Condition33%1/3
$on_stall // $self->{"on_stall"}
l!l&&r!l&&!r
1000
MC/DC0%0/2
$on_stall // $self->{"on_stall"}
$on_stall $self->{"on_stall"}
Truth table33%1/3
A // B
A= $on_stallB= $self->{"on_stall"}
ABresult
1X1
011
000
173 }
174 6 return ($context, $self->{on_stall});
175 }
176  
177 # Reads the request body, enforcing $max_body against the bytes actually
178 # read (not just the client-supplied CONTENT_LENGTH, which can lie).
179 # Returns ($body, $too_large).
180 18 ▶sub _read_body {
Subroutine _read_body: called CC=7 SCAR=20.6
181 18 my ($env, $max_body) = @_;
182 18 ▶ my $input = $env->{'psgi.input'} or return ('', 0);
Branch: 1 branch
BranchTrueFalseTotal
my $input = $env->{"psgi.input"} or return "", 0 18 0 18
183 18 ▶ my $length = $env->{CONTENT_LENGTH} || 0;
Condition50%1/2
$env->{'CONTENT_LENGTH'} || 0
l!l
180
MC/DC0%0/1
$env->{'CONTENT_LENGTH'} || 0
$env->{'CONTENT_LENGTH'}
Truth table50%1/2
A
A= $env->{'CONTENT_LENGTH'}
Aresult
11
00
184 18 my $body = '';
185 18 ▶ while ($length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 18 18 36
186 18 my $read = $input->read($body, $length, length $body);
187 18 ▶ last if !$read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 0 18 18
188 18 $length -= $read;
189 18 ▶ return ($body, 1) if $max_body && length($body) > $max_body;
Branch: 1 branch
BranchTrueFalseTotal
if $max_body and length $body > $max_body 0 18 18
Condition33%1/3
$max_body and length $body > $max_body
!ll&&!rl&&r
0180
MC/DC0%0/2
$max_body and length $body > $max_body
$max_body length $body > $max_body
Truth table33%1/3
A and B
A= $max_bodyB= length $body > $max_body
ABresult
0X0
100
111
190 }
191 18 return ($body, 0);
192 }
193  
194 2 ▶sub _accepts_html {
Subroutine _accepts_html: called CC=3 SCAR=11.1
195 2 my ($env) = @_;
196 2 ▶ return (($env->{HTTP_ACCEPT} || '') =~ m{text/html}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($env->{'HTTP_ACCEPT'} || '') =~ m[text/html]u ? : 1 1 2
Condition50%1/2
$env->{'HTTP_ACCEPT'} || ''
l!l
20
MC/DC0%0/1
$env->{'HTTP_ACCEPT'} || ''
$env->{'HTTP_ACCEPT'}
Truth table50%1/2
A
A= $env->{'HTTP_ACCEPT'}
Aresult
11
00
197 }
198  
199 22 ▶sub _response_content_type {
Subroutine _response_content_type: called CC=3 SCAR=11.1
200 22 my ($env) = @_;
201 22 ▶ my $accept = $env->{HTTP_ACCEPT} || '';
Condition50%1/2
$env->{'HTTP_ACCEPT'} || ''
l!l
220
MC/DC0%0/1
$env->{'HTTP_ACCEPT'} || ''
$env->{'HTTP_ACCEPT'}
Truth table50%1/2
A
A= $env->{'HTTP_ACCEPT'}
Aresult
11
00
202 22 ▶ return $accept =~ m{application/graphql-response\+json}
Branch: 1 branch
BranchTrueFalseTotal
$accept =~ m[application/graphql-response\+json]u ? : 2 20 22
203 ? 'application/graphql-response+json; charset=utf-8'
204 : 'application/json; charset=utf-8';
205 }
206  
207 7 ▶sub _error_response {
Subroutine _error_response: called CC=2 SCAR=6.9
208 7 my ($env, $status, $message, $extra_headers) = @_;
209 7 my $json = $JSON->encode({ errors => [ { message => $message } ] });
210 return [
211 $status,
212 [
213 'Content-Type' => _response_content_type($env),
214 'Content-Length' => length $json,
215 7 ▶ @{ $extra_headers || [] },
Condition100%2/2
$extra_headers || []
l!l
25
MC/DC100%1/1
$extra_headers || []
$extra_headers
Truth table100%2/2
A
A= $extra_headers
Aresult
11
00
216 ],
217 [ $json ],
218 ];
219 }
220  
221 1 ▶sub _graphiql_response {
Subroutine _graphiql_response: called CC=2 SCAR=7.0
222 1 my ($self) = @_;
223 1 ▶ my $endpoint = $self->{graphiql_path} // '';
Condition50%1/2
$self->{"graphiql_path"} // ""
l!l
01
MC/DC0%0/1
$self->{"graphiql_path"} // ""
$self->{"graphiql_path"}
Truth table50%1/2
A
A= $self->{"graphiql_path"}
Aresult
11
00
224 1 my $html = <<"HTML";
225 <!DOCTYPE html>
226 <html lang="en">
227 <head>
228 <meta charset="utf-8" />
229 <title>GraphiQL - GraphQL::Houtou</title>
230 <style>html, body, #graphiql { height: 100%; margin: 0; }</style>
231 <link rel="stylesheet" href="https://esm.sh/graphiql\@5.2.4/dist/style.css" />
232 </head>
233 <body>
234 <div id="graphiql">Loading GraphiQL...</div>
235 <script type="importmap">
236 {
237 "imports": {
238 "react": "https://esm.sh/react\@19.2.7",
239 "react/": "https://esm.sh/react\@19.2.7/",
240 "react-dom": "https://esm.sh/react-dom\@19.2.7",
241 "react-dom/": "https://esm.sh/react-dom\@19.2.7/",
242 "graphiql": "https://esm.sh/graphiql\@5.2.4?standalone&external=react,react-dom,\@graphiql/react,graphql",
243 "graphiql/": "https://esm.sh/graphiql\@5.2.4/",
244 "\@graphiql/react": "https://esm.sh/\@graphiql/react\@0.37.7?standalone&external=react,react-dom,graphql,\@graphiql/toolkit,\@emotion/is-prop-valid",
245 "\@graphiql/toolkit": "https://esm.sh/\@graphiql/toolkit\@0.12.1?standalone&external=graphql",
246 "graphql": "https://esm.sh/graphql\@17.0.2",
247 "\@emotion/is-prop-valid": "data:text/javascript,"
248 }
249 }
250 </script>
251 <script type="module">
252 import React from 'react';
253 import ReactDOM from 'react-dom/client';
254 import { GraphiQL } from 'graphiql';
255 import { createGraphiQLFetcher } from '\@graphiql/toolkit';
256 import 'graphiql/setup-workers/esm.sh';
257 const fetcher = createGraphiQLFetcher({ url: '$endpoint' || window.location.pathname });
258 ReactDOM.createRoot(document.getElementById('graphiql')).render(
259 React.createElement(GraphiQL, { fetcher })
260 );
261 </script>
262 </body>
263 </html>
264 HTML
265 return [
266 1 200,
267 [ 'Content-Type' => 'text/html; charset=utf-8', 'Content-Length' => length $html ],
268 [ $html ],
269 ];
270 }
271  
272 3 1;
273 __END__
« blib/lib/GraphQL/Houtou/Role/HashMappable.pm ↑ Summary blib/lib/GraphQL/Houtou/Validation/DepthLimit.pm »