File Coverage

blib/lib/GraphQL/Plugin/Convert/Test.pm
Criterion Covered Total %
statement 11 11 100.0
branch 2 4 50.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 18 20 90.0


line stmt bran cond sub pod time code
1             package GraphQL::Plugin::Convert::Test;
2              
3 1     1   4184 use Moo;
  1         2  
  1         9  
4 1     1   404 use GraphQL::Schema;
  1         2  
  1         244  
5             extends qw(GraphQL::Plugin::Convert);
6              
7             =head1 NAME
8              
9             GraphQL::Plugin::Convert::Test - GraphQL plugin test class
10              
11             =head1 SYNOPSIS
12              
13             package main;
14             use GraphQL::Plugin::Convert::Test;
15             use GraphQL::Execution qw(execute);
16             my $converted = GraphQL::Plugin::Convert::Test->to_graphql;
17             print execute(
18             $converted->{schema}, '{helloWorld}', $converted->{root_value}
19             )->{data}{helloWorld}, "\n";
20              
21             # show schema from shell
22             perl -Maliased=GraphQL::Plugin::Convert::Test -e 'print Test->to_graphql->{schema}->to_doc'
23              
24             =head1 DESCRIPTION
25              
26             Example class to allow testing of convert plugin consumers.
27              
28             =head1 METHODS
29              
30             Produces a schema and root value that defines the top-level query field
31             C<helloWorld>. That will return the string C<Hello, world!>.
32              
33             Also has a mutation, C<echo>, that takes a String C<s>, and returns it.
34              
35             =head2 to_graphql(@values)
36              
37             If the first value is true, it is a C<subscribe_resolver>,
38             enabling subscriptions in the generated schema. It will be returned
39             as the relevant key in the hash-ref, suitable for being passed as the
40             relevant arg to L<GraphQL::Subscription/subscribe>. The schema will have
41             a subscription field C<timedEcho> that takes a String C<s>, and should
42             return it periodically, in a way determined by the subscription function.
43              
44             =cut
45              
46             sub to_graphql {
47 1     1 1 466 my ($class, $subscribe_resolver) = @_;
48 1         3 my $sdl = <<'EOF';
49             type Query { helloWorld: String! }
50             type Mutation { echo(s: String!): String! }
51             EOF
52 1 50       4 $sdl .= "type Subscription { timedEcho(s: String!): String! }\n"
53             if $subscribe_resolver;
54             +{
55             schema => GraphQL::Schema->from_doc($sdl),
56             root_value => {
57             helloWorld => 'Hello, world!',
58 1     1   5 echo => sub { $_[0]->{s} },
59             },
60 1 50       8 $subscribe_resolver ? (subscribe_resolver => $subscribe_resolver) : (),
61             };
62             }
63              
64             __PACKAGE__->meta->make_immutable();
65              
66             1;