GraphQL/Filter.pm

filter: Statementstmt 94.7% 109 / 115 Branchbran 68.7% 33 / 48 Conditioncond 75.0% 18 / 24 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 85.7% 18 / 21 Podpod 0.0% 0 / 3 totaltotal 81.7% 184 / 225 CC 36 SCAR 21.7
CC
36
Cov
86%
_compile_column
17.5
_compile
12.6
to_search
4.1
CRAP 8.8
« GraphQL/Limits.pm ↑ Summary GraphQL/Relationship.pm »
1 package DBIO::GraphQL::Filter;
2 # ABSTRACT: Per-source GraphQL filter InputObject with adapter seam
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 base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use DBIO::GraphQL::Error qw(client_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use GraphQL::Type::InputObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use GraphQL::Type::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use GraphQL::Type::Scalar qw($Int $String $Float $Boolean);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶use DBIO::GraphQL::ScalarMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use DBIO::GraphQL::Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Adapter interface — two concrete adapters live in this distribution:
18 #
19 # DBIO::GraphQL::Filter::Search - per-source nested-DBIO-style filter
20 # (the default)
21 # DBIO::GraphQL::Filter::Null - no-op; exposes an empty filter type
22 #
23 # Subclasses must implement (or inherit) type_for($moniker) and
24 # to_search($args, $moniker). The seam is real because Null and Search
25 # produce different GraphQL surfaces AND different DBIO search
26 # conditions for the same source moniker.
27  
28 # schema is the connected DBIO::Schema; required at construction.
29 # _type_cache is a per-instance cache of { $moniker => $filter_type }.
30 # Shared scalar InputObjects (IntFilter, StringFilter, ...) are cached
31 # at the class level (see %_SCALAR_INPUT) so two filter instances cannot
32 # accidentally produce two IntFilter types with the same name.
33 14 __PACKAGE__->mk_group_accessors(simple => qw(schema policy _type_cache));
34  
35 81 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
36 81 my ($class, %args) = @_;
37 die "DBIO::GraphQL::Filter: 'schema' is required\n"
38 81 ▶ unless exists $args{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"schema"} 0 81 81
39 81 my $self = bless {}, $class;
40 81 $self->schema($args{schema});
41 $self->policy($args{policy}
42 81 ▶ // DBIO::GraphQL::Policy->new(schema => $args{schema}));
Condition66%2/3
$args{"policy"} // "DBIO::GraphQL::Policy"->new("schema", $args{"schema"})
l!l&&r!l&&!r
7920
MC/DC0%0/2
$args{"policy"} // "DBIO::GraphQL::Policy"->new("schema", $args{"schema"})
$args{"policy"} "DBIO::GraphQL::Policy"->new("schema", $args{"schema"})
Truth table66%2/3
A // B
A= $args{"policy"}B= "DBIO::GraphQL::Policy"->new("schema", $args{"schema"})
ABresult
1X1
011
000
43 81 $self->_type_cache({});
44 81 return $self;
45 }
46  
47 # Class-level cache of shared scalar InputObjects. Keyed by GraphQL
48 # scalar name (Int/Float/String/Boolean).
49 14 my %_SCALAR_INPUT;
50  
51 # Operator spec per GraphQL scalar. Each entry is a list of operator
52 # descriptors with:
53 # name => 'gt' (GraphQL field name)
54 # type => $GraphQL::Type::Scalar (input arg type)
55 # op => '>' | 'like' | '-in' | '_null' (DBIO op key, or special)
56 # wrap => sub { ... } (optional: value transformer)
57 #
58 # The 'wrap' lets us express 'contains' / 'startsWith' / 'endsWith'
59 # without inventing new DBIO operators: we just wrap the value with
60 # SQL wildcards before handing it to { like => ... }.
61 60 ▶sub _ops_for {
Subroutine _ops_for: called CC=1 SCAR=1.2
62 return {
63 Int => [
64 { name => 'eq', type => $Int, op => '=' },
65 { name => 'not', type => $Int, op => '!=' },
66 { name => 'gt', type => $Int, op => '>' },
67 { name => 'gte', type => $Int, op => '>=' },
68 { name => 'lt', type => $Int, op => '<' },
69 { name => 'lte', type => $Int, op => '<=' },
70 { name => 'in', type => GraphQL::Type::List->new(of => $Int), op => '-in' },
71 { name => 'isNull', type => $Boolean, op => '_null' },
72 ],
73 Float => [
74 { name => 'eq', type => $Float, op => '=' },
75 { name => 'not', type => $Float, op => '!=' },
76 { name => 'gt', type => $Float, op => '>' },
77 { name => 'gte', type => $Float, op => '>=' },
78 { name => 'lt', type => $Float, op => '<' },
79 { name => 'lte', type => $Float, op => '<=' },
80 { name => 'in', type => GraphQL::Type::List->new(of => $Float), op => '-in' },
81 { name => 'isNull', type => $Boolean, op => '_null' },
82 ],
83 String => [
84 { name => 'eq', type => $String, op => '=' },
85 { name => 'not', type => $String, op => '!=' },
86 { name => 'like', type => $String, op => 'like' },
87 { name => 'contains', type => $String, op => 'like',
88 1 ▶ wrap => sub { '%' . $_[0] . '%' } },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 { name => 'startsWith', type => $String, op => 'like',
90 0 ▶ wrap => sub { $_[0] . '%' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 { name => 'endsWith', type => $String, op => 'like',
92 0 ▶ wrap => sub { '%' . $_[0] } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 60 { name => 'in', type => GraphQL::Type::List->new(of => $String), op => '-in' },
94 { name => 'isNull', type => $Boolean, op => '_null' },
95 ],
96 Boolean => [
97 { name => 'eq', type => $Boolean, op => '=' },
98 { name => 'not', type => $Boolean, op => '!=' },
99 { name => 'isNull', type => $Boolean, op => '_null' },
100 ],
101 };
102 }
103  
104 # Return the shared scalar InputObject (e.g. IntFilter) for a given
105 # GraphQL scalar name. Memoized at the class level.
106 647 ▶sub _scalar_input {
Subroutine _scalar_input: called CC=3 SCAR=11.2
107 647 my ($class, $scalar_name) = @_;
108 647 ▶ return $_SCALAR_INPUT{$scalar_name} ||= do {
Condition66%2/3
$_SCALAR_INPUT{$scalar_name} ||= do { my $ops = $class->_ops_for->{$scalar_name} or die "DBIO::GraphQL::Filter: no operators for scalar '${scalar_name}'\n"; my(%fields) = map({$_->{"name"}, {"type", $_->{"type"}};} @$ops); "GraphQL::Type::InputObject"->new("name", "${scalar_name}Filter", "fields", \%fields) }
l!l&&r!l&&!r
609380
MC/DC0%0/2
$_SCALAR_INPUT{$scalar_name} ||= do { my $ops = $class->_ops_for->{$scalar_name} or die "DBIO::GraphQL::Filter: no operators for scalar '${scalar_name}'\n"; my(%fields) = map({$_->{"name"}, {"type", $_->{"type"}};} @$ops); "GraphQL::Type::InputObject"->new("name", "${scalar_name}Filter", "fields", \%fields) }
$_SCALAR_INPUT{$scalar_name} do { my $ops = $class->_ops_for->{$scalar_name} or die "DBIO::GraphQL::Filter: no operators for scalar '${scalar_name}'\n"; my(%fields) = map({$_->{"name"}, {"type", $_->{"type"}};} @$ops); "GraphQL::Type::InputObject"->new("name", "${scalar_name}Filter", "fields", \%fields) }
Truth table66%2/3
A ||= B
A= $_SCALAR_INPUT{$scalar_name}B= do { my $ops = $class->_ops_for->{$scalar_name} or die "DBIO::GraphQL::Filter: no operators for scalar '${scalar_name}'\n"; my(%fields) = map({$_->{"name"}, {"type", $_->{"type"}};} @$ops); "GraphQL::Type::InputObject"->new("name", "${scalar_name}Filter", "fields", \%fields) }
ABresult
1X1
011
000
109 38 ▶ my $ops = $class->_ops_for->{$scalar_name}
Branch: 1 branch
BranchTrueFalseTotal
my $ops = $class->_ops_for->{$scalar_name} or die "DBIO::GraphQL::Filter: no operators for scalar '${scalar_name}'\n" 38 0 38
110 or die "DBIO::GraphQL::Filter: no operators for scalar '$scalar_name'\n";
111 38 my %fields = map { $_->{name} => { type => $_->{type} } } @$ops;
112 38 GraphQL::Type::InputObject->new(
113 name => "${scalar_name}Filter",
114 fields => \%fields,
115 );
116 };
117 }
118  
119 # Build the per-source filter InputObject. The forward-declaration
120 # trick lets AND/OR reference the same type recursively.
121 176 ▶sub type_for {
Subroutine type_for: called CC=3 SCAR=11.0
Pod undocumented
122 176 my ($self, $moniker) = @_;
123 176 my $cache = $self->_type_cache;
124 176 ▶ return $cache->{$moniker} if $cache->{$moniker};
Branch: 1 branch
BranchTrueFalseTotal
if $cache->{$moniker} 0 176 176
125  
126 176 my $source = $self->schema->source($moniker);
127  
128 my $filter_type = GraphQL::Type::InputObject->new(
129 name => "${moniker}Filter",
130 0 ▶ fields => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
131 176 );
132  
133 176 my %fields;
134 176 for my $col ($self->policy->visible_columns($moniker)) {
135 647 my $scalar = DBIO::GraphQL::ScalarMap::for_column($source, $col);
136 647 $fields{$col} = { type => $self->_scalar_input($scalar->name) };
137 }
138  
139 176 $fields{AND} = { type => GraphQL::Type::List->new(of => $filter_type) };
140 176 $fields{OR} = { type => GraphQL::Type::List->new(of => $filter_type) };
141  
142 176 ▶ $filter_type->{fields} = sub { \%fields };
Subroutine __ANON__: called CC=1 SCAR=0.0
143  
144 176 return $cache->{$moniker} = $filter_type;
145 }
146  
147 # Translate a filter args hashref (as received from a GraphQL resolver)
148 # into a DBIO search condition. Returns undef for empty / inert input.
149 #
150 # The output shape mirrors the GraphQL input shape:
151 #
152 # { name: { like: "%Perl%" }, author_id: { gt: 3 } }
153 # => { name => { like => '%Perl%' }, author_id => { '>' => 3 } }
154 #
155 # AND / OR propagate naturally: each combinator becomes a -and / -or
156 # at the top level of the resulting hashref.
157 131 ▶sub to_search {
Subroutine to_search: called CC=4 SCAR=14.0
Pod undocumented
158 131 my ($self, $args, $moniker) = @_;
159 131 ▶ return undef unless $args && ref $args eq 'HASH' && %$args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args and ref $args eq "HASH" and %$args 113 18 131
Condition66%4/6
$args and ref $args eq "HASH" and %$args
!ll&&!rl&&r
113018
$args and ref $args eq "HASH"
!ll&&!rl&&r
113018
MC/DC33%1/3
$args and ref $args eq "HASH" and %$args
$args ref $args eq "HASH" %$args
Truth table50%2/4
A and B and C
A= $argsB= ref $args eq "HASH"C= %$args
ABCresult
0XX0
10X0
1100
1111
160 18 my $source = $self->schema->source($moniker);
161 18 return $self->_compile($args, $source);
162 }
163  
164 29 ▶sub _compile {
Subroutine _compile: called CC=12 SCAR=25.3
165 29 my ($self, $filter, $source) = @_;
166 29 my @clauses;
167  
168 29 ▶ if (my $and = $filter->{AND}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $and = $filter->{"AND"}) 3 26 29
169 3 my @sub = map { $self->_compile($_, $source) } @$and;
170 2 my @defined_sub = grep { defined } @sub;
171 2 ▶ push @clauses, { -and => \@defined_sub } if @defined_sub;
Branch: 1 branch
BranchTrueFalseTotal
if @defined_sub 2 0 2
172 }
173 28 ▶ if (my $or = $filter->{OR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $or = $filter->{"OR"}) 3 25 28
174 3 my @sub = map { $self->_compile($_, $source) } @$or;
175 2 my @defined_sub = grep { defined } @sub;
176 2 ▶ push @clauses, { -or => \@defined_sub } if @defined_sub;
Branch: 1 branch
BranchTrueFalseTotal
if @defined_sub 2 0 2
177 }
178  
179 27 for my $col (keys %$filter) {
180 27 ▶ next if $col eq 'AND' || $col eq 'OR';
Branch: 1 branch
BranchTrueFalseTotal
if $col eq "AND" or $col eq "OR" 4 23 27
Condition100%3/3
$col eq "AND" or $col eq "OR"
l!l&&r!l&&!r
2223
MC/DC100%2/2
$col eq "AND" or $col eq "OR"
$col eq "AND" $col eq "OR"
Truth table100%3/3
A or B
A= $col eq "AND"B= $col eq "OR"
ABresult
1X1
011
000
181 23 my $col_filter = $filter->{$col};
182 23 ▶ next unless ref $col_filter eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $col_filter eq "HASH" 0 23 23
183 23 my $clause = $self->_compile_column($col, $col_filter, $source);
184 19 ▶ push @clauses, $clause if $clause;
Branch: 1 branch
BranchTrueFalseTotal
if $clause 19 0 19
185 }
186  
187 23 ▶ return undef unless @clauses;
Branch: 1 branch
BranchTrueFalseTotal
unless @clauses 0 23 23
188 23 ▶ return $clauses[0] if @clauses == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @clauses == 1 23 0 23
189 0 return { -and => \@clauses };
190 }
191  
192 23 ▶sub _compile_column {
Subroutine _compile_column: called CC=16 SCAR=28.6
193 23 my ($self, $col, $ops, $source) = @_;
194 23 ▶ unless ($source->has_column($col)
Branch: 1 branch
BranchTrueFalseTotal
unless ($source->has_column($col) and not $self->policy->hidden($source->source_name, $col)) 1 22 23
Condition66%2/3
$source->has_column($col) and not $self->policy->hidden($source->source_name, $col)
!ll&&!rl&&r
1022
MC/DC50%1/2
$source->has_column($col) and not $self->policy->hidden($source->source_name, $col)
$source->has_column($col) not $self->policy->hidden($source->source_name, $col)
Truth table66%2/3
A and not(B)
A= $source->has_column($col)B= not $self->policy->hidden($source->source_name, $col)
ABresult
0X0
100
111
195 && !$self->policy->hidden($source->source_name, $col)) {
196 1 die client_error "DBIO::GraphQL::Filter: unknown column '$col' on source '"
197 . $source->source_name . "'\n";
198 }
199  
200 22 my $scalar_name = DBIO::GraphQL::ScalarMap::for_column($source, $col)->name;
201 22 ▶ my $spec_list = $self->_ops_for->{$scalar_name}
Branch: 1 branch
BranchTrueFalseTotal
my $spec_list = $self->_ops_for->{$scalar_name} or die "DBIO::GraphQL::Filter: no operators for scalar '${scalar_name}'\n" 22 0 22
202 or die "DBIO::GraphQL::Filter: no operators for scalar '$scalar_name'\n";
203 22 my %op_map = map { $_->{name} => $_ } @$spec_list;
204  
205 22 my @parts;
206 22 for my $op_name (keys %$ops) {
207 22 ▶ next unless defined $ops->{$op_name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ops->{$op_name} 0 22 22
208 22 ▶ my $spec = $op_map{$op_name}
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $op_map{$op_name} or die client_error("DBIO::GraphQL::Filter: unknown operator '${op_name}' on column " . "'${col}' (scalar: $scalar_name)\n") 22 0 22
209 or die client_error "DBIO::GraphQL::Filter: unknown operator '$op_name' on column "
210 . "'$col' (scalar: $scalar_name)\n";
211  
212 22 my $dbio_op = $spec->{op};
213 22 my $val = $ops->{$op_name};
214  
215 22 ▶ if ($dbio_op eq '_null') {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbio_op eq '_null') { } 1 21 22
elsif ($op_name eq 'eq') { } 11 10 21
216 # isNull: true => col => undef (IS NULL)
217 # isNull: false => col => { '!=' => undef } (IS NOT NULL)
218 1 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 1 0 1
219 1 push @parts, { $col => undef };
220 } else {
221 0 push @parts, { $col => { '!=' => undef } };
222 }
223 }
224 elsif ($op_name eq 'eq') {
225 # eq: emit the bare "col => val" form (DBIO's implicit '=')
226 11 push @parts, { $col => $val };
227 }
228 else {
229 10 ▶ if ($dbio_op eq '-in' && ref $val eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if ($dbio_op eq "-in" and ref $val eq "ARRAY" and @$val > $self->policy->max_in) 3 7 10
Condition83%5/6
$dbio_op eq "-in" and ref $val eq "ARRAY" and @$val > $self->policy->max_in
!ll&&!rl&&r
523
$dbio_op eq "-in" and ref $val eq "ARRAY"
!ll&&!rl&&r
505
MC/DC66%2/3
$dbio_op eq "-in" and ref $val eq "ARRAY" and @$val > $self->policy->max_in
$dbio_op eq "-in" ref $val eq "ARRAY" @$val > $self->policy->max_in
Truth table75%3/4
A and B and C
A= $dbio_op eq "-in"B= ref $val eq "ARRAY"C= @$val > $self->policy->max_in
ABCresult
0XX0
10X0
1100
1111
230 && @$val > $self->policy->max_in) {
231 3 die client_error "DBIO::GraphQL::Filter: 'in' on column '$col' has " . scalar(@$val)
232 . " values, more than max_in (" . $self->policy->max_in . ")\n";
233 }
234 7 ▶ my $final_val = $spec->{wrap} ? $spec->{wrap}->($val) : $val;
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'wrap'} ? : 1 6 7
235 7 push @parts, { $col => { $dbio_op => $final_val } };
236 }
237 }
238  
239 19 ▶ return undef unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 19 19
240 19 ▶ return $parts[0] if @parts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 19 0 19
241 0 return { -and => \@parts };
242 }
243  
244 14 1;
245  
246 __END__
« GraphQL/Limits.pm ↑ Summary GraphQL/Relationship.pm »