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