File: NonNull.pm

package info (click to toggle)
libgraphql-perl 0.54-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 712 kB
  • sloc: perl: 5,094; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 1,654 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package GraphQL::Type::NonNull;

use 5.014;
use strict;
use warnings;
use Moo;
use Types::Standard -all;
use GraphQL::MaybeTypeCheck;
extends qw(GraphQL::Type);

our $VERSION = '0.02';

# A-ha
my @TAKE_ON_ME = qw(
  GraphQL::Role::Input
  GraphQL::Role::Output
);

=head1 NAME

GraphQL::Type::NonNull - GraphQL type that is a non-null version of another type

=head1 SYNOPSIS

  use GraphQL::Type::NonNull;
  my $type = GraphQL::Type::NonNull->new(of => $other_type);

=head1 DESCRIPTION

Type that is a wrapper for another type. Means data cannot be a null value.

=head1 ATTRIBUTES

=head2 of

GraphQL type object of which this is a non-null version.

=cut

has of => (
  is => 'ro',
  isa => InstanceOf['GraphQL::Type'],
  required => 1,
  handles => [ qw(name) ],
);

=head1 METHODS

=head2 BUILD

L<Moo> method that applies the relevant roles.

=cut

sub BUILD {
  my ($self, $args) = @_;
  my $of = $self->of;
  Role::Tiny->apply_roles_to_object($self, grep $of->DOES($_), @TAKE_ON_ME);
}

=head2 to_string

Part of serialisation.

=cut

has to_string => (is => 'lazy', isa => Str, init_arg => undef, builder => sub {
  my ($self) = @_;
  $self->of->to_string . '!';
});

=head2 is_valid

True if given Perl value is a valid value for this type.

=cut

method is_valid(Any $item) :ReturnType(Bool) {
  return if !defined $item or !$self->of->is_valid($item);
  1;
}

method graphql_to_perl(Any $item) :ReturnType(Any) {
  my $of = $self->of;
  $of->graphql_to_perl($item) // die $self->to_string . " given null value.\n";
}

=head2 name

The C<name> of the type this object is a non-null version of.

=cut

__PACKAGE__->meta->make_immutable();

1;