GraphQL/Relationship.pm

filter: Statementstmt 100% 65 / 65 Branchbran 86.1% 31 / 36 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 91.5% 119 / 130 CC 23 SCAR 19.1
CC
23
Cov
94%
__ANON__
10.2
build_field
9.0
new
3.0
CRAP 6.8
« GraphQL/Filter.pm ↑ Summary GraphQL/Mutation.pm »
1 package DBIO::GraphQL::Relationship;
2 # ABSTRACT: Resolve GraphQL relationship fields from a DBIO source
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 our $VERSION = '0.900007';
7  
8 15 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use GraphQL::Type::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # DBIO::GraphQL reads two keys of $source->relationship_info($rel):
12 #
13 # $info->{source} - the target result class
14 # $info->{attrs}{accessor} - 'multi' for has_many, 'single' otherwise
15 #
16 # This module wraps the resolution path. The two keys are treated as a
17 # documented-stable contract; a build-time error is raised when they are
18 # missing. on_error = 'warn' downgrades the error to a warning and returns
19 # undef so the caller can skip the relationship.
20  
21 # schema is the connected DBIO::Schema; required at construction.
22 # policy (DBIO::GraphQL::Policy) is optional: without one every target
23 # is included and nothing is authorized.
24 # on_error is 'die' (default) or 'warn' (downgrade + skip).
25 15 __PACKAGE__->mk_group_accessors(simple => qw(schema on_error policy));
26  
27 85 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
28 85 my ($class, %args) = @_;
29 die "DBIO::GraphQL::Relationship: 'schema' is required\n"
30 85 ▶ unless exists $args{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"schema"} 0 85 85
31 85 my $self = bless {}, $class;
32 85 $self->schema($args{schema});
33 85 ▶ $self->on_error($args{on_error} // 'die');
Condition100%2/2
$args{"on_error"} // "die"
l!l
184
MC/DC100%1/1
$args{"on_error"} // "die"
$args{"on_error"}
Truth table100%2/2
A
A= $args{"on_error"}
Aresult
11
00
34 85 $self->policy($args{policy});
35 85 return $self;
36 }
37  
38 # Build a GraphQL field hashref for the given relationship.
39 # Returns undef when on_error eq 'warn' and a required key is missing
40 # (caller should skip the field); dies otherwise.
41 100 ▶sub build_field {
Subroutine build_field: called CC=9 SCAR=22.0
Pod undocumented
42 100 my ($self, $source, $rel_name, $types_snapshot) = @_;
43 100 my $rel_info = $source->relationship_info($rel_name);
44 100 my $moniker = $source->source_name;
45  
46 100 ▶ unless (defined $rel_info->{source}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rel_info->{"source"}) 2 98 100
47 2 return $self->_fail(
48 "relationship_info for '$rel_name' on source '$moniker' "
49 . "lacks required key 'source' (target result class)"
50 );
51 }
52 98 ▶ 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 97 98
Condition66%2/3
ref $rel_info->{"attrs"} eq "HASH" and exists $rel_info->{"attrs"}{"accessor"}
!ll&&!rl&&r
1097
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
53 1 return $self->_fail(
54 "relationship_info for '$rel_name' on source '$moniker' "
55 . "lacks required key 'attrs.accessor' (cardinality hint)"
56 );
57 }
58  
59 97 my $target_moniker = $rel_info->{source};
60 97 $target_moniker =~ s/^.*:://;
61  
62 # A source the policy leaves out is gone as a relationship target too.
63 97 ▶ return undef if $self->policy && !$self->policy->included($target_moniker);
Branch: 1 branch
BranchTrueFalseTotal
if $self->policy and not $self->policy->included($target_moniker) 2 95 97
Condition100%3/3
$self->policy and not $self->policy->included($target_moniker)
!ll&&!rl&&r
3922
MC/DC100%2/2
$self->policy and not $self->policy->included($target_moniker)
$self->policy not $self->policy->included($target_moniker)
Truth table100%3/3
A and not(B)
A= $self->policyB= not $self->policy->included($target_moniker)
ABresult
0X0
100
111
64  
65 95 my $target_type = $types_snapshot->{$target_moniker};
66 95 ▶ unless ($target_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target_type) 1 94 95
67 1 return $self->_fail(
68 "target type '$target_moniker' (for relationship '$rel_name' on "
69 . "source '$moniker') is not in the type snapshot"
70 );
71 }
72  
73 94 ▶ my $is_plural = ($rel_info->{attrs}{accessor} eq 'multi') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rel_info->{'attrs'}{'accessor'} eq 'multi' ? : 51 43 94
74  
75 return {
76 type => $is_plural
77 ? GraphQL::Type::List->new(of => $target_type)
78 : $target_type,
79 57 ▶ resolve => sub {
Subroutine __ANON__: called CC=10 SCAR=23.2
80 57 my ($row, $args, $ctx, $info) = @_;
81 57 ▶ $self->policy->check_depth($info) if $self->policy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->policy 57 0 57
82 53 ▶ my $restriction = $self->policy && $self->policy->authorize(
Condition66%2/3
$self->policy && $self->policy->authorize('relationship', $target_moniker, $args, $ctx, $rel_name)
!ll&&!rl&&r
0494
MC/DC50%1/2
$self->policy && $self->policy->authorize('relationship', $target_moniker, $args, $ctx, $rel_name)
$self->policy $self->policy->authorize('relationship', $target_moniker, $args, $ctx, $rel_name)
Truth table66%2/3
A && B
A= $self->policyB= $self->policy->authorize('relationship', $target_moniker, $args, $ctx, $rel_name)
ABresult
0X0
100
111
83 'relationship', $target_moniker, $args, $ctx, $rel_name);
84 52 ▶ if (ref($row) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row eq "HASH") 52 0 52
85 52 ▶ $row = $self->_pk_find($ctx, $moniker, $row) or return;
Branch: 1 branch
BranchTrueFalseTotal
$row = $self->_pk_find($ctx, $moniker, $row) or return 50 2 52
86 }
87 50 ▶ if ($is_plural) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_plural) { } 26 24 50
88 26 my $rs = $row->$rel_name;
89 26 ▶ $rs = $rs->search($restriction) if $restriction;
Branch: 1 branch
BranchTrueFalseTotal
if $restriction 1 25 26
90 26 ▶ $rs = $rs->search(undef, { rows => $self->policy->default_rows })
Branch: 1 branch
BranchTrueFalseTotal
if $self->policy 26 0 26
91 if $self->policy;
92 26 return [ map { { $_->get_columns } } $rs->all ];
93 }
94 else {
95 24 ▶ my $related = $restriction
Branch: 1 branch
BranchTrueFalseTotal
$restriction ? : 2 22 24
96 ? $row->search_related($rel_name)->search($restriction)->first
97 : $row->$rel_name;
98 24 ▶ return $related ? { $related->get_columns } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$related ? : 23 1 24
99 }
100 },
101 94 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$is_plural ? : 51 43 94
102 }
103  
104 4 ▶sub _fail {
Subroutine _fail: called CC=2 SCAR=6.9
105 4 my ($self, $msg) = @_;
106 4 ▶ if ($self->on_error eq 'warn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_error eq "warn") 1 3 4
107 1 warn "DBIO::GraphQL::Relationship: $msg (skipping)\n";
108 1 return undef;
109 }
110 3 die "DBIO::GraphQL::Relationship: $msg\n";
111 }
112  
113 # Re-look-up a row from a HASH by primary key. Used when a relationship
114 # resolver receives a hashref (the connection-list return shape) rather
115 # than a live DBIO row object.
116 52 ▶sub _pk_find {
Subroutine _pk_find: called CC=3 SCAR=11.0
117 52 my ($self, $ctx, $moniker, $row_hash) = @_;
118 52 my $source = $ctx->source($moniker);
119 52 my @pk_cols = $source->primary_columns;
120 52 ▶ return unless @pk_cols; # a keyless source has no row identity to look up
Branch: 1 branch
BranchTrueFalseTotal
unless @pk_cols 2 50 52
121 50 my @pk_vals = map { $row_hash->{$_} } @pk_cols;
122 50 ▶ return if grep { !defined } @pk_vals;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @pk_vals 0 50 50
123 return $ctx->resultset($moniker)->find(
124 50 { map { $pk_cols[$_] => $pk_vals[$_] } 0 .. $#pk_cols }
125 );
126 }
127  
128 15 1;
129  
130 __END__
« GraphQL/Filter.pm ↑ Summary GraphQL/Mutation.pm »