File: Type.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 (85 lines) | stat: -rw-r--r-- 2,055 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
package GraphQL::Plugin::Type;

use Moo;
use GraphQL::MaybeTypeCheck;
use Types::Standard -all;

=head1 NAME

GraphQL::Plugin::Type - GraphQL plugins implementing types

=head1 SYNOPSIS

  package GraphQL::Plugin::Type::DateTime;
  use Moo;
  extends qw(GraphQL::Plugin::Type);
  my $iso8601 = DateTime::Format::ISO8601->new;
  GraphQL::Plugin::Type->register(
    GraphQL::Type::Scalar->new(
      name => 'DateTime',
      serialize => sub { return if !defined $_[0]; $_[0].'' },
      parse_value => sub { return if !defined $_[0]; $iso8601->parse_datetime(@_); },
    )
  );
  1;

  package main;
  use GraphQL::Schema;
  use GraphQL::Plugin::Type::DateTime;
  use GraphQL::Execution qw(execute);
  my $schema = GraphQL::Schema->from_doc(<<'EOF');
  type Query { dateTimeNow: DateTime }
  EOF
  post '/graphql' => sub {
    send_as JSON => execute(
      $schema,
      body_parameters->{query},
      { dateTimeNow => sub { DateTime->now } },
      undef,
      body_parameters->{variables},
      body_parameters->{operationName},
      undef,
    );
  };

=head1 DESCRIPTION

Class implementing the scheme by which additional GraphQL type classes
can be implemented.

The author considers this is only worth doing for scalars, and
indeed this scheme is (now) how the non-standard C<DateTime> is
implemented in graphql-perl. If one wants to create other types
(L<GraphQL::Type::Object>, L<GraphQL::Type::InputObject>, etc), then
the L<Schema Definition Language|GraphQL::Schema/from_doc> is already
available. However, any type can be registered with the L</register>
method, and will be automatically available to L<GraphQL::Schema>
objects with no additional code.

=head1 METHODS

=head2 register($graphql_type)

When called with a L<GraphQL::Type> subclass, will register it,
otherwise dies.

=cut

my @registered;
method register((InstanceOf['GraphQL::Type']) $type) {
  push @registered, $type;
}

=head2 registered

Returns a list of registered classes.

=cut

method registered() {
  @registered;
}

__PACKAGE__->meta->make_immutable();

1;