GraphQL/Limits.pm

filter: Statementstmt 100% 104 / 104 Branchbran 91.6% 55 / 60 Conditioncond 67.1% 47 / 70 MC/DCmcdc 35.1% 13 / 37 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 81.7% 233 / 285 CC 65 SCAR 22.6
CC
65
Cov
88%
_cost
16.5
_factor
13.2
check
10.1
CRAP 9.6
« GraphQL.pm ↑ Summary GraphQL/Filter.pm »
1 package DBIO::GraphQL::Limits;
2 # ABSTRACT: Check a GraphQL document for cycles, depth and cost before it runs
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 our $VERSION = '0.900007';
7  
8 14 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # The document is walked before the engine sees it: the engine has no
11 # validation of its own, and the relationship fields point at each other, so
12 # one document can ask for an unbounded amount of work. Three rules, all
13 # checked on the parsed AST before the first SQL:
14 #
15 # * no fragment cycle (the engine has no NoFragmentCycles rule);
16 # * the selection depth after unfolding fragments is at most max_depth;
17 # * the cost is at most max_cost: every field costs 1, aliases one by one,
18 # and a field that yields rows -- a plural root query with its connection
19 # object, a has_many relationship field -- multiplies the cost of what is
20 # below it by its page size.
21  
22  
23 235 ▶sub check {
Subroutine check: called CC=10 SCAR=23.2
Pod documented
24 235 my ($class, %a) = @_;
25 235 my $ast = $a{ast};
26 235 my $policy = $a{policy};
27  
28 235 my %fragments = map { $_->{name} => $_ } grep { $_->{kind} eq 'fragment' } @$ast;
29 235 _no_fragment_cycles(\%fragments);
30  
31 231 my @operations = grep { $_->{kind} eq 'operation' } @$ast;
32 2 ▶ @operations = grep { ($_->{name} // '') eq $a{operation} } @operations
Condition50%1/2
$_->{"name"} // ""
l!l
20
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
Truth table50%1/2
A
A= $_->{"name"}
Aresult
11
00
33 231 ▶ if defined $a{operation} && length $a{operation};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a{"operation"} and length $a{"operation"} 1 230 231
Condition66%2/3
defined $a{"operation"} and length $a{"operation"}
!ll&&!rl&&r
23001
MC/DC50%1/2
defined $a{"operation"} and length $a{"operation"}
defined $a{"operation"} length $a{"operation"}
Truth table66%2/3
A and B
A= defined $a{"operation"}B= length $a{"operation"}
ABresult
0X0
100
111
34  
35 231 my $schema = $a{schema};
36 231 my %connection = _connection_types($schema);
37  
38 231 for my $op (@operations) {
39 231 ▶ my $type = $schema->${\ ($op->{operationType} || 'query')} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $schema->${\($op->{"operationType"} || "query");} or next 230 1 231
Condition100%2/2
$op->{'operationType'} || 'query'
l!l
111120
MC/DC100%1/1
$op->{'operationType'} || 'query'
$op->{'operationType'}
Truth table100%2/2
A
A= $op->{'operationType'}
Aresult
11
00
40 my $ctx = {
41 fragments => \%fragments,
42 policy => $policy,
43 schema => $schema,
44 connection => \%connection,
45 variables => $a{variables} || {},
46 op_vars => $op->{variables} || {},
47 230 ▶ };
Condition75%3/4
$a{'variables'} || {}
l!l
2300
$op->{'variables'} || {}
l!l
57173
MC/DC0%0/1
$a{'variables'} || {}
$a{'variables'}
MC/DC100%1/1
$op->{'variables'} || {}
$op->{'variables'}
Truth table50%1/2
A
A= $a{'variables'}
Aresult
11
00
Truth table100%2/2
A
A= $op->{'variables'}
Aresult
11
00
48 230 ▶ _cost($ctx, $op->{selections} || [], $type, 0);
Condition50%1/2
$op->{'selections'} || []
l!l
2300
MC/DC0%0/1
$op->{'selections'} || []
$op->{'selections'}
Truth table50%1/2
A
A= $op->{'selections'}
Aresult
11
00
49 }
50 212 return;
51 }
52  
53 # The connection objects of the plural root queries: the return type of every
54 # root field that takes both page and cursor.
55 231 ▶sub _connection_types {
Subroutine _connection_types: called CC=7 SCAR=20.4
56 231 my ($schema) = @_;
57 231 my %names;
58 231 my $fields = $schema->query->fields;
59 231 for my $def (values %$fields) {
60 1050 ▶ next unless ref $def->{args} eq 'HASH' && $def->{args}{page} && $def->{args}{cursor};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $def->{"args"} eq "HASH" and $def->{"args"}{"page"} and $def->{"args"}{"cursor"} 505 545 1050
Condition66%4/6
ref $def->{"args"} eq "HASH" and $def->{"args"}{"page"} and $def->{"args"}{"cursor"}
!ll&&!rl&&r
5050545
ref $def->{"args"} eq "HASH" and $def->{"args"}{"page"}
!ll&&!rl&&r
0505545
MC/DC33%1/3
ref $def->{"args"} eq "HASH" and $def->{"args"}{"page"} and $def->{"args"}{"cursor"}
ref $def->{"args"} eq "HASH" $def->{"args"}{"page"} $def->{"args"}{"cursor"}
Truth table50%2/4
A and B and C
A= ref $def->{"args"} eq "HASH"B= $def->{"args"}{"page"}C= $def->{"args"}{"cursor"}
ABCresult
0XX0
10X0
1100
1111
61 545 my $type = _unwrap($def->{type});
62 545 ▶ $names{ $type->name } = 1 if $type && $type->can('name');
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type->can("name") 545 0 545
Condition33%1/3
$type and $type->can("name")
!ll&&!rl&&r
00545
MC/DC0%0/2
$type and $type->can("name")
$type $type->can("name")
Truth table33%1/3
A and B
A= $typeB= $type->can("name")
ABresult
0X0
100
111
63 }
64 231 return %names;
65 }
66  
67 11675 ▶sub _unwrap {
Subroutine _unwrap: called CC=3 SCAR=11.0
68 11675 my ($type) = @_;
69 11675 ▶ $type = $type->of while $type && $type->can('of');
Branch: 1 branch
BranchTrueFalseTotal
$type and $type->can("of") and do { $type = $type->of } 192 11675 11867
Condition66%2/3
$type and $type->can("of")
!ll&&!rl&&r
011675192
MC/DC50%1/2
$type and $type->can("of")
$type $type->can("of")
Truth table66%2/3
A and B
A= $typeB= $type->can("of")
ABresult
0X0
100
111
70 11675 return $type;
71 }
72  
73 # --- fragment cycles -------------------------------------------------------
74  
75 17 ▶sub _spreads {
Subroutine _spreads: called CC=5 SCAR=16.1
76 17 my ($selections, $into) = @_;
77 17 for my $node (@$selections) {
78 21 ▶ push @$into, $node->{name} if ($node->{kind} // '') eq 'fragment_spread';
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"kind"} // "") eq "fragment_spread" 6 15 21
Condition50%1/2
$node->{"kind"} // ""
l!l
210
MC/DC0%0/1
$node->{"kind"} // ""
$node->{"kind"}
Truth table50%1/2
A
A= $node->{"kind"}
Aresult
11
00
79 21 ▶ _spreads($node->{selections}, $into) if $node->{selections};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"selections"} 7 14 21
80 }
81 17 return $into;
82 }
83  
84 235 ▶sub _no_fragment_cycles {
Subroutine _no_fragment_cycles: called CC=3 SCAR=11.0
85 235 my ($fragments) = @_;
86 235 ▶ my %edges = map { $_ => _spreads($fragments->{$_}{selections} || [], []) } keys %$fragments;
Condition50%1/2
$fragments->{$_}{'selections'} || []
l!l
100
MC/DC0%0/1
$fragments->{$_}{'selections'} || []
$fragments->{$_}{'selections'}
Truth table50%1/2
A
A= $fragments->{$_}{'selections'}
Aresult
11
00
87 235 my %state; # 1 = on the current path, 2 = done
88 15 ▶ my $visit; $visit = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
89 15 my ($name) = @_;
90 15 ▶ return if !$edges{$name} || ($state{$name} // 0) == 2;
Branch: 1 branch
BranchTrueFalseTotal
if not $edges{$name} or ($state{$name} // 0) == 2 1 14 15
Condition80%4/5
not $edges{$name} or ($state{$name} // 0) == 2
l!l&&r!l&&!r
0114
$state{$name} // 0
l!l
510
MC/DC50%1/2
not $edges{$name} or ($state{$name} // 0) == 2
not $edges{$name} $state{$name}
Truth table66%2/3
not(A) or B
A= not $edges{$name}B= $state{$name}
ABresult
1X1
011
000
91 14 ▶ die "DBIO::GraphQL: fragment cycle through '$name'\n" if ($state{$name} // 0) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if ($state{$name} // 0) == 1 4 10 14
Condition100%2/2
$state{$name} // 0
l!l
410
MC/DC100%1/1
$state{$name} // 0
$state{$name}
Truth table100%2/2
A
A= $state{$name}
Aresult
11
00
92 10 $state{$name} = 1;
93 10 $visit->($_) for @{ $edges{$name} };
94 5 $state{$name} = 2;
95 235 };
96 235 $visit->($_) for sort keys %edges;
97 231 undef $visit;
98 231 return;
99 }
100  
101 # --- depth and cost ----------------------------------------------------------
102  
103 # _cost($ctx, \@selections, $parent_type, $depth)
104 #
105 # The cost of a selection set, fragments unfolded at every spread. Dies as soon
106 # as the depth or the running cost passes its limit, so the walk stays as small
107 # as the budget.
108 10739 ▶sub _cost {
Subroutine _cost: called CC=16 SCAR=28.1
109 10739 my ($ctx, $selections, $type, $depth) = @_;
110 10739 my $policy = $ctx->{policy};
111 10739 my $total = 0;
112  
113 10739 for my $node (@$selections) {
114 21147 ▶ my $kind = $node->{kind} // '';
Condition50%1/2
$node->{"kind"} // ""
l!l
211470
MC/DC0%0/1
$node->{"kind"} // ""
$node->{"kind"}
Truth table50%1/2
A
A= $node->{"kind"}
Aresult
11
00
115  
116 21147 ▶ if ($kind eq 'fragment_spread') {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind eq 'fragment_spread') { } 10006 11141 21147
elsif ($kind eq 'inline_fragment') { } 3 11138 11141
elsif ($kind eq 'field') { } 11138 0 11138
117 10006 ▶ my $fragment = $ctx->{fragments}{ $node->{name} } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $fragment = $ctx->{"fragments"}{$node->{"name"}} or next 10006 0 10006
118 10006 ▶ $total += _cost($ctx, $fragment->{selections} || [], $type, $depth);
Condition50%1/2
$fragment->{'selections'} || []
l!l
100060
MC/DC0%0/1
$fragment->{'selections'} || []
$fragment->{'selections'}
Truth table50%1/2
A
A= $fragment->{'selections'}
Aresult
11
00
119 }
120 elsif ($kind eq 'inline_fragment') {
121 3 ▶ $total += _cost($ctx, $node->{selections} || [], $type, $depth);
Condition50%1/2
$node->{'selections'} || []
l!l
30
MC/DC0%0/1
$node->{'selections'} || []
$node->{'selections'}
Truth table50%1/2
A
A= $node->{'selections'}
Aresult
11
00
122 }
123 elsif ($kind eq 'field') {
124 11138 my $level = $depth + 1;
125 11138 ▶ die "DBIO::GraphQL: query depth exceeds max_depth (" . $policy->max_depth . ")\n"
Branch: 1 branch
BranchTrueFalseTotal
if $level > $policy->max_depth 4 11134 11138
126 if $level > $policy->max_depth;
127  
128 11134 ▶ my $def = $type && $type->can('fields') ? $type->fields->{ $node->{name} } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$type && $type->can('fields') ? : 11132 2 11134
Condition66%2/3
$type and $type->can("fields")
!ll&&!rl&&r
2011132
MC/DC50%1/2
$type and $type->can("fields")
$type $type->can("fields")
Truth table66%2/3
A and B
A= $typeB= $type->can("fields")
ABresult
0X0
100
111
129 11134 ▶ my $inner = $def ? _unwrap($def->{type}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$def ? : 11130 4 11134
130 11134 ▶ my $factor = $def ? _factor($ctx, $node, $def, $type) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$def ? : 11130 4 11134
131 my $below = $node->{selections}
132 11130 ▶ ? _cost($ctx, $node->{selections}, $inner, $level)
Branch: 1 branch
BranchTrueFalseTotal
$node->{'selections'} ? : 500 10630 11130
133 : 0;
134 11109 $total += 1 + $factor * $below;
135 }
136  
137 21114 ▶ die "DBIO::GraphQL: query cost exceeds max_cost (" . $policy->max_cost . ")\n"
Branch: 1 branch
BranchTrueFalseTotal
if $total > $policy->max_cost 11 21103 21114
138 if $total > $policy->max_cost;
139 }
140 10695 return $total;
141 }
142  
143 # How many rows a field yields, per parent row: the page size of a plural root
144 # query, default_rows for a has_many field, 1 for everything else.
145 11130 ▶sub _factor {
Subroutine _factor: called CC=13 SCAR=25.8
146 11130 my ($ctx, $node, $def, $parent) = @_;
147 11130 my $policy = $ctx->{policy};
148  
149 11130 ▶ if (ref $def->{args} eq 'HASH' && $def->{args}{page} && $def->{args}{cursor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $def->{"args"} eq "HASH" and $def->{"args"}{"page"} and $def->{"args"}{"cursor"}) 176 10954 11130
Condition83%5/6
ref $def->{"args"} eq "HASH" and $def->{"args"}{"page"} and $def->{"args"}{"cursor"}
!ll&&!rl&&r
109540176
ref $def->{"args"} eq "HASH" and $def->{"args"}{"page"}
!ll&&!rl&&r
10822132176
MC/DC66%2/3
ref $def->{"args"} eq "HASH" and $def->{"args"}{"page"} and $def->{"args"}{"cursor"}
ref $def->{"args"} eq "HASH" $def->{"args"}{"page"} $def->{"args"}{"cursor"}
Truth table75%3/4
A and B and C
A= ref $def->{"args"} eq "HASH"B= $def->{"args"}{"page"}C= $def->{"args"}{"cursor"}
ABCresult
0XX0
10X0
1100
1111
150 176 ▶ my $args = $node->{arguments} || {};
Condition100%2/2
$node->{'arguments'} || {}
l!l
11858
MC/DC100%1/1
$node->{'arguments'} || {}
$node->{'arguments'}
Truth table100%2/2
A
A= $node->{'arguments'}
Aresult
11
00
151 176 my $cursor = _value($ctx, $args->{cursor});
152 176 ▶ if (ref $cursor eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cursor eq "HASH") 65 111 176
153 65 return _rows(_value($ctx, $cursor->{first}), 10, $policy, 'first');
154 }
155 111 my $page = _value($ctx, $args->{page});
156 111 ▶ if (ref $page eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $page eq "HASH") 31 80 111
157 31 return _rows(_value($ctx, $page->{take}), 10, $policy, 'take');
158 }
159 80 return $policy->default_rows;
160 }
161  
162 10954 my $type = $def->{type};
163 10954 ▶ $type = $type->of while $type && $type->can('of') && !$type->isa('GraphQL::Type::List');
Branch: 1 branch
BranchTrueFalseTotal
$type and $type->can("of") and not $type->isa("GraphQL::Type::List") and do { $type = $type->of } 0 10954 10954
Condition66%4/6
$type and $type->can("of") and not $type->isa("GraphQL::Type::List")
!ll&&!rl&&r
107621920
$type and $type->can("of")
!ll&&!rl&&r
010762192
MC/DC0%0/3
$type and $type->can("of") and not $type->isa("GraphQL::Type::List")
$type $type->can("of") not $type->isa("GraphQL::Type::List")
Truth table50%2/4
A and B and not(C)
A= $typeB= $type->can("of")C= not $type->isa("GraphQL::Type::List")
ABCresult
0XX0
10X0
1100
1111
164 return $policy->default_rows
165 if $type && $type->isa('GraphQL::Type::List')
166 10954 ▶ && !$ctx->{connection}{ $parent->name };
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type->isa("GraphQL::Type::List") and not $ctx->{"connection"}{$parent->name} 26 10928 10954
Condition83%5/6
$type and $type->isa("GraphQL::Type::List") and not $ctx->{"connection"}{$parent->name}
!ll&&!rl&&r
1076216626
$type and $type->isa("GraphQL::Type::List")
!ll&&!rl&&r
010762192
MC/DC66%2/3
$type and $type->isa("GraphQL::Type::List") and not $ctx->{"connection"}{$parent->name}
$type $type->isa("GraphQL::Type::List") not $ctx->{"connection"}{$parent->name}
Truth table75%3/4
A and B and not(C)
A= $typeB= $type->isa("GraphQL::Type::List")C= not $ctx->{"connection"}{$parent->name}
ABCresult
0XX0
10X0
1100
1111
167 10928 return 1;
168 }
169  
170 # A given row count, or the resolver's own default when the argument is absent.
171 96 ▶sub _rows {
Subroutine _rows: called CC=6 SCAR=18.3
172 96 my ($value, $absent, $policy, $what) = @_;
173 96 ▶ return $absent unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 7 89 96
174 89 ▶ return $policy->default_rows
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $value and &Scalar::Util::looks_like_number($value) 0 89 89
Condition33%1/3
not ref $value and &Scalar::Util::looks_like_number($value)
!ll&&!rl&&r
0089
MC/DC0%0/2
not ref $value and &Scalar::Util::looks_like_number($value)
not ref $value &Scalar::Util::looks_like_number($value)
Truth table33%1/3
not(A) and B
A= not ref $valueB= &Scalar::Util::looks_like_number($value)
ABresult
0X0
100
111
175 unless !ref $value && Scalar::Util::looks_like_number($value);
176 89 ▶ die "DBIO::GraphQL: $what $value exceeds max_rows (" . $policy->max_rows . ")\n"
Branch: 1 branch
BranchTrueFalseTotal
if $value > $policy->max_rows 4 85 89
177 if $value > $policy->max_rows;
178 85 ▶ return $value >= 1 ? $value : 1;
Branch: 1 branch
BranchTrueFalseTotal
$value >= 1 ? : 80 5 85
179 }
180  
181 # An argument value with variables resolved: the given value, else the
182 # variable's default in the operation.
183 383 ▶sub _value {
Subroutine _value: called CC=4 SCAR=13.9
184 383 my ($ctx, $value) = @_;
185 383 ▶ if (ref $value eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "SCALAR") 6 377 383
186 6 my $name = $$value;
187 return exists $ctx->{variables}{$name}
188 ? $ctx->{variables}{$name}
189 6 ▶ : ($ctx->{op_vars}{$name} || {})->{default_value};
Branch: 1 branch
BranchTrueFalseTotal
exists $ctx->{'variables'}{$name} ? : 5 1 6
Condition50%1/2
$ctx->{'op_vars'}{$name} || {}
l!l
10
MC/DC0%0/1
$ctx->{'op_vars'}{$name} || {}
$ctx->{'op_vars'}{$name}
Truth table50%1/2
A
A= $ctx->{'op_vars'}{$name}
Aresult
11
00
190 }
191 377 return $value;
192 }
193  
194 14 1;
195  
196 __END__
« GraphQL.pm ↑ Summary GraphQL/Filter.pm »