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