File Coverage

blib/lib/GraphQL/Type.pm
Criterion Covered Total %
statement 40 40 100.0
branch 10 18 55.5
condition 2 2 100.0
subroutine 10 10 100.0
pod 1 1 100.0
total 63 71 88.7


line stmt bran cond sub pod time code
1              
2             use 5.014;
3 18     18   10110 use strict;
  18         59  
4 18     18   113 use warnings;
  18         32  
  18         391  
5 18     18   74 use Moo;
  18         54  
  18         527  
6 18     18   101 use GraphQL::MaybeTypeCheck;
  18         34  
  18         230  
7 18     18   6402 use Types::Standard qw(InstanceOf Any HashRef Str); # if -all causes objects to be class 'Object'!
  18         35  
  18         122  
8 18     18   97 with 'GraphQL::Role::Listable';
  18         40  
  18         134  
9              
10             our $VERSION = '0.02';
11              
12             =head1 NAME
13              
14             GraphQL::Type - GraphQL type object
15              
16             =head1 SYNOPSIS
17              
18             extends qw(GraphQL::Type);
19              
20             =head1 DESCRIPTION
21              
22             Superclass for other GraphQL type classes to inherit from.
23              
24             =head1 ENCODING
25              
26             Those Perl classes each implement a GraphQL type. Each item of
27             GraphQL data has a GraphQL type. Such an item of data can also be
28             represented within Perl. Objects of that Perl class take responsibility
29             for translating between the Perl representation and the "GraphQL
30             representation". A "GraphQL representation" means something
31             JSON-encodeable: an "object" (in Perl terms, a hash), an array (Perl:
32             array-reference), string, number, boolean, or null.
33              
34             See L</METHODS> for generic methods to translate back and forth between
35             these worlds.
36              
37             Code that you provide to do this translation must return things that
38             I<can> be JSON-encoded, not things that I<have been> so encoded: this
39             means, among other things, do not surround strings in C<">, and for
40             boolean values, use the mechanism in L<JSON::MaybeXS>: C<JSON->true> etc.
41              
42             =head1 SUBCLASSES
43              
44             These subclasses implement part of the GraphQL language
45             specification. Objects of these classes implement user-defined types
46             used to implement a GraphQL API.
47              
48             =over
49              
50             =item L<GraphQL::Type::Enum>
51              
52             =item L<GraphQL::Type::InputObject>
53              
54             =item L<GraphQL::Type::Interface>
55              
56             =item L<GraphQL::Type::List>
57              
58             =item L<GraphQL::Type::NonNull>
59              
60             =item L<GraphQL::Type::Object>
61              
62             =item L<GraphQL::Type::Scalar> - also implements example types such as C<String>
63              
64             =item L<GraphQL::Type::Union>
65              
66             =back
67              
68             =head1 ROLES
69              
70             These roles implement part of the GraphQL language
71             specification. They are applied to objects of L<GraphQL::Type> classes,
72             either to facilitate type constrants, or as noted below.
73              
74             =over
75              
76             =item L<GraphQL::Role::FieldsInput> - provides C<fields> attribute for an input type
77              
78             =item L<GraphQL::Role::FieldsOutput> - provides C<fields> attribute for an output type
79              
80             =item L<GraphQL::Role::Abstract> - abstract type
81              
82             =item L<GraphQL::Role::Composite> - type has fields
83              
84             =item L<GraphQL::Role::Input> - type can be an input
85              
86             =item L<GraphQL::Role::Leaf> - simple type - enum or scalar
87              
88             =item L<GraphQL::Role::Listable> - can be list-wrapped; provides convenience method
89              
90             =item L<GraphQL::Role::Named> - has a C<name> and C<description>, provided by this role
91              
92             =item L<GraphQL::Role::Nullable> - can be null-valued
93              
94             =item L<GraphQL::Role::Output> - type can be an output
95              
96             =back
97              
98             =head1 TYPE LIBRARY
99              
100             L<GraphQL::Type::Library> - implements various L<Type::Tiny>
101             type constraints, for use in L<Moo> attributes, and
102             L<Function::Parameters>/L<Return::Type> methods and functions.
103              
104             =head1 METHODS
105              
106             =head2 uplift
107              
108             Turn given Perl entity into valid Perl value for this type if possible.
109              
110             =cut
111              
112             method uplift(Any $item) :ReturnType(Any) { $item; }
113 18 50   18 1 16391  
  18 50   47   34  
  18 50       101  
  47         88  
  47         68  
  47         52  
  47         65  
  47         75  
  47         251  
  47         104  
114             =head2 graphql_to_perl
115              
116             Turn given GraphQL entity into Perl entity.
117              
118             =head2 perl_to_graphql
119              
120             Turn given Perl entity into GraphQL entity.
121              
122             =cut
123              
124             =head2 from_ast($name2type, $ast_node)
125              
126             Class method. C<$name2type> is a hash-ref populated by
127             L<GraphQL::Schema/from_ast>. Takes a hash-ref node from an AST made by
128             L<GraphQL::Language::Parser/parse>. Returns a type object.
129              
130             =head2 to_doc($doc)
131              
132             Returns Schema Definition Language (SDL) document that describes this
133             object.
134              
135             =cut
136              
137             method _from_ast_maptype(
138             HashRef $name2type,
139             HashRef $ast_node,
140             Str $key,
141             ) {
142 74 50   74   210 return if !$ast_node->{$key};
  74 50       169  
  74 50       120  
  74 50       143  
  74 50       164  
  74         513  
  74         486  
  74         834  
143 74 100       423 ($key => sub { [
144             map { $name2type->{$_} // die "Unknown type '$_'.\n" } @{$ast_node->{$key}}
145 9   100 9   841 ] });
  10         191  
  9         35  
146 9         182 }
147              
148             __PACKAGE__->meta->make_immutable();
149              
150             1;