GraphQL/Relationship.pm

filter: Statementstmt 100% 56 / 56 Branchbran 79.1% 19 / 24 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 90.9% 90 / 99 CC 15 SCAR 14.3
CC
15
Cov
93%
build_field
7.0
__ANON__
5.2
new
3.0
CRAP 4.2
« GraphQL.pm ↑ Summary GraphQL/Mutation.pm »
1 package DBIO::GraphQL::Relationship;
2 # ABSTRACT: Resolve GraphQL relationship fields from a DBIO source
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use GraphQL::Type::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # KARR #1 (filed 2026-06-09, low priority): DBIO::GraphQL previously
10 # read two undocumented keys of $source->relationship_info($rel):
11 #
12 # $info->{source} - the target result class
13 # $info->{attrs}{accessor} - 'multi' for has_many, 'single' otherwise
14 #
15 # If either key was missing or absent, the relationship was silently
16 # dropped from the GraphQL type (`or next` in the build loop), with no
17 # error and no warning. A future core change to either of these keys
18 # would have failed invisibly.
19 #
20 # This module wraps the resolution path. The two keys are now treated
21 # as a documented-stable contract; a build-time error is raised when
22 # they are missing. on_error = 'warn' downgrades the error to a warning
23 # and returns undef so the caller can skip the relationship.
24  
25 # schema is the connected DBIO::Schema; required at construction.
26 # on_error is 'die' (default) or 'warn' (downgrade + skip).
27 8 __PACKAGE__->mk_group_accessors(simple => qw(schema on_error));
28  
29 22 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
30 22 my ($class, %args) = @_;
31 die "DBIO::GraphQL::Relationship: 'schema' is required\n"
32 22 ▶ unless exists $args{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"schema"} 0 22 22
33 22 my $self = bless {}, $class;
34 22 $self->schema($args{schema});
35 22 ▶ $self->on_error($args{on_error} // 'die');
Condition100%2/2
$args{"on_error"} // "die"
l!l
121
MC/DC100%1/1
$args{"on_error"} // "die"
$args{"on_error"}
Truth table100%2/2
A
A= $args{"on_error"}
Aresult
11
00
36 22 return $self;
37 }
38  
39 # Build a GraphQL field hashref for the given relationship.
40 # Returns undef when on_error eq 'warn' and a required key is missing
41 # (caller should skip the field); dies otherwise.
42 23 ▶sub build_field {
Subroutine build_field: called CC=7 SCAR=19.5
Pod undocumented
43 23 my ($self, $source, $rel_name, $types_snapshot) = @_;
44 23 my $rel_info = $source->relationship_info($rel_name);
45 23 my $moniker = $source->source_name;
46  
47 23 ▶ unless (defined $rel_info->{source}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rel_info->{"source"}) 2 21 23
48 2 return $self->_fail(
49 "relationship_info for '$rel_name' on source '$moniker' "
50 . "lacks required key 'source' (target result class)"
51 );
52 }
53 21 ▶ unless (ref($rel_info->{attrs}) eq 'HASH' && exists $rel_info->{attrs}{accessor}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $rel_info->{"attrs"} eq "HASH" and exists $rel_info->{"attrs"}{"accessor"}) 1 20 21
Condition66%2/3
ref $rel_info->{"attrs"} eq "HASH" and exists $rel_info->{"attrs"}{"accessor"}
!ll&&!rl&&r
1020
MC/DC50%1/2
ref $rel_info->{"attrs"} eq "HASH" and exists $rel_info->{"attrs"}{"accessor"}
ref $rel_info->{"attrs"} eq "HASH" exists $rel_info->{"attrs"}{"accessor"}
Truth table66%2/3
A and B
A= ref $rel_info->{"attrs"} eq "HASH"B= exists $rel_info->{"attrs"}{"accessor"}
ABresult
0X0
100
111
54 1 return $self->_fail(
55 "relationship_info for '$rel_name' on source '$moniker' "
56 . "lacks required key 'attrs.accessor' (cardinality hint)"
57 );
58 }
59  
60 20 my $target_moniker = $rel_info->{source};
61 20 $target_moniker =~ s/^.*:://;
62 20 my $target_type = $types_snapshot->{$target_moniker};
63 20 ▶ unless ($target_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target_type) 1 19 20
64 1 return $self->_fail(
65 "target type '$target_moniker' (for relationship '$rel_name' on "
66 . "source '$moniker') is not in the type snapshot"
67 );
68 }
69  
70 19 ▶ my $is_plural = ($rel_info->{attrs}{accessor} eq 'multi') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rel_info->{'attrs'}{'accessor'} eq 'multi' ? : 9 10 19
71  
72 return {
73 type => $is_plural
74 ? GraphQL::Type::List->new(of => $target_type)
75 : $target_type,
76 3 ▶ resolve => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
77 3 my ($row, $args, $ctx) = @_;
78 3 ▶ if (ref($row) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row eq "HASH") 3 0 3
79 3 ▶ $row = $self->_pk_find($ctx, $moniker, $row) or return;
Branch: 1 branch
BranchTrueFalseTotal
$row = $self->_pk_find($ctx, $moniker, $row) or return 3 0 3
80 }
81 3 ▶ if ($is_plural) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_plural) { } 1 2 3
82 return [
83 1 map { { $_->get_columns } }
84 $row->$rel_name->all
85 ];
86 }
87 else {
88 2 my $related = $row->$rel_name;
89 2 ▶ return $related ? { $related->get_columns } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$related ? : 2 0 2
90 }
91 },
92 19 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$is_plural ? : 9 10 19
93 }
94  
95 4 ▶sub _fail {
Subroutine _fail: called CC=2 SCAR=6.9
96 4 my ($self, $msg) = @_;
97 4 ▶ if ($self->on_error eq 'warn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_error eq "warn") 1 3 4
98 1 warn "DBIO::GraphQL::Relationship: $msg (skipping)\n";
99 1 return undef;
100 }
101 3 die "DBIO::GraphQL::Relationship: $msg\n";
102 }
103  
104 # Re-look-up a row from a HASH by primary key. Used when a relationship
105 # resolver receives a hashref (the connection-list return shape) rather
106 # than a live DBIO row object.
107 3 ▶sub _pk_find {
Subroutine _pk_find: called CC=2 SCAR=6.9
108 3 my ($self, $ctx, $moniker, $row_hash) = @_;
109 3 my $source = $ctx->source($moniker);
110 3 my @pk_cols = $source->primary_columns;
111 3 my @pk_vals = map { $row_hash->{$_} } @pk_cols;
112 3 ▶ return if grep { !defined } @pk_vals;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @pk_vals 0 3 3
113 return $ctx->resultset($moniker)->find(
114 3 { map { $pk_cols[$_] => $pk_vals[$_] } 0 .. $#pk_cols }
115 );
116 }
117  
118 8 1;
119  
120 __END__
121  
122 =pod
123  
124 =encoding UTF-8
125  
126 =head1 NAME
127  
128 DBIO::GraphQL::Relationship - Resolve GraphQL relationship fields from a DBIO source
129  
130 =head1 VERSION
131  
132 version 0.900001
133  
134 =head1 AUTHOR
135  
136 DBIO Authors
137  
138 =head1 COPYRIGHT AND LICENSE
139  
140 Copyright (C) 2026 DBIO Authors
141  
142 This is free software; you can redistribute it and/or modify it under
143 the same terms as the Perl 5 programming language system itself.
144  
145 =cut
« GraphQL.pm ↑ Summary GraphQL/Mutation.pm »