blib/lib/GraphQL/Houtou/Directive.pm

filter: Statementstmt 84.6% 66 / 78 Branchbran 68.7% 11 / 16 Conditioncond 21.7% 5 / 23 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 81.8% 18 / 22 Podpod 15.3% 2 / 13 totaltotal 62.4% 103 / 165 CC 18 SCAR 17.6
CC
18
Cov
70%
has_executable_location
20.0
has_runtime_hook
12.0
to_doc
7.9
CRAP 5.8
« blib/lib/GraphQL/Houtou/DataLoader.pm ↑ Summary blib/lib/GraphQL/Houtou/Runtime/Slot.pm »
1 package GraphQL::Houtou::Directive;
2  
3 62 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 62 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 62 ▶use parent 'GraphQL::Houtou::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 62 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 62 ▶use GraphQL::Houtou::Internal::TypeSupport qw(apply_fields_deprecation description_doc_lines from_ast_fields make_fieldtuples named_from_ast);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 62 ▶use GraphQL::Houtou::Type::Scalar qw($Boolean $String);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 62 with qw(
14 GraphQL::Houtou::Role::Input
15 );
16  
17 62 ▶use constant DEBUG => $ENV{GRAPHQL_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 62 my @LOCATIONS = qw(
20 QUERY
21 MUTATION
22 SUBSCRIPTION
23 FIELD
24 FRAGMENT_DEFINITION
25 FRAGMENT_SPREAD
26 INLINE_FRAGMENT
27 VARIABLE_DEFINITION
28 SCHEMA
29 SCALAR
30 OBJECT
31 FIELD_DEFINITION
32 ARGUMENT_DEFINITION
33 INTERFACE
34 UNION
35 ENUM
36 ENUM_VALUE
37 INPUT_OBJECT
38 INPUT_FIELD_DEFINITION
39 );
40  
41 459 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
42 459 my ($class, %args) = @_;
43 459 my $self = $class->SUPER::new(%args);
44 459 $self->{name} = $args{name};
45 459 $self->{description} = $args{description};
46 459 ▶ $self->{locations} = $args{locations} || [];
Condition50%1/2
$args{'locations'} || []
l!l
4590
MC/DC0%0/1
$args{'locations'} || []
$args{'locations'}
Truth table50%1/2
A
A= $args{'locations'}
Aresult
11
00
47 459 ▶ $self->{args} = apply_fields_deprecation($args{args} || {});
Condition100%2/2
$args{'args'} || {}
l!l
4536
MC/DC100%1/1
$args{'args'} || {}
$args{'args'}
Truth table100%2/2
A
A= $args{'args'}
Aresult
11
00
48 459 ▶ $self->{repeatable} = $args{repeatable} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'repeatable'} ? : 52 407 459
49 459 $self->{resolve_field} = $args{resolve_field};
50 459 $self->{apply_field_result} = $args{apply_field_result};
51 459 return bless $self, $class;
52 }
53  
54 16436 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
55 5310 ▶sub description { $_[0]->{description} }
Subroutine description: called CC=1 SCAR=0.0
Pod undocumented
56 6610 ▶sub locations { $_[0]->{locations} }
Subroutine locations: called CC=1 SCAR=0.0
Pod undocumented
57 7619 ▶sub args { $_[0]->{args} }
Subroutine args: called CC=1 SCAR=0.0
Pod undocumented
58 5336 ▶sub repeatable { $_[0]->{repeatable} }
Subroutine repeatable: called CC=1 SCAR=0.0
Pod undocumented
59 6 ▶sub resolve_field { $_[0]->{resolve_field} }
Subroutine resolve_field: called CC=1 SCAR=0.0
Pod documented
60 13 ▶sub apply_field_result { $_[0]->{apply_field_result} }
Subroutine apply_field_result: called CC=1 SCAR=0.0
Pod documented
61 0 ▶sub to_string { $_[0]->{to_string} ||= $_[0]->name }
Subroutine to_string: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{"to_string"} ||= $_[0]->name
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"to_string"} ||= $_[0]->name
$_[0]{"to_string"} $_[0]->name
Truth table0%0/3
A ||= B
A= $_[0]{"to_string"}B= $_[0]->name
ABresult
1X1
011
000
62  
63 132 ▶sub from_ast {
Subroutine from_ast: called CC=3 SCAR=11.1
Pod undocumented
64 132 my ($class, $name2type, $ast_node) = @_;
65 132 my (undef, $lazy_args) = from_ast_fields($name2type, $ast_node, 'args');
66 return $class->new(
67 named_from_ast($ast_node),
68 locations => $ast_node->{locations} || [],
69 132 ▶ ($ast_node->{repeatable} ? (repeatable => 1) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$ast_node->{'repeatable'} ? : 49 83 132
Condition50%1/2
$ast_node->{'locations'} || []
l!l
1320
MC/DC0%0/1
$ast_node->{'locations'} || []
$ast_node->{'locations'}
Truth table50%1/2
A
A= $ast_node->{'locations'}
Aresult
11
00
70 args => $lazy_args->(),
71 );
72 }
73  
74 0 ▶sub has_executable_location {
Subroutine has_executable_location: not called CC=4 SCAR=30.0
Pod undocumented
75 0 my ($self) = @_;
76 return !!grep {
77 0 ▶ $_ eq 'FIELD' || $_ eq 'FRAGMENT_SPREAD' || $_ eq 'INLINE_FRAGMENT'
Condition0%0/6
$_ eq 'FIELD' || $_ eq 'FRAGMENT_SPREAD' || $_ eq 'INLINE_FRAGMENT'
l!l&&r!l&&!r
000
$_ eq "FIELD" or $_ eq "FRAGMENT_SPREAD"
l!l&&r!l&&!r
000
MC/DC0%0/3
$_ eq 'FIELD' || $_ eq 'FRAGMENT_SPREAD' || $_ eq 'INLINE_FRAGMENT'
$_ eq "FIELD" $_ eq "FRAGMENT_SPREAD" $_ eq 'INLINE_FRAGMENT'
Truth table0%0/4
A or B || C
A= $_ eq "FIELD"B= $_ eq "FRAGMENT_SPREAD"C= $_ eq 'INLINE_FRAGMENT'
ABCresult
1XX1
01X1
0011
0000
78 0 ▶ } @{ $self->locations || [] };
Condition0%0/2
$self->locations || []
l!l
00
MC/DC0%0/1
$self->locations || []
$self->locations
Truth table0%0/2
A
A= $self->locations
Aresult
11
00
79 }
80  
81 0 ▶sub has_runtime_hook {
Subroutine has_runtime_hook: not called CC=3 SCAR=24.8
Pod undocumented
82 0 my ($self) = @_;
83 0 ▶ return ($self->resolve_field || $self->apply_field_result) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->resolve_field || $self->apply_field_result ? : 0 0 0
Condition0%0/3
$self->resolve_field or $self->apply_field_result
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->resolve_field or $self->apply_field_result
$self->resolve_field $self->apply_field_result
Truth table0%0/3
A or B
A= $self->resolve_fieldB= $self->apply_field_result
ABresult
1X1
011
000
84 }
85  
86 6 ▶sub to_doc {
Subroutine to_doc: called CC=7 SCAR=20.7
Pod undocumented
87 6 my ($self) = @_;
88 6 ▶ return $self->{to_doc} ||= do {
Condition33%1/3
$self->{"to_doc"} ||= do { my(@argtuples) = make_fieldtuples($self->args); my $end = @argtuples ? ")" : ""; $end .= ' repeatable' if $self->repeatable; $end .= ' on ' . join(" | ", @{$self->locations;}); if (not @argtuples) { join "", map(("$_\n"), description_doc_lines($self->description), "directive \@@{[$self->name];}$end"); } else { my(@start) = (description_doc_lines($self->description), "directive \@@{[$self->name];}("); if (not grep($_->[1], @argtuples)) { join("\n", @start) . join(", ", map($_->[0], @argtuples)) . $end . "\n"; } else { join "", map(("$_\n"), @start, map({() = @$_; map((length $_ ? " $_" : ""), @description, $main);} @argtuples), $end); } ; } }
l!l&&r!l&&!r
060
MC/DC0%0/2
$self->{"to_doc"} ||= do { my(@argtuples) = make_fieldtuples($self->args); my $end = @argtuples ? ")" : ""; $end .= ' repeatable' if $self->repeatable; $end .= ' on ' . join(" | ", @{$self->locations;}); if (not @argtuples) { join "", map(("$_\n"), description_doc_lines($self->description), "directive \@@{[$self->name];}$end"); } else { my(@start) = (description_doc_lines($self->description), "directive \@@{[$self->name];}("); if (not grep($_->[1], @argtuples)) { join("\n", @start) . join(", ", map($_->[0], @argtuples)) . $end . "\n"; } else { join "", map(("$_\n"), @start, map({() = @$_; map((length $_ ? " $_" : ""), @description, $main);} @argtuples), $end); } ; } }
$self->{"to_doc"} do { my(@argtuples) = make_fieldtuples($self->args); my $end = @argtuples ? ")" : ""; $end .= ' repeatable' if $self->repeatable; $end .= ' on ' . join(" | ", @{$self->locations;}); if (not @argtuples) { join "", map(("$_\n"), description_doc_lines($self->description), "directive \@@{[$self->name];}$end"); } else { my(@start) = (description_doc_lines($self->description), "directive \@@{[$self->name];}("); if (not grep($_->[1], @argtuples)) { join("\n", @start) . join(", ", map($_->[0], @argtuples)) . $end . "\n"; } else { join "", map(("$_\n"), @start, map({() = @$_; map((length $_ ? " $_" : ""), @description, $main);} @argtuples), $end); } ; } }
Truth table33%1/3
A ||= B
A= $self->{"to_doc"}B= do { my(@argtuples) = make_fieldtuples($self->args); my $end = @argtuples ? ")" : ""; $end .= ' repeatable' if $self->repeatable; $end .= ' on ' . join(" | ", @{$self->locations;}); if (not @argtuples) { join "", map(("$_\n"), description_doc_lines($self->description), "directive \@@{[$self->name];}$end"); } else { my(@start) = (description_doc_lines($self->description), "directive \@@{[$self->name];}("); if (not grep($_->[1], @argtuples)) { join("\n", @start) . join(", ", map($_->[0], @argtuples)) . $end . "\n"; } else { join "", map(("$_\n"), @start, map({() = @$_; map((length $_ ? " $_" : ""), @description, $main);} @argtuples), $end); } ; } }
ABresult
1X1
011
000
89 6 my @argtuples = make_fieldtuples($self->args);
90 6 ▶ my $end = @argtuples ? ')' : '';
Branch: 1 branch
BranchTrueFalseTotal
@argtuples ? : 3 3 6
91 6 ▶ $end .= ' repeatable' if $self->repeatable;
Branch: 1 branch
BranchTrueFalseTotal
if $self->repeatable 3 3 6
92 6 $end .= ' on ' . join(' | ', @{ $self->locations });
93 6 ▶ if (!@argtuples) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @argtuples) { } 3 3 6
94 3 join '', map "$_\n",
95 description_doc_lines($self->description),
96 3 "directive \@@{[$self->name]}$end";
97 }
98 else {
99 3 my @start = (
100 description_doc_lines($self->description),
101 3 "directive \@@{[$self->name]}(",
102 );
103 3 ▶ if (!grep $_->[1], @argtuples) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep($_->[1], @argtuples)) { } 3 0 3
104 3 join("\n", @start) . join(', ', map $_->[0], @argtuples) . $end . "\n";
105 }
106 else {
107 join '', map "$_\n",
108 @start,
109 (map {
110 0 my ($main, @description) = @$_;
111 0 ▶ (map length() ? " $_" : "", @description, $main)
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 0 0 0
112 } @argtuples),
113 $end;
114 }
115 }
116 };
117 }
118  
119 0 ▶sub _get_directive_values {
Subroutine _get_directive_values: not called CC=1 SCAR=6.9
120 0 my ($self) = @_;
121 0 die "Directive->_get_directive_values is part of the removed legacy execution path; use GraphQL::Houtou::Schema->compile_program / ->compile_native_bundle for directive evaluation on '$self->{name}'.\n";
122 }
123  
124 62 our $DEPRECATED = __PACKAGE__->new(
125 name => 'deprecated',
126 description => 'Marks an element of a GraphQL schema as no longer supported.',
127 locations => [ qw(FIELD_DEFINITION ENUM_VALUE ARGUMENT_DEFINITION INPUT_FIELD_DEFINITION) ],
128 args => {
129 reason => {
130 type => $String,
131 description =>
132 'Explains why this element was deprecated, usually also including ' .
133 'a suggestion for how to access supported similar data. Formatted ' .
134 'in [Markdown](https://daringfireball.net/projects/markdown/).',
135 default_value => 'No longer supported',
136 },
137 },
138 );
139  
140 62 our $INCLUDE = __PACKAGE__->new(
141 name => 'include',
142 description => 'Directs the executor to include this field or fragment only when the `if` argument is true.',
143 locations => [ qw(FIELD FRAGMENT_SPREAD INLINE_FRAGMENT) ],
144 args => {
145 if => {
146 type => $Boolean->non_null,
147 description => 'Included when true.',
148 },
149 },
150 );
151  
152 62 our $SKIP = __PACKAGE__->new(
153 name => 'skip',
154 description => 'Directs the executor to skip this field or fragment when the `if` argument is true.',
155 locations => [ qw(FIELD FRAGMENT_SPREAD INLINE_FRAGMENT) ],
156 args => {
157 if => {
158 type => $Boolean->non_null,
159 description => 'Skipped when true.',
160 },
161 },
162 );
163  
164 62 our $SPECIFIED_BY = __PACKAGE__->new(
165 name => 'specifiedBy',
166 description => 'Exposes a URL that specifies the behavior of this scalar.',
167 locations => [ qw(SCALAR) ],
168 args => {
169 url => {
170 type => $String->non_null,
171 description => 'The URL that specifies the behavior of this scalar.',
172 },
173 },
174 );
175  
176 62 our $ONE_OF = __PACKAGE__->new(
177 name => 'oneOf',
178 description => 'Indicates exactly one field must be supplied and this field must not be `null`.',
179 locations => [ qw(INPUT_OBJECT) ],
180 args => {},
181 );
182  
183 62 our @SPECIFIED_DIRECTIVES = (
184 $INCLUDE,
185 $SKIP,
186 $DEPRECATED,
187 $SPECIFIED_BY,
188 $ONE_OF,
189 );
190  
191 62 1;
192  
193 =head1 NAME
194  
195 GraphQL::Houtou::Directive - Directive definitions for GraphQL::Houtou
196  
197 =head1 SYNOPSIS
198  
199 use GraphQL::Houtou::Directive;
200 use GraphQL::Houtou::Schema;
201 use GraphQL::Houtou::Type::Object;
202 use GraphQL::Houtou::Type::Scalar qw($Boolean $String);
203  
204 my $Upper = GraphQL::Houtou::Directive->new(
205 name => 'upper',
206 locations => [qw(FIELD FRAGMENT_SPREAD INLINE_FRAGMENT)],
207 apply_field_result => sub {
208 my ($value) = @_;
209 return undef if !defined $value;
210 return uc $value;
211 },
212 );
213  
214 my $Mask = GraphQL::Houtou::Directive->new(
215 name => 'mask',
216 locations => [qw(FIELD FRAGMENT_SPREAD INLINE_FRAGMENT)],
217 args => {
218 enabled => { type => $Boolean->non_null },
219 },
220 apply_field_result => sub {
221 my ($value, $source, $field_args, $context, $info, $return_type, $directive_args) = @_;
222 return $directive_args->{enabled} ? '***' : $value;
223 },
224 );
225  
226 my $RequireRole = GraphQL::Houtou::Directive->new(
227 name => 'requireRole',
228 locations => [qw(FIELD_DEFINITION)],
229 args => {
230 role => { type => $String->non_null },
231 },
232 resolve_field => sub {
233 my ($next, $source, $field_args, $context, $info, $return_type, $directive_args) = @_;
234 die "forbidden\n" if (($context || {})->{role} || '') ne ($directive_args->{role} || '');
235 return $next->();
236 },
237 );
238  
239 my $schema = GraphQL::Houtou::Schema->new(
240 query => GraphQL::Houtou::Type::Object->new(
241 name => 'ExampleQuery',
242 fields => {
243 hello => {
244 type => $String,
245 resolver_mode => 'native',
246 resolve => sub { 'hello' },
247 },
248 secret => {
249 type => $String,
250 directives => [
251 { name => 'requireRole', arguments => { role => 'admin' } },
252 ],
253 },
254 },
255 ),
256 directives => [
257 @GraphQL::Houtou::Directive::SPECIFIED_DIRECTIVES,
258 $Upper,
259 $Mask,
260 $RequireRole,
261 ],
262 );
263  
264 my $result = $schema->execute(
265 'query Q($enabled: Boolean!) { hello @upper @mask(enabled: $enabled) secret }',
266 root_value => { secret => 'classified' },
267 context => { role => 'admin' },
268 variables => { enabled => 0 },
269 );
270  
271 =head1 DESCRIPTION
272  
273 L<GraphQL::Houtou::Directive> represents a directive definition that can be
274 registered in C<< GraphQL::Houtou::Schema->new(directives => [...]) >>.
275  
276 Executable directives can be implemented in two ways.
277  
278 =over 4
279  
280 =item * C<apply_field_result>
281  
282 Use this for directives that transform an already-resolved field value.
283 This is the lighter-weight hook for query directives such as formatting,
284 masking, or string rewriting.
285  
286 The callback receives:
287  
288 ($value, $source, $field_args, $context, $info, $return_type, $directive_args, $directive)
289  
290 =item * C<resolve_field>
291  
292 Use this for middleware-style directives that need to control whether the
293 field resolver runs at all, wrap the default resolver, or short-circuit
294 execution. This is the right hook for C<FIELD_DEFINITION> directives such as
295 authorization checks.
296  
297 The callback receives:
298  
299 ($next, $source, $field_args, $context, $info, $return_type, $directive_args, $directive)
300  
301 =back
302  
303 If both are present, C<apply_field_result> is preferred for executable runtime
304 directives. In practice, C<apply_field_result> should be the default choice
305 for query/fragment directives, and C<resolve_field> should be reserved for
306 true resolver middleware.
307  
308 For a runnable reference, see F<examples/custom-directives.pl>.
309  
310 =cut
« blib/lib/GraphQL/Houtou/DataLoader.pm ↑ Summary blib/lib/GraphQL/Houtou/Runtime/Slot.pm »