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