File Coverage

blib/lib/GraphQL/Role/FieldsEither.pm
Criterion Covered Total %
statement 92 92 100.0
branch 37 56 66.0
condition 2 2 100.0
subroutine 14 14 100.0
pod n/a
total 145 164 88.4


line stmt bran cond sub pod time code
1             package GraphQL::Role::FieldsEither;
2              
3 18     18   11771 use 5.014;
  18         68  
4 18     18   113 use strict;
  18         353  
  18         407  
5 18     18   81 use warnings;
  18         40  
  18         453  
6 18     18   88 use Moo::Role;
  18         42  
  18         130  
7 18     18   6642 use GraphQL::Debug qw(_debug);
  18         43  
  18         1050  
8 18     18   124 use GraphQL::MaybeTypeCheck;
  18         35  
  18         137  
9 18     18   98 use Types::Standard -all;
  18         40  
  18         148  
10 18     18   792123 use JSON::MaybeXS;
  18         55  
  18         2281  
11             with qw(GraphQL::Role::FieldDeprecation);
12              
13             our $VERSION = '0.02';
14 18     18   128 use constant DEBUG => $ENV{GRAPHQL_DEBUG};
  18         45  
  18         2108  
15             my $JSON_noutf8 = JSON::MaybeXS->new->utf8(0)->allow_nonref;
16              
17             =head1 NAME
18              
19             GraphQL::Role::FieldsEither - GraphQL object role with code common to all fields
20              
21             =head1 SYNOPSIS
22              
23             with qw(GraphQL::Role::FieldsEither);
24              
25             # or runtime
26             Role::Tiny->apply_roles_to_object($foo, qw(GraphQL::Role::FieldsEither));
27              
28             =head1 DESCRIPTION
29              
30             Provides code useful to either type of fields.
31              
32             =cut
33              
34             method _make_field_def(
35             HashRef $name2type,
36             Str $field_name,
37             HashRef $field_def,
38 127 50   127   433 ) {
  127 50       349  
  127 50       218  
  127 50       306  
  127 50       348  
  127         1272  
  127         1368  
  127         793  
39 127         156 DEBUG and _debug('FieldsEither._make_field_def', $field_def);
40 127         691 require GraphQL::Schema;
41 127         213 my %args;
42             %args = (args => +{
43             map $self->_make_field_def($name2type, $_, $field_def->{args}{$_}),
44 22         149 keys %{$field_def->{args}}
45 127 100       328 }) if $field_def->{args};
46 127         1094 ($_ => {
47             %$field_def,
48             type => GraphQL::Schema::lookup_type($field_def, $name2type),
49             %args,
50             });
51             }
52              
53             method _from_ast_fields(
54             HashRef $name2type,
55             HashRef $ast_node,
56             Str $key,
57 87 50   87   261 ) {
  87 50       246  
  87 50       163  
  87 50       210  
  87 50       225  
  87         708  
  87         690  
  87         813  
58 87         213 my $fields = $ast_node->{$key};
59 87         982 $fields = $self->_from_ast_field_deprecate($_, $fields) for keys %$fields;
60             (
61             $key => sub { +{
62             map {
63 74     74   9434 my @pair = eval {
  99         234  
64 99         436 $self->_make_field_def($name2type, $_, $fields->{$_})
65             };
66 99 100       2289 die "Error in field '$_': $@" if $@;
67 97         1964 @pair;
68             } keys %$fields
69             } },
70 87         3382 );
71             }
72              
73             method _description_doc_lines(
74             Maybe[Str] $description,
75 153 50   153   369 ) {
  153 50       309  
  153 50       241  
  153         344  
  153         395  
  153         1504  
76 153         187 DEBUG and _debug('FieldsEither._description_doc_lines', $description);
77 153 100       653 return if !$description;
78 8 50       44 my @lines = $description ? split /\n/, $description : ();
79 8 50       21 return if !@lines;
80 8 100       28 if (@lines == 1) {
    50          
81 7         46 return '"' . ($lines[0] =~ s#"#\\"#gr) . '"';
82             } elsif (@lines > 1) {
83             return (
84 1         12 '"""',
85             (map s#"""#\\"""#gr, @lines),
86             '"""',
87             );
88             }
89             }
90              
91             method _make_fieldtuples(
92             HashRef $fields,
93 134 50   134   1151 ) {
  134 50       334  
  134 50       199  
  134         242  
  134         388  
  134         1008  
94 134         197 DEBUG and _debug('FieldsEither._make_fieldtuples', $fields);
95             map {
96 134         424 my $field = $fields->{$_};
  88         280  
97             my @argtuples = map $_->[0],
98 88   100     475 $self->_make_fieldtuples($field->{args} || {});
99 88         238 my $type = $field->{type};
100 88         137 my $line = $_;
101 88 100       293 $line .= '('.join(', ', @argtuples).')' if @argtuples;
102 88         1760 $line .= ': ' . $type->to_string;
103             $line .= ' = ' . $JSON_noutf8->encode(
104             $type->perl_to_graphql($field->{default_value})
105 88 100       1779 ) if exists $field->{default_value};
106             my @directives = map {
107 4         11 my $args = $_->{arguments};
108 4         12 my @argtuples = map { "$_: " . $JSON_noutf8->encode($args->{$_}) } keys %$args;
  1         18  
109 4 100       27 '@' . $_->{name} . (@argtuples ? '(' . join(', ', @argtuples) . ')' : '');
110 88 100       143 } @{ $field->{directives} || [] };
  88         465  
111 88 100       261 $line .= join(' ', ('', @directives)) if @directives;
112             [
113             $self->_to_doc_field_deprecate($line, $field),
114 88         321 $self->_description_doc_lines($field->{description}),
115             ]
116             } sort keys %$fields;
117             }
118              
119             __PACKAGE__->meta->make_immutable();
120              
121             1;