GraphQL/Plugin/Convert/DBIC.pm

filter: Statementstmt 99.5% 224 / 225 Branchbran 82.9% 73 / 88 Conditioncond 79.5% 35 / 44 MC/DCmcdc 60.0% 15 / 25 Subroutinesub 100% 34 / 34 Podpod 50.0% 1 / 2 totaltotal 91.3% 382 / 418 CC 66 SCAR 20.1
CC
66
Cov
93%
to_graphql
19.0
field_resolver
10.3
_mutation_resolver
10.1
CRAP 7.5
↑ Summary SQL/Translator/Producer/GraphQL.pm »
1 package GraphQL::Plugin::Convert::DBIC;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use GraphQL::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use GraphQL::Plugin::Type::DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use GraphQL::Debug qw(_debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Lingua::EN::Inflect::Number qw(to_S to_PL);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = "0.19";
11 5 ▶use constant DEBUG => $ENV{GRAPHQL_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 my %GRAPHQL_TYPE2SQLS = (
14 String => [
15 'wlongvarchar',
16 'guid',
17 'uuid',
18 'wvarchar',
19 'wchar',
20 'longvarbinary',
21 'varbinary',
22 'binary',
23 'longvarchar',
24 'unknown_type',
25 'all_types',
26 'char',
27 'varchar',
28 'udt',
29 'udt_locator',
30 'row',
31 'ref',
32 'blob',
33 'blob_locator',
34 'clob',
35 'clob_locator',
36 'array',
37 'array_locator',
38 'multiset',
39 'multiset_locator',
40 # mysql
41 'set',
42 'text',
43 'tinytext',
44 'mediumtext',
45 'longtext',
46 # pgsql
47 'cidr',
48 'inet',
49 ],
50 Int => [
51 'bigint',
52 'bit',
53 'tinyint',
54 'integer',
55 'smallint',
56 'mediumint',
57 'interval',
58 'interval_year',
59 'interval_month',
60 'interval_day',
61 'interval_hour',
62 'interval_minute',
63 'interval_second',
64 'interval_year_to_month',
65 'interval_day_to_hour',
66 'interval_day_to_minute',
67 'interval_day_to_second',
68 'interval_hour_to_minute',
69 'interval_hour_to_second',
70 'interval_minute_to_second',
71 # not DBI SQL_* types
72 'int',
73 ],
74 Float => [
75 'numeric',
76 'decimal',
77 'float',
78 'real',
79 'double',
80 ],
81 DateTime => [
82 'datetime',
83 'date',
84 'time',
85 'timestamp',
86 'type_date',
87 'type_time',
88 'type_timestamp',
89 'type_time_with_timezone',
90 'type_timestamp_with_timezone',
91 # pgsql
92 'timestamp with time zone',
93 'timestamp without time zone',
94 ],
95 Boolean => [
96 'boolean',
97 ],
98 ID => [
99 'wvarchar',
100 ],
101 );
102 my %TYPEMAP = (
103 (map {
104 30 my $gql_type = $_;
105 map {
106 355 ($_ => $gql_type)
107 30 } @{ $GRAPHQL_TYPE2SQLS{$gql_type} }
108 } keys %GRAPHQL_TYPE2SQLS),
109 13 ▶ enum => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
110 13 my ($source, $column, $info) = @_;
111 13 my $extra = $info->{extra};
112 return {
113 kind => 'enum',
114 name => _dbicsource2pretty(
115 $extra->{custom_type_name} || "${source}_$column"
116 ),
117 13 ▶ values => { map { _trim_name($_) => { value => $_ } } @{ $extra->{list} } },
Condition100%2/2
$extra->{'custom_type_name'} || "${source}_$column"
l!l
67
MC/DC100%1/1
$extra->{'custom_type_name'} || "${source}_$column"
$extra->{'custom_type_name'}
Truth table100%2/2
A
A= $extra->{'custom_type_name'}
Aresult
11
00
118 }
119 },
120 5 );
121 5 my %TYPE2SCALAR = map { ($_ => 1) } qw(ID String Int Float Boolean);
122  
123 83 ▶sub _dbicsource2pretty {
Subroutine _dbicsource2pretty: called CC=3 SCAR=11.1
124 83 my ($source) = @_;
125 83 ▶ confess "_dbicsource2pretty given undef" if !defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 83 83
126 83 ▶ $source = eval { $source->source_name } || $source;
Condition66%2/3
eval { do { $source->source_name } } || $source
l!l&&r!l&&!r
30530
MC/DC0%0/2
eval { do { $source->source_name } } || $source
eval { do { $source->source_name } } $source
Truth table66%2/3
A || B
A= eval { do { $source->source_name } }B= $source
ABresult
1X1
011
000
127 83 $source =~ s#.*::##;
128 83 $source = to_S $source;
129 83 join '', map ucfirst, split /_+/, $source;
130 }
131  
132 38 ▶sub _trim_name {
Subroutine _trim_name: called CC=2 SCAR=7.0
133 38 my ($name) = @_;
134 38 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 38 38
135 38 $name =~ s#[^a-zA-Z0-9_]+#_#g;
136 38 $name;
137 }
138  
139 854 ▶sub _apply_modifier {
Subroutine _apply_modifier: called CC=5 SCAR=16.1
140 854 my ($modifier, $typespec) = @_;
141 854 ▶ return $typespec if !$modifier;
Branch: 1 branch
BranchTrueFalseTotal
unless $modifier 113 741 854
142 741 ▶ return $typespec if $modifier eq 'non_null'
Branch: 1 branch
BranchTrueFalseTotal
if $modifier eq "non_null" and ref $typespec eq "ARRAY" and $typespec->[0] eq "non_null" 63 678 741
Condition100%6/6
$modifier eq "non_null" and ref $typespec eq "ARRAY" and $typespec->[0] eq "non_null"
!ll&&!rl&&r
56711163
$modifier eq "non_null" and ref $typespec eq "ARRAY"
!ll&&!rl&&r
270297174
MC/DC100%3/3
$modifier eq "non_null" and ref $typespec eq "ARRAY" and $typespec->[0] eq "non_null"
$modifier eq "non_null" ref $typespec eq "ARRAY" $typespec->[0] eq "non_null"
Truth table100%4/4
A and B and C
A= $modifier eq "non_null"B= ref $typespec eq "ARRAY"C= $typespec->[0] eq "non_null"
ABCresult
0XX0
10X0
1100
1111
143 and ref $typespec eq 'ARRAY'
144 and $typespec->[0] eq 'non_null'; # no double-non_null
145 678 [ $modifier, { type => $typespec } ];
146 }
147  
148 68 ▶sub _remove_modifiers {
Subroutine _remove_modifiers: called CC=3 SCAR=11.0
149 68 my ($typespec) = @_;
150 68 ▶ return _remove_modifiers($typespec->{type}) if ref $typespec eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $typespec eq "HASH" 18 50 68
151 50 ▶ return $typespec if ref $typespec ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $typespec ne "ARRAY" 32 18 50
152 18 _remove_modifiers($typespec->[1]);
153 }
154  
155 28 ▶sub _type2createinput {
Subroutine _type2createinput: called CC=2 SCAR=6.9
156 28 my ($name, $fields, $pk21, $fk21, $column21, $name2type) = @_;
157 +{
158 kind => 'input',
159 name => "${name}CreateInput",
160 fields => {
161 162 ▶ (map { ($_ => $fields->{$_}) }
Condition100%3/3
$pk21->{$_} || $fk21->{$_}
l!l&&r!l&&!r
272162
MC/DC100%2/2
$pk21->{$_} || $fk21->{$_}
$pk21->{$_} $fk21->{$_}
Truth table100%3/3
A || B
A= $pk21->{$_}B= $fk21->{$_}
ABresult
1X1
011
000
162 28 grep !$pk21->{$_} && !$fk21->{$_}, keys %$column21),
163 _make_fk_fields($name, $fk21, $name2type),
164 },
165 };
166 }
167  
168 28 ▶sub _type2idinput {
Subroutine _type2idinput: called CC=1 SCAR=0.0
169 28 my ($name, $fields, $pk21) = @_;
170 +{
171 kind => 'input',
172 name => "${name}IDInput",
173 fields => {
174 28 (map { ($_ => $fields->{$_}) }
175 keys %$pk21),
176 },
177 };
178 }
179  
180 30 ▶sub _type2searchinput {
Subroutine _type2searchinput: called CC=1 SCAR=0.0
181 30 my ($name, $column2rawtype, $pk21, $column21) = @_;
182 +{
183 kind => 'input',
184 name => "${name}SearchInput",
185 fields => {
186 171 (map { ($_ => { type => $column2rawtype->{$_} }) }
187 30 grep !$pk21->{$_}, keys %$column21),
188 },
189 };
190 }
191  
192 28 ▶sub _type2updateinput {
Subroutine _type2updateinput: called CC=1 SCAR=0.0
193 28 my ($name) = @_;
194 +{
195 28 kind => 'input',
196 name => "${name}UpdateInput",
197 fields => {
198 id => { type => _apply_modifier('non_null', "${name}IDInput") },
199 payload => { type => _apply_modifier('non_null', "${name}SearchInput") },
200 },
201 };
202 }
203  
204 28 ▶sub _make_fk_fields {
Subroutine _make_fk_fields: called CC=4 SCAR=13.9
205 28 my ($name, $fk21, $name2type) = @_;
206 28 my $type = $name2type->{$name};
207 (map {
208 28 my $field_type = $type->{fields}{$_}{type};
209 17 ▶ if (!$TYPE2SCALAR{_remove_modifiers($field_type)}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($TYPE2SCALAR{_remove_modifiers($field_type)}) 15 2 17
210 15 ▶ my $non_null =
Condition66%2/3
ref $field_type eq 'ARRAY' && $field_type->[0] eq 'non_null'
!ll&&!rl&&r
609
MC/DC50%1/2
ref $field_type eq 'ARRAY' && $field_type->[0] eq 'non_null'
ref $field_type eq 'ARRAY' $field_type->[0] eq 'non_null'
Truth table66%2/3
A && B
A= ref $field_type eq 'ARRAY'B= $field_type->[0] eq 'non_null'
ABresult
0X0
100
111
211 ref($field_type) eq 'ARRAY' && $field_type->[0] eq 'non_null';
212 15 ▶ $field_type = _apply_modifier(
Condition100%2/2
$non_null && 'non_null'
!ll
69
MC/DC100%1/1
$non_null && 'non_null'
$non_null
Truth table100%2/2
A
A= $non_null
Aresult
00
11
213 $non_null && 'non_null', _remove_modifiers($field_type)."IDInput"
214 );
215 }
216 17 ($_ => { type => $field_type })
217 } keys %$fk21);
218 }
219  
220 64 ▶sub field_resolver {
Subroutine field_resolver: called CC=10 SCAR=23.4
Pod documented
221 64 my ($root_value, $args, $context, $info) = @_;
222 64 my $field_name = $info->{field_name};
223 64 DEBUG and _debug('DBIC.resolver', $field_name, $args, $info);
224 64 my $parent_name = $info->{parent_type}->name;
225 64 ▶ if ($parent_name eq 'Mutation') {
Branch: 2 branches
BranchTrueFalseTotal
if ($parent_name eq 'Mutation') { } 3 61 64
elsif ($parent_name eq 'Query') { } 8 53 61
226 3 goto &_mutation_resolver;
227 } elsif ($parent_name eq 'Query') {
228 8 goto &_query_resolver;
229 }
230 my $property = ref($root_value) eq 'HASH'
231 53 ▶ ? $root_value->{$field_name}
Branch: 1 branch
BranchTrueFalseTotal
ref $root_value eq 'HASH' ? : 45 8 53
232 : $root_value;
233 53 ▶ return $property->($args, $context, $info) if ref $property eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $property eq "CODE" 0 53 53
234 53 ▶ return $property // die "DBIC.resolver could not resolve '$field_name'\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $root_value eq "HASH" or not $root_value->can($field_name) 45 8 53
Condition60%3/5
ref $root_value eq "HASH" or not $root_value->can($field_name)
l!l&&r!l&&!r
4508
$property // die("DBIC.resolver could not resolve '${field_name}'\n")
l!l
450
MC/DC50%1/2
ref $root_value eq "HASH" or not $root_value->can($field_name)
ref $root_value eq "HASH" not $root_value->can($field_name)
MC/DC0%0/1
$property // die("DBIC.resolver could not resolve '${field_name}'\n")
$property
Truth table66%2/3
A or not(B)
A= ref $root_value eq "HASH"B= not $root_value->can($field_name)
ABresult
1X1
011
000
Truth table50%1/2
A
A= $property
Aresult
11
00
235 if ref $root_value eq 'HASH' or !$root_value->can($field_name);
236 8 ▶ return $root_value->$field_name($args, $context, $info)
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($root_value, "DBIx::Class::Core") 0 8 8
237 if !UNIVERSAL::isa($root_value, 'DBIx::Class::Core');
238 # dbic search
239 8 my $rs = $root_value->$field_name;
240 8 ▶ $rs = [ $rs->all ] if $info->{return_type}->isa('GraphQL::Type::List');
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"return_type"}->isa("GraphQL::Type::List") 0 8 8
241 8 return $rs;
242 }
243  
244 13 ▶sub _subfieldrels {
Subroutine _subfieldrels: called CC=6 SCAR=18.2
245 13 my ($field_node) = @_;
246 13 ▶ die "_subfieldrels called on non-field" if $field_node->{kind} ne 'field';
Branch: 1 branch
BranchTrueFalseTotal
if $field_node->{"kind"} ne "field" 0 13 13
247 13 ▶ return {} unless my @sels = @{ $field_node->{selections} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless my(@sels) = @{$field_node->{"selections"} or [];} 0 13 13
Condition50%1/2
$field_node->{'selections'} || []
l!l
130
MC/DC0%0/1
$field_node->{'selections'} || []
$field_node->{'selections'}
Truth table50%1/2
A
A= $field_node->{'selections'}
Aresult
11
00
248 13 ▶ return {} unless my @withsels = grep @{ $_->{selections} || [] }, @sels;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@withsels) = grep(@{$_->{"selections"} or [];}, @sels) 9 4 13
Condition100%2/2
$_->{'selections'} || []
l!l
522
MC/DC100%1/1
$_->{'selections'} || []
$_->{'selections'}
Truth table100%2/2
A
A= $_->{'selections'}
Aresult
11
00
249 4 +{ map { $_->{name} => _subfieldrels($_) } @withsels };
250 }
251  
252 8 ▶sub _query_resolver {
Subroutine _query_resolver: called CC=4 SCAR=13.9
253 8 my ($dbic_schema, $args, $context, $info) = @_;
254 8 my $name = $info->{return_type}->name;
255 8 ▶ my $method = $info->{return_type}->isa('GraphQL::Type::List')
Branch: 1 branch
BranchTrueFalseTotal
$info->{'return_type'}->isa('GraphQL::Type::List') ? : 7 1 8
256 ? 'search' : 'find';
257 8 my @subfieldrels = map _subfieldrels($_), @{$info->{field_nodes}};
258 8 ▶ $args = $args->{input} if ref $args->{input} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args->{"input"} eq "HASH" 1 7 8
259 8 $args = +{ map { ("me.$_" => $args->{$_}) } keys %$args };
260 8 DEBUG and _debug('DBIC.root_value', $name, $method, $args, \@subfieldrels, $info);
261 8 my $rs = $dbic_schema->resultset($name);
262 8 my $result = $rs->$method(
263 $args,
264 {
265 prefetch => { map %$_, @subfieldrels },
266 result_class => 'DBIx::Class::ResultClass::HashRefInflator'
267 },
268 );
269 8 ▶ $result = [ $result->all ] if $method eq 'search';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "search" 7 1 8
270 8 $result;
271 }
272  
273 52 ▶sub _make_query_pk_field {
Subroutine _make_query_pk_field: called CC=3 SCAR=11.0
274 52 my ($typename, $type, $name2pk21, $is_list) = @_;
275 52 my $return_type = $typename;
276 52 ▶ $return_type = _apply_modifier('list', $return_type) if $is_list;
Branch: 1 branch
BranchTrueFalseTotal
if $is_list 26 26 52
277 +{
278 type => $return_type,
279 args => {
280 map {
281 54 my $field_type = _apply_modifier('non_null', $type->{fields}{$_}{type});
282 54 ▶ $field_type = _apply_modifier('non_null', _apply_modifier('list',
Branch: 1 branch
BranchTrueFalseTotal
if $is_list 27 27 54
283 $field_type
284 )) if $is_list;
285 54 $_ => { type => $field_type }
286 52 } keys %{ $name2pk21->{$typename} }
287 },
288 };
289 }
290  
291 114 ▶sub _make_input_field {
Subroutine _make_input_field: called CC=3 SCAR=11.0
292 114 my ($typename, $return_type, $mutation_kind, $list_in, $list_out) = @_;
293 114 ▶ $return_type = _apply_modifier('list', $return_type) if $list_out;
Branch: 1 branch
BranchTrueFalseTotal
if $list_out 114 0 114
294 114 my $input_type = $typename . ucfirst($mutation_kind) . 'Input';
295 114 $input_type = _apply_modifier('non_null', $input_type);
296 114 ▶ $input_type = _apply_modifier('non_null', _apply_modifier('list',
Branch: 1 branch
BranchTrueFalseTotal
if $list_in 84 30 114
297 $input_type
298 )) if $list_in;
299 +{
300 114 type => $return_type,
301 args => { input => { type => $input_type } },
302 };
303 }
304  
305 5 ▶use constant MUTATE_IDPROCESS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
306 3 ▶ update => sub { $_[0]->{id} },
Subroutine __ANON__: called CC=1 SCAR=0.0
307 1 ▶ delete => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
308 5 };
309 5 ▶use constant MUTATE_ARGSPROCESS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
310 2 ▶ update => sub { $_[0]->{payload} },
Subroutine __ANON__: called CC=1 SCAR=0.0
311 1 ▶ delete => sub { },
Subroutine __ANON__: called CC=1 SCAR=0.0
312 5 };
313 5 ▶use constant MUTATE_POSTPROCESS => {
Subroutine BEGIN: called CC=1 SCAR=0.2
314 2 ▶ update => sub { ref($_[0]) eq 'GraphQL::Error' ? $_[0] : $_[0]->discard_changes },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'GraphQL::Error' ? : 0 2 2
315 1 ▶ delete => sub { ref($_[0]) eq 'GraphQL::Error' ? $_[0] : $_[0] && 1 },
Subroutine __ANON__: called CC=3 SCAR=12.7
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'GraphQL::Error' ? : 0 1 1
Condition50%1/2
$_[0] and 1
!ll
01
MC/DC0%0/1
$_[0] and 1
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
00
11
316 5 };
317 3 ▶sub _mutation_resolver {
Subroutine _mutation_resolver: called CC=10 SCAR=23.1
318 3 my ($dbic_schema, $args, $context, $info) = @_;
319 3 my $name = $info->{field_name};
320 3 ▶ die "Couldn't understand field '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ s/^(create|update|delete)// 0 3 3
321 unless $name =~ s/^(create|update|delete)//;
322 3 my $method = $1;
323 3 my $find_first = $method ne 'create';
324 3 my ($id_process, $args_process, $result_process) = map $_->{$method},
325 MUTATE_IDPROCESS, MUTATE_ARGSPROCESS, MUTATE_POSTPROCESS;
326 3 ▶ $args = $args->{input} if $args->{input};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"input"} 3 0 3
327 3 my $is_list = ref $args eq 'ARRAY';
328 3 ▶ $args = [ $args ] if !$is_list; # so can just deal as list below
Branch: 1 branch
BranchTrueFalseTotal
unless $is_list 0 3 3
329 3 DEBUG and _debug("DBIC._mutation_resolver", $info->{field_name}, [ $id_process ? map $id_process->($_), @$args : () ], $args);
330 3 my $rs = $dbic_schema->resultset($name);
331 my $all_result = [
332 map {
333 3 my $operand = $rs;
334 5 ▶ $operand = $operand->find($id_process->($_)) if $find_first;
Branch: 1 branch
BranchTrueFalseTotal
if $find_first 4 1 5
335 5 ▶ my $result = $operand
Branch: 2 branches
BranchTrueFalseTotal
$operand ? : 4 1 5
$args_process ? : 3 1 4
336 ? $operand->$method($args_process ? $args_process->($_) : $_)
337 : GraphQL::Error->coerce("$name not found");
338 5 ▶ $result = $result_process->($result)
Branch: 1 branch
BranchTrueFalseTotal
if $result_process and ref $result ne "GraphQL::Error" 3 2 5
Condition100%3/3
$result_process and ref $result ne "GraphQL::Error"
!ll&&!rl&&r
113
MC/DC100%2/2
$result_process and ref $result ne "GraphQL::Error"
$result_process ref $result ne "GraphQL::Error"
Truth table100%3/3
A and B
A= $result_processB= ref $result ne "GraphQL::Error"
ABresult
0X0
100
111
339 if $result_process and ref($result) ne 'GraphQL::Error';
340 5 $result;
341 } @$args
342 ];
343 3 ▶ $all_result = $all_result->[0] if !$is_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_list 0 3 3
344 3 $all_result
345 }
346  
347 6 ▶sub to_graphql {
Subroutine to_graphql: called CC=19 SCAR=29.5
Pod undocumented
348 6 my ($class, $dbic_schema) = @_;
349 6 ▶ $dbic_schema = $dbic_schema->() if ((ref($dbic_schema)||'') eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dbic_schema || "") eq "CODE" 0 6 6
Condition50%1/2
ref $dbic_schema || ''
l!l
60
MC/DC0%0/1
ref $dbic_schema || ''
ref $dbic_schema
Truth table50%1/2
A
A= ref $dbic_schema
Aresult
11
00
350 6 my @ast;
351 my (
352 6 %name2type, %name2column21, %name2pk21, %name2fk21,
353 %name2column2rawtype, %seentype, %name2isview,
354 );
355 6 for my $source (map $dbic_schema->source($_), $dbic_schema->sources) {
356 30 my $name = _dbicsource2pretty($source);
357 30 DEBUG and _debug("schema_dbic2graphql($name)", $source);
358 30 ▶ $name2isview{$name} = 1 if $source->can('view_definition');
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("view_definition") 2 28 30
359 30 my %fields;
360 30 my $columns_info = $source->columns_info;
361 30 $name2pk21{$name} = +{ map { ($_ => 1) } $source->primary_columns };
362 my %rel2info = map {
363 30 ($_ => $source->relationship_info($_))
364 } $source->relationships;
365 30 for my $column (keys %$columns_info) {
366 216 my $info = $columns_info->{$column};
367 216 DEBUG and _debug("schema_dbic2graphql($name.col)", $column, $info);
368 216 my $rawtype = $TYPEMAP{ lc $info->{data_type} };
369 216 ▶ if ( 'CODE' eq ref $rawtype ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $rawtype) 13 203 216
370 13 my $col_spec = $rawtype->($name, $column, $info);
371 13 ▶ push @ast, $col_spec unless $seentype{$col_spec->{name}};
Branch: 1 branch
BranchTrueFalseTotal
unless $seentype{$col_spec->{"name"}} 10 3 13
372 13 $rawtype = $col_spec->{name};
373 13 $seentype{$col_spec->{name}} = 1;
374 }
375 216 $name2column2rawtype{$name}->{$column} = $rawtype;
376 my $fulltype = _apply_modifier(
377 216 ▶ !$info->{is_nullable} && 'non_null',
Condition75%3/4
!$info->{'is_nullable'} && 'non_null'
!ll
107109
$rawtype // die(qq['${column}' unknown data type: @{[lc $info->{"data_type"}];}\n])
l!l
2160
MC/DC100%1/1
!$info->{'is_nullable'} && 'non_null'
!$info->{'is_nullable'}
MC/DC0%0/1
$rawtype // die(qq['${column}' unknown data type: @{[lc $info->{"data_type"}];}\n])
$rawtype
Truth table100%2/2
not(A)
A= !$info->{'is_nullable'}
Aresult
00
11
Truth table50%1/2
A
A= $rawtype
Aresult
11
00
378 $rawtype
379 0 // die "'$column' unknown data type: @{[lc $info->{data_type}]}\n",
380 );
381 216 $fields{$column} = +{ type => $fulltype };
382 216 ▶ $name2fk21{$name}->{$column} = 1 if $info->{is_foreign_key};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_foreign_key"} 17 199 216
383 216 $name2column21{$name}->{$column} = 1;
384 }
385 30 for my $rel (keys %rel2info) {
386 40 my $info = $rel2info{$rel};
387 40 DEBUG and _debug("schema_dbic2graphql($name.rel)", $rel, $info);
388 40 my $type = _dbicsource2pretty($info->{source});
389 40 $rel =~ s/_id$//; # dumb heuristic
390 40 delete $name2column21{$name}->{$rel}; # so it's not a "column" now
391 40 delete $name2pk21{$name}{$rel}; # it's not a PK either
392 # if it WAS a column, capture its non-null-ness
393 40 ▶ my $non_null = ref(($fields{$rel} || {})->{type}) eq 'ARRAY';
Condition100%2/2
$fields{$rel} || {}
l!l
1822
MC/DC100%1/1
$fields{$rel} || {}
$fields{$rel}
Truth table100%2/2
A
A= $fields{$rel}
Aresult
11
00
394 40 ▶ $type = _apply_modifier('non_null', $type) if $non_null;
Branch: 1 branch
BranchTrueFalseTotal
if $non_null 9 31 40
395 40 ▶ $type = _apply_modifier('list', $type) if $info->{attrs}{accessor} eq 'multi';
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"attrs"}{"accessor"} eq "multi" 19 21 40
396 40 ▶ $type = _apply_modifier('non_null', $type) if $non_null; # in case list
Branch: 1 branch
BranchTrueFalseTotal
if $non_null 9 31 40
397 40 $fields{$rel} = +{ type => $type };
398 }
399 30 my $spec = +{
400 kind => 'type',
401 name => $name,
402 fields => \%fields,
403 };
404 30 $name2type{$name} = $spec;
405 30 push @ast, $spec;
406 }
407 push @ast, map _type2idinput(
408 $_, $name2type{$_}->{fields}, $name2pk21{$_},
409 $name2column21{$_},
410 6 ▶ ), grep !$name2isview{$_} || keys %{ $name2pk21{$_} }, keys %name2type;
Condition66%2/3
!$name2isview{$_} || keys %{$name2pk21{$_};}
l!l&&r!l&&!r
2802
MC/DC50%1/2
!$name2isview{$_} || keys %{$name2pk21{$_};}
!$name2isview{$_} keys %{$name2pk21{$_};}
Truth table66%2/3
not(A) || B
A= !$name2isview{$_}B= keys %{$name2pk21{$_};}
ABresult
1X1
011
000
411 push @ast, map _type2createinput(
412 $_, $name2type{$_}->{fields}, $name2pk21{$_}, $name2fk21{$_},
413 $name2column21{$_}, \%name2type,
414 6 ), grep !$name2isview{$_}, keys %name2type;
415 push @ast, map _type2searchinput(
416 $_, $name2column2rawtype{$_}, $name2pk21{$_},
417 6 $name2column21{$_},
418 ), keys %name2type;
419 6 push @ast, map _type2updateinput($_), grep !$name2isview{$_}, keys %name2type;
420 push @ast, {
421 kind => 'type',
422 name => 'Query',
423 fields => {
424 map {
425 6 my $name = $_;
426 30 my $type = $name2type{$name};
427 30 my $pksearch_name = lcfirst $name;
428 30 my $pksearch_name_plural = to_PL($pksearch_name);
429 30 my $input_search_name = "search$name";
430 30 my @fields = (
431 $input_search_name => _make_input_field($name, $name, 'search', 0, 1),
432 );
433 push @fields, map((
434 ($_ ? $pksearch_name_plural : $pksearch_name),
435 _make_query_pk_field($name, $type, \%name2pk21, $_),
436 30 ▶ ), (0, 1)) if keys %{ $name2pk21{$name} };
Branch: 2 branches
BranchTrueFalseTotal
if keys %{$name2pk21{$name};} 26 4 30
$_ ? : 26 26 52
437 30 @fields;
438 } keys %name2type
439 },
440 };
441 push @ast, {
442 kind => 'type',
443 name => 'Mutation',
444 fields => {
445 map {
446 28 my $name = $_;
447 28 my $create_name = "create$name";
448 28 my $update_name = "update$name";
449 28 my $delete_name = "delete$name";
450 (
451 28 $create_name => _make_input_field($name, $name, 'create', 1, 1),
452 $update_name => _make_input_field($name, $name, 'update', 1, 1),
453 $delete_name => _make_input_field($name, 'Boolean', 'ID', 1, 1),
454 )
455 6 } grep !$name2isview{$_}, keys %name2type
456 },
457 };
458 +{
459 6 schema => GraphQL::Schema->from_ast(\@ast),
460 root_value => $dbic_schema,
461 resolver => \&field_resolver,
462 };
463 }
464  
465 =encoding utf-8
466  
467 =head1 NAME
468  
469 GraphQL::Plugin::Convert::DBIC - convert DBIx::Class schema to GraphQL schema
470  
471 =begin markdown
472  
473 # PROJECT STATUS
474  
475 | OS | Build status |
476 |:-------:|--------------:|
477 | Linux | [![Build Status](https://travis-ci.org/graphql-perl/GraphQL-Plugin-Convert-DBIC.svg?branch=master)](https://travis-ci.org/graphql-perl/GraphQL-Plugin-Convert-DBIC) |
478  
479 [![CPAN version](https://badge.fury.io/pl/GraphQL-Plugin-Convert-DBIC.svg)](https://metacpan.org/pod/GraphQL::Plugin::Convert::DBIC)
480  
481 =end markdown
482  
483 =head1 SYNOPSIS
484  
485 use GraphQL::Plugin::Convert::DBIC;
486 use My::Local::Schema;
487 my $converted = GraphQL::Plugin::Convert::DBIC->to_graphql(My::Local::Schema->connect);
488 print $converted->{schema}->to_doc;
489  
490 =head1 DESCRIPTION
491  
492 This module implements the L<GraphQL::Plugin::Convert> API to convert
493 a L<DBIx::Class::Schema> to L<GraphQL::Schema> etc.
494  
495 Its C<Query> type represents a guess at what fields are suitable, based
496 on providing a lookup for each type (a L<DBIx::Class::ResultSource>).
497  
498 =head2 Example
499  
500 Consider this minimal data model:
501  
502 blog:
503 id # primary key
504 articles # has_many
505 title # non null
506 language # nullable
507 article:
508 id # primary key
509 blog # foreign key to Blog
510 title # non null
511 content # nullable
512  
513 =head2 Generated Output Types
514  
515 These L<GraphQL::Type::Object> types will be generated:
516  
517 type Blog {
518 id: Int!
519 articles: [Article]
520 title: String!
521 language: String
522 }
523  
524 type Article {
525 id: Int!
526 blog: Blog
527 title: String!
528 content: String
529 }
530  
531 type Query {
532 blog(id: [Int!]!): [Blog]
533 article(id: [Int!]!): [Blog]
534 }
535  
536 Note that while the queries take a list, the return order is
537 undefined. This also applies to the mutations. If this matters, request
538 the primary key fields and use those to sort.
539  
540 =head2 Generated Input Types
541  
542 Different input types are needed for each of CRUD (Create, Read, Update,
543 Delete).
544  
545 The create one needs to have non-null fields be non-null, for idiomatic
546 GraphQL-level error-catching. The read one needs all fields nullable,
547 since this will be how searches are implemented, allowing fields to be
548 left un-searched-for. Both need to omit primary key fields. The read
549 one also needs to omit foreign key fields, since the idiomatic GraphQL
550 way for this is to request the other object, with this as a field on it,
551 then request any required fields of this.
552  
553 Meanwhile, the update and delete ones need to include the primary key
554 fields, to indicate what to mutate, and also all non-primary key fields
555 as nullable, which for update will mean leaving them unchanged, and for
556 delete is to be ignored. These input types are split into one input
557 for the primary keys, which is a full input type to allow for multiple
558 primary keys, then a wrapper input for updates, that takes one ID input,
559 and a payload that due to the same requirements, is just the search input.
560  
561 Therefore, for the above, these input types (and an updated Query,
562 and Mutation) are created:
563  
564 input BlogCreateInput {
565 title: String!
566 language: String
567 }
568  
569 input BlogSearchInput {
570 title: String
571 language: String
572 }
573  
574 input BlogIDInput {
575 id: Int!
576 }
577  
578 input BlogUpdateInput {
579 id: BlogIDInput!
580 payload: BlogSearchInput!
581 }
582  
583 input ArticleCreateInput {
584 blog_id: Int!
585 title: String!
586 content: String
587 }
588  
589 input ArticleSearchInput {
590 title: String
591 content: String
592 }
593  
594 input ArticleIDInput {
595 id: Int!
596 }
597  
598 input ArticleUpdateInput {
599 id: ArticleIDInput!
600 payload: ArticleSearchInput!
601 }
602  
603 type Mutation {
604 createBlog(input: [BlogCreateInput!]!): [Blog]
605 createArticle(input: [ArticleCreateInput!]!): [Article]
606 deleteBlog(input: [BlogIDInput!]!): [Boolean]
607 deleteArticle(input: [ArticleIDInput!]!): [Boolean]
608 updateBlog(input: [BlogUpdateInput!]!): [Blog]
609 updateArticle(input: [ArticleUpdateInput!]!): [Article]
610 }
611  
612 extends type Query {
613 searchBlog(input: BlogSearchInput!): [Blog]
614 searchArticle(input: ArticleSearchInput!): [Article]
615 }
616  
617 =head1 ARGUMENTS
618  
619 To the C<to_graphql> method: a L<DBIx::Class::Schema> object.
620  
621 =head1 PACKAGE FUNCTIONS
622  
623 =head2 field_resolver
624  
625 This is available as C<\&GraphQL::Plugin::Convert::DBIC::field_resolver>
626 in case it is wanted for use outside of the "bundle" of the C<to_graphql>
627 method.
628  
629 =head1 DEBUGGING
630  
631 To debug, set environment variable C<GRAPHQL_DEBUG> to a true value.
632  
633 =head1 AUTHOR
634  
635 Ed J, C<< <etj at cpan.org> >>
636  
637 =head1 LICENSE
638  
639 Copyright (C) Ed J
640  
641 This library is free software; you can redistribute it and/or modify
642 it under the same terms as Perl itself.
643  
644 =cut
645  
646 5 1;
↑ Summary SQL/Translator/Producer/GraphQL.pm »