GraphQL/Plugin/Type.pm

filter: Statementstmt 100% 26 / 26 Branchbran 50.0% 5 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 88.3% 38 / 43 CC 6 SCAR 10.7
CC
6
Cov
86%
register
4.2
registered
3.1
BEGIN
1.0
CRAP 2.9
« GraphQL/Type/Union.pm ↑ Summary GraphQL/Type/NonNull.pm »
1 package GraphQL::Plugin::Type;
2  
3 18 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use GraphQL::MaybeTypeCheck;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Types::Standard -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 18 my @registered;
69 93 ▶method register((InstanceOf['GraphQL::Type']) $type) {
Subroutine register: called CC=4 SCAR=14.3
Pod documented
Branch: 3 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method register (expected 2, got ' . @_ . ')') 0 93 93
@_ > 2 and Function::Parameters::_croak('Too many arguments for method register (expected 2, got ' . @_ . ')') 0 93 93
Type::Tiny::XS::AUTO::TC9($type) or Function::Parameters::_croak('In method register: parameter 1 ($type): ' . ${my $v = bless( {"_overload_coderef" => sub { package Sub::Defer; use warnings; use strict; $undeferred ||= undefer_sub($deferred_info->[4]); goto \&$undeferred;},"_overload_coderef_no_rebuild" => 1,"_overrides_assert_return" => !!0,"class" => "GraphQL::Type","coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 985,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "InstanceOf[\"GraphQL::Type\"]","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Type::Tiny::Class; use warnings; use strict; my $var = $_[1]; return "do { use Scalar::Util (); Scalar::Util::blessed($var) and $var->isa(q[$class]) }" if $Type::Tiny::AvoidCallbacks; return "$xsub($var)" if $xsub; "Scalar::Util::blessed($var) and $var->isa(q[$class])";},"name" => "__ANON__","parent" => bless( {"compiled_type_constraint" => sub ;,"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Method/Generate/Accessor.pm","line" => 716,"package" => "Method::Generate::Accessor"},"deprecated" => !!0,"exception_class" => "Error::TypeTiny::Assertion","name" => "__ANON__","type_constraints" => [bless( {"class" => "Moo::Object","compiled_type_constraint" => sub ;,"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Method/Generate/Accessor.pm","line" => 716,"package" => "Method::Generate::Accessor"},"deprecated" => !!0,"exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Type::Tiny::Class; use warnings; use strict; my $var = $_[1]; return "do { use Scalar::Util (); Scalar::Util::blessed($var) and $var->isa(q[$class]) }" if $Type::Tiny::AvoidCallbacks; return "$xsub($var)" if $xsub; "Scalar::Util::blessed($var) and $var->isa(q[$class])";},"name" => "__ANON__","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; Ref::Util::XS::is_blessed_ref($_[0]) or _failed_check(24, 'Object', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Object","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; builtin::blessed($_);},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 578,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Object","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; !$Type::Tiny::AvoidCallbacks ? "Ref::Util::XS::is_blessed_ref($_[1])" : &$maybe_load_modules('Scalar::Util', "Scalar::Util::blessed($_[1])");},"is_object" => 1,"library" => "Types::Standard","name" => "Object","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; not ref $_[0] and _failed_check(16, 'Ref', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Ref","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Types::Standard; no warnings; use strict; ref $_;},"constraint_generator" => sub { package Types::Standard; no warnings; use strict; return $meta->get_type('Ref') unless @_; Type::Tiny::check_parameter_count_for_parameterized_type('Types::Standard', 'Ref', \@_, 1); my $reftype = shift(); $reftype =~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE|FORMAT|IO|VSTRING|REGEXP|Regexp)$/i or _croak "Parameter to Ref[`a] expected to be a Perl ref type; got $reftype"; $reftype = "$reftype"; return sub { ref $_[0] and builtin::reftype($_[0]) eq $reftype; } ;},"deep_explanation" => sub { package Types::Standard; no warnings; use strict; require B; my($type, $value, $varname) = @_; my $param = $type->parameters->[0]; return if $type->check($value); my $reftype = builtin::reftype($value); return [sprintf('"%s" constrains reftype(%s) to be equal to %s', $type, $varname, B::perlstring($param)), sprintf('reftype(%s) is %s', $varname, defined $reftype ? B::perlstring($reftype) : 'undef')];},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 438,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Ref","exception_class" => "Error::TypeTiny::Assertion","inline_generator" => sub { package Types::Standard; no warnings; use strict; my $reftype = shift(); return sub { my $v = $_[1]; &$maybe_load_modules('Scalar::Util', "ref($v) and Scalar::Util::reftype($v) eq q($reftype)"); } ;},"inlined" => sub { package Types::Standard; no warnings; use strict; "!!ref($_[1])";},"library" => "Types::Standard","name" => "Ref","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; defined $_[0] or _failed_check(7, 'Defined', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Defined","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complementary_type" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; defined $_[0] and _failed_check(6, 'Undef', $_[0], 'exception_class', 'Error::TypeTiny::Assertion'); $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Undef","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complementary_type" => {},"constraint" => sub { package Types::Standard; no warnings; use strict; not defined $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 254,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Undef","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "!defined($_[1])";},"library" => "Types::Standard","name" => "Undef","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Type::Tiny; BEGIN {${^WARNING_BITS} = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x01\x40\x05\x45\x55\x15\x55\x55\x01\x00"} use strict; '???'; $_[0];},"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => sub ;,"display_name" => "to_Item","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"constraint" => sub { package Type::Tiny; use warnings; use strict; !0;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 226,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Item","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; '!!1';},"library" => "Types::Standard","name" => "Item","parent" => bless( {"_is_core" => 1,"_overload_coderef" => sub { package Sub::Defer; use warnings; use strict; $undeferred ||= undefer_sub($deferred_info->[4]); goto \&$undeferred;},"_overload_coderef_no_rebuild" => 1,"_overrides_assert_return" => !!0,"coercion" => bless( {"_compiled_type_constraint_check" => do{my $o},"display_name" => "to_Any","frozen" => 1,"name" => "__ANON__","type_coercion_map" => [],"type_constraint" => {}}, 'Type::Coercion' ),"compiled_type_constraint" => do{my $o},"complement_name" => "None","constraint" => do{my $o},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 220,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Any","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; '!!1';},"library" => "Types::Standard","name" => "Any","type_default" => sub { package Types::Standard; no warnings; use strict; return undef;},"uniq" => 1}, 'Type::Tiny' ),"uniq" => 2}, 'Type::Tiny' ),"type_default" => sub { package Types::Standard; no warnings; use strict; return undef;},"uniq" => 6}, 'Type::Tiny' ),"constraint" => sub { package Types::Standard; no warnings; use strict; defined $_;},"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Types/Standard.pm","line" => 262,"package" => "Types::Standard"},"deprecated" => !!0,"display_name" => "Defined","exception_class" => "Error::TypeTiny::Assertion","inlined" => sub { package Types::Standard; no warnings; use strict; "defined($_[1])";},"library" => "Types::Standard","name" => "Defined","parent" => {},"uniq" => 7}, 'Type::Tiny' ),"uniq" => 16}, 'Type::Tiny' ),"uniq" => 24}, 'Type::Tiny' ),"uniq" => 103}, 'Type::Tiny::Class' ),bless( {"class" => "GraphQL::MaybeTypeCheck","compiled_type_constraint" => sub ;,"definition_context" => {"file" => "/usr/local/lib/perl5/site_perl/5.44.0/Method/Generate/Accessor.pm","line" => 716,"package" => "Method::Generate::Accessor"},"deprecated" => !!0,"inlined" => sub { package Type::Tiny::Class; use warnings; use strict; my $var = $_[1]; return "do { use Scalar::Util (); Scalar::Util::blessed($var) and $var->isa(q[$class]) }" if $Type::Tiny::AvoidCallbacks; return "$xsub($var)" if $xsub; "Scalar::Util::blessed($var) and $var->isa(q[$class])";},"name" => "__ANON__","uniq" => 104}, 'Type::Tiny::Class' )],"uniq" => 105,"xs_sub" => "Type::Tiny::XS::AUTO::TC12"}, 'Type::Tiny::Intersection' ),"uniq" => 101}, 'Type::Tiny::Class' );$v->{"coercion"}{"type_constraint"} = $v;$v->{"compiled_type_constraint"} = $v->{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"compiled_type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"complementary_type"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"coercion"}{"type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"compiled_type_constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"coercion"}{"_compiled_type_constraint_check"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"parent"}{"constraint"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}{"constraint"};$v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"parent"} = $v->{"parent"}{"type_constraints"}[0]{"parent"}{"parent"}{"parent"}{"complementary_type"}{"parent"}; \$v}->get_message($type)) 93 0 93
70 93 push @registered, $type;
71 18 }
72  
73 =head2 registered
74  
75 Returns a list of registered classes.
76  
77 =cut
78  
79 178 ▶method registered() {
Subroutine registered: called CC=3 SCAR=11.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method registered (expected 1, got ' . @_ . ')') 0 178 178
@_ > 1 and Function::Parameters::_croak('Too many arguments for method registered (expected 1, got ' . @_ . ')') 0 178 178
80 178 @registered;
81 18 }
82  
83 18 __PACKAGE__->meta->make_immutable();
84  
85 18 1;
« GraphQL/Type/Union.pm ↑ Summary GraphQL/Type/NonNull.pm »