GraphQL/Mutation.pm

filter: Statementstmt 100% 160 / 160 Branchbran 84.4% 49 / 58 Conditioncond 65.3% 17 / 26 MC/DCmcdc 47.0% 8 / 17 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 5 totaltotal 88.8% 255 / 287 CC 44 SCAR 17.1
CC
44
Cov
93%
__ANON__
9.2
_resolve_row
8.0
_apply_create_restriction
7.2
CRAP 5.5
« GraphQL/Relationship.pm ↑ Summary GraphQL/Error.pm »
1 package DBIO::GraphQL::Mutation;
2 # ABSTRACT: Build createX / updateX / deleteX GraphQL field per source
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 GraphQL::Type::NonNull;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use GraphQL::Type::Scalar qw($Boolean);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 14 ▶use DBIO::GraphQL::ScalarMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use DBIO::GraphQL::Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use DBIO::GraphQL::Error qw(client_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # fields_for($source, $moniker, $gql_type) returns:
17 #
18 # {
19 # 'create' . ucfirst($moniker) => { type, args, description, resolve },
20 # 'update' . ucfirst($moniker) => { ... },
21 # 'delete' . ucfirst($moniker) => { ... },
22 # }
23 #
24 # Each builder is its own method (build_create / build_update /
25 # build_delete) so subclasses can override a single kind without
26 # rewriting the rest. This is the seam that future SoftDelete / Audit
27 # variants plug into.
28  
29 # schema is the connected DBIO::Schema; required at construction.
30 14 __PACKAGE__->mk_group_accessors(simple => qw(schema policy));
31  
32 79 ▶sub new {
Subroutine new: called CC=3 SCAR=11.6
Pod undocumented
33 79 my ($class, %args) = @_;
34 die "DBIO::GraphQL::Mutation: 'schema' is required\n"
35 79 ▶ unless exists $args{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"schema"} 0 79 79
36 79 my $self = bless {}, $class;
37 79 $self->schema($args{schema});
38 $self->policy($args{policy}
39 79 ▶ // DBIO::GraphQL::Policy->new(schema => $args{schema}));
Condition33%1/3
$args{"policy"} // "DBIO::GraphQL::Policy"->new("schema", $args{"schema"})
l!l&&r!l&&!r
7900
MC/DC0%0/2
$args{"policy"} // "DBIO::GraphQL::Policy"->new("schema", $args{"schema"})
$args{"policy"} "DBIO::GraphQL::Policy"->new("schema", $args{"schema"})
Truth table33%1/3
A // B
A= $args{"policy"}B= "DBIO::GraphQL::Policy"->new("schema", $args{"schema"})
ABresult
1X1
011
000
40 79 return $self;
41 }
42  
43 76 ▶sub fields_for {
Subroutine fields_for: called CC=2 SCAR=6.9
Pod undocumented
44 76 my ($self, $source, $moniker, $gql_type) = @_;
45 76 my %fields = (
46 'create' . ucfirst($moniker) => $self->build_create($source, $moniker, $gql_type),
47 );
48 # A source without a primary key has no row identity to update or delete
49 # by: only createX is generated for it.
50 76 ▶ if ($source->primary_columns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->primary_columns) 74 2 76
51 74 $fields{'update' . ucfirst($moniker)} =
52 $self->build_update($source, $moniker, $gql_type);
53 74 $fields{'delete' . ucfirst($moniker)} =
54 $self->build_delete($source, $moniker);
55 }
56 76 return \%fields;
57 }
58  
59 810 ▶sub _scalar_for {
Subroutine _scalar_for: called CC=1 SCAR=0.0
60 810 my ($self, $source, $col) = @_;
61 810 return DBIO::GraphQL::ScalarMap::for_column($source, $col);
62 }
63  
64 334 ▶sub _col_is_required {
Subroutine _col_is_required: called CC=4 SCAR=13.9
65 334 my ($self, $source, $col) = @_;
66 334 my $info = $source->column_info($col);
67 334 ▶ return 0 if $info->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_auto_increment"} 68 266 334
68 266 ▶ return 0 if defined $info->{default_value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"default_value"} 65 201 266
69 201 ▶ return 0 if $info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_nullable"} 89 112 201
70 112 return 1;
71 }
72  
73 # createX - all columns are accepted as args; required columns (not
74 # auto-inc, no default, not nullable) are wrapped in NonNull.
75 76 ▶sub build_create {
Subroutine build_create: called CC=3 SCAR=11.0
Pod undocumented
76 76 my ($self, $source, $moniker, $gql_type) = @_;
77 76 my %args;
78 76 for my $col ($self->policy->writable_columns($moniker)) {
79 334 my $scalar = $self->_scalar_for($source, $col);
80 334 ▶ $args{$col} = {
Branch: 1 branch
BranchTrueFalseTotal
$self->_col_is_required($source, $col) ? : 112 222 334
81 type => $self->_col_is_required($source, $col)
82 ? GraphQL::Type::NonNull->new(of => $scalar)
83 : $scalar,
84 };
85 }
86  
87 return {
88 type => $gql_type,
89 args => \%args,
90 description => "Insert a new $moniker row. Required columns are marked non-null.",
91 17 ▶ resolve => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
92 17 my ($root, $args, $ctx) = @_;
93 17 my $restriction = $self->policy->authorize('create', $moniker, $args, $ctx);
94 34 my %data = map { $_ => $args->{$_} }
95 16 grep { defined $args->{$_} } keys %$args;
96 16 ▶ $self->_apply_create_restriction(\%data, $restriction) if $restriction;
Branch: 1 branch
BranchTrueFalseTotal
if $restriction 2 14 16
97 15 my $row = eval { $ctx->resultset($moniker)->create(\%data) };
98 15 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 8 7 15
99 7 return { $row->get_columns };
100 },
101 76 };
102 }
103  
104 # updateX - lookup by PK or any unique constraint; data args are all
105 # non-key columns, all optional (sparse update: only defined args written).
106 74 ▶sub build_update {
Subroutine build_update: called CC=1 SCAR=0.0
Pod undocumented
107 74 my ($self, $source, $moniker, $gql_type) = @_;
108 74 my @pk_cols = $source->primary_columns;
109 74 my %is_pk = map { $_ => 1 } @pk_cols;
110 74 my @data_cols = grep { !$is_pk{$_} } $self->policy->writable_columns($moniker);
111 74 my %is_data = map { $_ => 1 } @data_cols;
112  
113 74 my %lookup_args = %{ $self->_build_lookup_args($source, $moniker) };
114 my %data_args = map {
115 74 $_ => { type => $self->_scalar_for($source, $_) }
116 } @data_cols;
117  
118 return {
119 type => $gql_type,
120 args => { %lookup_args, %data_args },
121 description => "Update an existing $moniker row identified by its primary key "
122 . "or by one complete unique constraint, never both and never "
123 . "several constraints. Non-key columns are optional; only supplied ones are updated.",
124 14 ▶ resolve => sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
125 14 my ($root, $args, $ctx) = @_;
126 14 my $restriction = $self->policy->authorize('update', $moniker, $args, $ctx);
127 13 my $row = $self->_resolve_row($ctx, $moniker, $args);
128 11 ▶ die client_error "No $moniker row found for the supplied key(s)\n" unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 3 8 11
129 11 my %data = map { $_ => $args->{$_} }
130 8 ▶ grep { $is_data{$_} && defined $args->{$_} } keys %$args;
Condition66%2/3
$is_data{$_} && defined $args->{$_}
!ll&&!rl&&r
7011
MC/DC50%1/2
$is_data{$_} && defined $args->{$_}
$is_data{$_} defined $args->{$_}
Truth table66%2/3
A && B
A= $is_data{$_}B= defined $args->{$_}
ABresult
0X0
100
111
131 8 ▶ if ($restriction) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restriction) { } 2 6 8
132 # The restriction stands in the writing statement itself: a row
133 # outside it is not touched, whatever the lookup found.
134 2 my $ident = { map { $_ => $row->get_column($_) } @pk_cols };
135 2 my $rs = $ctx->resultset($moniker)->search($restriction)->search($ident);
136 2 ▶ my $n = eval { %data ? $rs->update(\%data) : $rs->count };
Branch: 1 branch
BranchTrueFalseTotal
%data ? : 2 0 2
137 2 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
138 2 ▶ die client_error "No $moniker row found for the supplied key(s)\n" unless $n && $n + 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $n and $n + 0 1 1 2
Condition66%2/3
$n and $n + 0
!ll&&!rl&&r
011
MC/DC50%1/2
$n and $n + 0
$n $n + 0
Truth table66%2/3
A and B
A= $nB= $n + 0
ABresult
0X0
100
111
139 1 $row = $ctx->resultset($moniker)->find($ident);
140 }
141 else {
142 6 $row = eval { $row->update(\%data); $row->discard_changes; $row };
143 6 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
144 }
145 7 return { $row->get_columns };
146 },
147 74 };
148 }
149  
150 # deleteX - lookup by PK or any unique constraint; returns Boolean.
151 74 ▶sub build_delete {
Subroutine build_delete: called CC=1 SCAR=0.0
Pod undocumented
152 74 my ($self, $source, $moniker) = @_;
153 return {
154 type => $Boolean,
155 args => $self->_build_lookup_args($source, $moniker),
156 description => "Delete a $moniker row identified by its primary key "
157 . "or by one complete unique constraint, never both and never "
158 . "several constraints. Returns true on success, false if "
159 . "no row matches; a failing DELETE is reported as an error.",
160 18 ▶ resolve => sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
161 18 my ($root, $args, $ctx) = @_;
162 18 my $restriction = $self->policy->authorize('delete', $moniker, $args, $ctx);
163 17 my $row = $self->_resolve_row($ctx, $moniker, $args);
164 14 ▶ return 0 unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 4 10 14
165 10 ▶ if ($restriction) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restriction) 1 9 10
166 # The restriction stands in the DELETE itself; false when it
167 # matches no row.
168 1 my $ident = { map { $_ => $row->get_column($_) } $source->primary_columns };
169 1 my $n = eval { $ctx->resultset($moniker)->search($restriction)
170 ->search($ident)->delete };
171 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
172 1 ▶ return ($n && $n + 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$n && $n + 0 ? : 0 1 1
Condition33%1/3
$n and $n + 0
!ll&&!rl&&r
010
MC/DC0%0/2
$n and $n + 0
$n $n + 0
Truth table33%1/3
A and B
A= $nB= $n + 0
ABresult
0X0
100
111
173 }
174 # A failing DELETE dies like create/update, so the database error
175 # reaches the top-level errors array; false is only "not found".
176 9 ▶ eval { $row->delete; 1 } or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $row->delete; 1 } } or die $@ 8 1 9
177 8 return 1;
178 },
179 74 };
180 }
181  
182 # Lookup arg shape: PK columns first, then each unique-constraint's
183 # columns (deduped).
184 # The primary key is always an identity. A unique-constraint column is an
185 # identity only while it may also be written: a hidden or readonly column
186 # is no argument, so its constraint can never be complete.
187 148 ▶sub _build_lookup_args {
Subroutine _build_lookup_args: called CC=7 SCAR=19.5
188 148 my ($self, $source, $moniker) = @_;
189 148 my %args;
190 148 for my $col ($source->primary_columns) {
191 154 $args{$col} = { type => $self->_scalar_for($source, $col) };
192 }
193 148 my %writable = map { $_ => 1 } $self->policy->writable_columns($moniker);
194 148 my %unique = $source->unique_constraints;
195 148 for my $name (keys %unique) {
196 216 ▶ next if $name eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "primary" 148 68 216
197 68 for my $col (@{ $unique{$name} }) {
198 68 ▶ next unless $writable{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $writable{$col} 0 68 68
199 68 ▶ $args{$col} //= { type => $self->_scalar_for($source, $col) };
Condition50%1/2
$args{$col} //= {"type", $self->_scalar_for($source, $col)}
l!l
068
MC/DC0%0/1
$args{$col} //= {"type", $self->_scalar_for($source, $col)}
$args{$col}
Truth table50%1/2
A
A= $args{$col}
Aresult
11
00
200 }
201 }
202 148 return \%args;
203 }
204  
205 # The equality values of an authorize restriction become column values of
206 # the new row; a value from the client that differs from one is refused
207 # (core k381: the client does not choose what the policy fixes).
208 2 ▶sub _apply_create_restriction {
Subroutine _apply_create_restriction: called CC=7 SCAR=19.8
209 2 my ($self, $data, $restriction) = @_;
210 2 for my $col (sort keys %$restriction) {
211 4 my $want = $restriction->{$col};
212 4 ▶ die client_error "DBIO::GraphQL: authorize restriction on column '$col' is not an "
Branch: 1 branch
BranchTrueFalseTotal
if ref $want 0 4 4
213 . "equality value and cannot be applied to a create\n"
214 if ref $want;
215 4 ▶ if (defined $data->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{$col}) 2 2 4
216 die client_error "DBIO::GraphQL: column '$col' conflicts with the authorize restriction\n"
217 2 ▶ unless defined $want && $data->{$col} eq $want;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want and $data->{$col} eq $want 1 1 2
Condition66%2/3
defined $want and $data->{$col} eq $want
!ll&&!rl&&r
011
MC/DC50%1/2
defined $want and $data->{$col} eq $want
defined $want $data->{$col} eq $want
Truth table66%2/3
A and B
A= defined $wantB= $data->{$col} eq $want
ABresult
0X0
100
111
218 }
219 3 ▶ $data->{$col} = $want if defined $want;
Branch: 1 branch
BranchTrueFalseTotal
if defined $want 3 0 3
220 }
221 }
222  
223 30 ▶sub _resolve_row {
Subroutine _resolve_row: called CC=8 SCAR=20.8
224 30 my ($self, $ctx, $moniker, $args) = @_;
225 30 my $source = $ctx->source($moniker);
226 30 my $rs = $ctx->resultset($moniker);
227  
228 # A complete primary key alone names the row: no fallback to a unique
229 # constraint, whose columns are data arguments of updateX at the same time.
230 30 my @pk_cols = $source->primary_columns;
231 30 my @pk_vals = map { $args->{$_} } @pk_cols;
232 30 ▶ if (@pk_cols && !grep { !defined } @pk_vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols and not grep({not defined $_;} @pk_vals)) 19 11 30
Condition66%2/3
@pk_cols and not grep({not defined $_;} @pk_vals)
!ll&&!rl&&r
01119
MC/DC50%1/2
@pk_cols and not grep({not defined $_;} @pk_vals)
@pk_cols not grep({not defined $_;} @pk_vals)
Truth table66%2/3
A and not(B)
A= @pk_colsB= not grep({not defined $_;} @pk_vals)
ABresult
0X0
100
111
233 19 return $rs->find({ map { $pk_cols[$_] => $pk_vals[$_] }
234 0 .. $#pk_cols });
235 }
236  
237 # No complete primary key. A partial primary key names no row, and it
238 # must not be dropped silently while a constraint finds some other one,
239 # nor may one of several complete constraints be guessed.
240 11 my @given_pk = grep { defined $args->{$_} } @pk_cols;
241  
242 11 my %unique = $source->unique_constraints;
243 my @complete = grep {
244 11 my $n = $_;
245 26 ▶ $n ne 'primary' && !grep { !defined $args->{$_} } @{ $unique{$n} };
Condition100%3/3
$n ne 'primary' && !grep({not defined $args->{$_};} @{$unique{$n};})
!ll&&!rl&&r
11312
MC/DC100%2/2
$n ne 'primary' && !grep({not defined $args->{$_};} @{$unique{$n};})
$n ne 'primary' !grep({not defined $args->{$_};} @{$unique{$n};})
Truth table100%3/3
A && not(B)
A= $n ne 'primary'B= !grep({not defined $args->{$_};} @{$unique{$n};})
ABresult
0X0
100
111
246 } sort keys %unique;
247  
248 11 ▶ die client_error "DBIO::GraphQL: $moniker key is ambiguous: the complete unique "
Branch: 1 branch
BranchTrueFalseTotal
if @complete > 1 2 9 11
249 . "constraints " . join(', ', @complete) . " were supplied together; "
250 . "pass one\n"
251 if @complete > 1;
252 9 ▶ die client_error "DBIO::GraphQL: $moniker key is ambiguous: a partial primary key ("
Branch: 1 branch
BranchTrueFalseTotal
if @given_pk and @complete 3 6 9
Condition100%3/3
@given_pk and @complete
!ll&&!rl&&r
513
MC/DC100%2/2
@given_pk and @complete
@given_pk @complete
Truth table100%3/3
A and B
A= @given_pkB= @complete
ABresult
0X0
100
111
253 . join(', ', @given_pk) . ") was supplied beside unique constraint "
254 . "$complete[0]; pass the complete primary key or the constraint alone\n"
255 if @given_pk && @complete;
256  
257 6 ▶ return undef unless @complete;
Branch: 1 branch
BranchTrueFalseTotal
unless @complete 1 5 6
258 5 return $rs->find({ map { $_ => $args->{$_} } @{ $unique{ $complete[0] } } });
259 }
260  
261 14 1;
262  
263 __END__
« GraphQL/Relationship.pm ↑ Summary GraphQL/Error.pm »