GraphQL/Filter.pm

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