File Coverage

blib/lib/GraphQL/Plugin/Type.pm
Criterion Covered Total %
statement 21 21 100.0
branch 5 10 50.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 33 38 86.8


line stmt bran cond sub pod time code
1             package GraphQL::Plugin::Type;
2              
3 18     18   141 use Moo;
  23         116  
  23         214  
4 18     18   6429 use GraphQL::MaybeTypeCheck;
  18         45  
  18         109  
5 18     18   102 use Types::Standard -all;
  18         38  
  18         143  
6              
7             =head1 NAME
8              
9             GraphQL::Plugin::Type - GraphQL plugins implementing types
10              
11             =head1 SYNOPSIS
12              
13             package GraphQL::Plugin::Type::DateTime;
14             use Moo;
15             extends qw(GraphQL::Plugin::Type);
16             my $iso8601 = DateTime::Format::ISO8601->new;
17             GraphQL::Plugin::Type->register(
18             GraphQL::Type::Scalar->new(
19             name => 'DateTime',
20             serialize => sub { return if !defined $_[0]; $_[0].'' },
21             parse_value => sub { return if !defined $_[0]; $iso8601->parse_datetime(@_); },
22             )
23             );
24             1;
25              
26             package main;
27             use GraphQL::Schema;
28             use GraphQL::Plugin::Type::DateTime;
29             use GraphQL::Execution qw(execute);
30             my $schema = GraphQL::Schema->from_doc(<<'EOF');
31             type Query { dateTimeNow: DateTime }
32             EOF
33             post '/graphql' => sub {
34             send_as JSON => execute(
35             $schema,
36             body_parameters->{query},
37             { dateTimeNow => sub { DateTime->now } },
38             undef,
39             body_parameters->{variables},
40             body_parameters->{operationName},
41             undef,
42             );
43             };
44              
45             =head1 DESCRIPTION
46              
47             Class implementing the scheme by which additional GraphQL type classes
48             can be implemented.
49              
50             The author considers this is only worth doing for scalars, and
51             indeed this scheme is (now) how the non-standard C<DateTime> is
52             implemented in graphql-perl. If one wants to create other types
53             (L<GraphQL::Type::Object>, L<GraphQL::Type::InputObject>, etc), then
54             the L<Schema Definition Language|GraphQL::Schema/from_doc> is already
55             available. However, any type can be registered with the L</register>
56             method, and will be automatically available to L<GraphQL::Schema>
57             objects with no additional code.
58              
59             =head1 METHODS
60              
61             =head2 register($graphql_type)
62              
63             When called with a L<GraphQL::Type> subclass, will register it,
64             otherwise dies.
65              
66             =cut
67              
68             my @registered;
69 93 50   93 1 683 method register((InstanceOf['GraphQL::Type']) $type) {
  93 50       188  
  93 50       172  
  93         172  
  93         341  
  88         1860  
70 88         259 push @registered, $type;
71             }
72              
73             =head2 registered
74              
75             Returns a list of registered classes.
76              
77             =cut
78              
79 178 50   178 1 796 method registered() {
  178 50       607  
  178         405  
  178         284  
80 178         2270 @registered;
81             }
82              
83             __PACKAGE__->meta->make_immutable();
84              
85             1;