GraphQL/Mutation.pm

filter: Statementstmt 100% 115 / 115 Branchbran 86.6% 26 / 30 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 5 totaltotal 92.6% 163 / 176 CC 23 SCAR 12.5
CC
23
Cov
96%
_build_lookup_args
6.0
_resolve_row
6.0
__ANON__
4.0
CRAP 3.5
« GraphQL/Relationship.pm ↑ Summary GraphQL/ScalarMap.pm »
1 package DBIO::GraphQL::Mutation;
2 # ABSTRACT: Build createX / updateX / deleteX GraphQL field per source
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::NonNull;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use GraphQL::Type::Scalar qw($Boolean);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use DBIO::GraphQL::ScalarMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # fields_for($source, $moniker, $gql_type) returns:
13 #
14 # {
15 # 'create' . ucfirst($moniker) => { type, args, description, resolve },
16 # 'update' . ucfirst($moniker) => { ... },
17 # 'delete' . ucfirst($moniker) => { ... },
18 # }
19 #
20 # Each builder is its own method (build_create / build_update /
21 # build_delete) so subclasses can override a single kind without
22 # rewriting the rest. This is the seam that future SoftDelete / Audit /
23 # PartialUpdate variants plug into.
24  
25 # schema is the connected DBIO::Schema; required at construction.
26 7 __PACKAGE__->mk_group_accessors(simple => qw(schema));
27  
28 16 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
29 16 my ($class, %args) = @_;
30 die "DBIO::GraphQL::Mutation: 'schema' is required\n"
31 16 ▶ unless exists $args{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"schema"} 0 16 16
32 16 my $self = bless {}, $class;
33 16 $self->schema($args{schema});
34 16 return $self;
35 }
36  
37 33 ▶sub fields_for {
Subroutine fields_for: called CC=1 SCAR=0.0
Pod undocumented
38 33 my ($self, $source, $moniker, $gql_type) = @_;
39 return {
40 33 'create' . ucfirst($moniker) => $self->build_create($source, $moniker, $gql_type),
41 'update' . ucfirst($moniker) => $self->build_update($source, $moniker, $gql_type),
42 'delete' . ucfirst($moniker) => $self->build_delete($source, $moniker),
43 };
44 }
45  
46 378 ▶sub _scalar_for {
Subroutine _scalar_for: called CC=1 SCAR=0.0
47 378 my ($self, $source, $col) = @_;
48 378 return DBIO::GraphQL::ScalarMap::for_column($source, $col);
49 }
50  
51 157 ▶sub _col_is_required {
Subroutine _col_is_required: called CC=4 SCAR=13.9
52 157 my ($self, $source, $col) = @_;
53 157 my $info = $source->column_info($col);
54 157 ▶ return 0 if $info->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_auto_increment"} 32 125 157
55 125 ▶ return 0 if defined $info->{default_value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"default_value"} 30 95 125
56 95 ▶ return 0 if $info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_nullable"} 46 49 95
57 49 return 1;
58 }
59  
60 # createX - all columns are accepted as args; required columns (not
61 # auto-inc, no default, not nullable) are wrapped in NonNull.
62 33 ▶sub build_create {
Subroutine build_create: called CC=3 SCAR=11.0
Pod undocumented
63 33 my ($self, $source, $moniker, $gql_type) = @_;
64 33 my %args;
65 33 for my $col ($source->columns) {
66 157 my $scalar = $self->_scalar_for($source, $col);
67 157 ▶ $args{$col} = {
Branch: 1 branch
BranchTrueFalseTotal
$self->_col_is_required($source, $col) ? : 49 108 157
68 type => $self->_col_is_required($source, $col)
69 ? GraphQL::Type::NonNull->new(of => $scalar)
70 : $scalar,
71 };
72 }
73  
74 return {
75 type => $gql_type,
76 args => \%args,
77 description => "Insert a new $moniker row. Required columns are marked non-null.",
78 3 ▶ resolve => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
79 3 my ($root, $args, $ctx) = @_;
80 9 my %data = map { $_ => $args->{$_} }
81 3 grep { defined $args->{$_} } keys %$args;
82 3 my $row = eval { $ctx->resultset($moniker)->create(\%data) };
83 3 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
84 3 return { $row->get_columns };
85 },
86 33 };
87 }
88  
89 # updateX - lookup by PK or any unique constraint; data args are all
90 # non-key columns (full update).
91 33 ▶sub build_update {
Subroutine build_update: called CC=1 SCAR=0.0
Pod undocumented
92 33 my ($self, $source, $moniker, $gql_type) = @_;
93 33 my @pk_cols = $source->primary_columns;
94 33 my %is_pk = map { $_ => 1 } @pk_cols;
95 33 my @data_cols = grep { !$is_pk{$_} } $source->columns;
96  
97 33 my %lookup_args = %{ $self->_build_lookup_args($source) };
98 my %data_args = map {
99 33 $_ => { type => $self->_scalar_for($source, $_) }
100 } @data_cols;
101  
102 return {
103 type => $gql_type,
104 args => { %lookup_args, %data_args },
105 description => "Update an existing $moniker row identified by its primary key "
106 . "or any unique constraint. Pass all non-key columns.",
107 4 ▶ resolve => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
108 4 my ($root, $args, $ctx) = @_;
109 4 my $row = $self->_resolve_row($ctx, $moniker, $args);
110 4 ▶ die "No $moniker row found for the supplied key(s)\n" unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 1 3 4
111 5 my %data = map { $_ => $args->{$_} }
112 3 ▶ grep { !$is_pk{$_} && defined $args->{$_} } keys %$args;
Condition66%2/3
!$is_pk{$_} && defined $args->{$_}
!ll&&!rl&&r
305
MC/DC50%1/2
!$is_pk{$_} && defined $args->{$_}
!$is_pk{$_} defined $args->{$_}
Truth table66%2/3
not(A) && B
A= !$is_pk{$_}B= defined $args->{$_}
ABresult
0X0
100
111
113 3 $row = eval { $row->update(\%data); $row->discard_changes; $row };
114 3 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
115 3 return { $row->get_columns };
116 },
117 33 };
118 }
119  
120 # deleteX - lookup by PK or any unique constraint; returns Boolean.
121 33 ▶sub build_delete {
Subroutine build_delete: called CC=1 SCAR=0.0
Pod undocumented
122 33 my ($self, $source, $moniker) = @_;
123 return {
124 type => $Boolean,
125 args => $self->_build_lookup_args($source),
126 description => "Delete a $moniker row identified by its primary key "
127 . "or any unique constraint. Returns true on success.",
128 4 ▶ resolve => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
129 4 my ($root, $args, $ctx) = @_;
130 4 my $row = $self->_resolve_row($ctx, $moniker, $args);
131 4 ▶ return 0 unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 1 3 4
132 3 eval { $row->delete };
133 3 ▶ return $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 3 3
134 },
135 33 };
136 }
137  
138 # Lookup arg shape: PK columns first, then each unique-constraint's
139 # columns (deduped).
140 66 ▶sub _build_lookup_args {
Subroutine _build_lookup_args: called CC=6 SCAR=17.9
141 66 my ($self, $source) = @_;
142 66 my %args;
143 66 for my $col ($source->primary_columns) {
144 68 $args{$col} = { type => $self->_scalar_for($source, $col) };
145 }
146 66 my %unique = $source->unique_constraints;
147 66 for my $name (keys %unique) {
148 96 ▶ next if $name eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "primary" 66 30 96
149 30 for my $col (@{ $unique{$name} }) {
150 30 ▶ $args{$col} //= { type => $self->_scalar_for($source, $col) };
Condition50%1/2
$args{$col} //= {"type", $self->_scalar_for($source, $col)}
l!l
030
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
151 }
152 }
153 66 return \%args;
154 }
155  
156 8 ▶sub _resolve_row {
Subroutine _resolve_row: called CC=6 SCAR=17.9
157 8 my ($self, $ctx, $moniker, $args) = @_;
158 8 my $source = $ctx->source($moniker);
159 8 my $rs = $ctx->resultset($moniker);
160  
161 8 my @pk_cols = $source->primary_columns;
162 8 my @pk_vals = map { $args->{$_} } @pk_cols;
163 8 ▶ unless (grep { !defined } @pk_vals) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {not defined $_;} @pk_vals) 6 2 8
164 6 my $row = $rs->find({ map { $pk_cols[$_] => $pk_vals[$_] }
165 0 .. $#pk_cols });
166 6 ▶ return $row if $row;
Branch: 1 branch
BranchTrueFalseTotal
if $row 4 2 6
167 }
168  
169 4 my %unique = $source->unique_constraints;
170 4 for my $name (sort grep { $_ ne 'primary' } keys %unique) {
171 4 my @cols = @{ $unique{$name} };
172 4 my @vals = map { $args->{$_} } @cols;
173 4 ▶ next if grep { !defined } @vals;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @vals 1 3 4
174 3 my $row = $rs->find({ map { $cols[$_] => $vals[$_] } 0 .. $#cols });
175 3 ▶ return $row if $row;
Branch: 1 branch
BranchTrueFalseTotal
if $row 2 1 3
176 }
177 2 return undef;
178 }
179  
180 7 1;
181  
182 __END__
183  
184 =pod
185  
186 =encoding UTF-8
187  
188 =head1 NAME
189  
190 DBIO::GraphQL::Mutation - Build createX / updateX / deleteX GraphQL field per source
191  
192 =head1 VERSION
193  
194 version 0.900001
195  
196 =head1 AUTHOR
197  
198 DBIO Authors
199  
200 =head1 COPYRIGHT AND LICENSE
201  
202 Copyright (C) 2026 DBIO Authors
203  
204 This is free software; you can redistribute it and/or modify it under
205 the same terms as the Perl 5 programming language system itself.
206  
207 =cut
« GraphQL/Relationship.pm ↑ Summary GraphQL/ScalarMap.pm »