File: directives.t

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 (108 lines) | stat: -rw-r--r-- 2,590 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
97
98
99
100
101
102
103
104
105
106
107
108
use lib 't/lib';
use GQLTest;

BEGIN {
  use_ok( 'GraphQL::Schema' ) || print "Bail out!\n";
  use_ok( 'GraphQL::Execution', qw(execute) ) || print "Bail out!\n";
}

my $doc = q<
  directive @length(max: Int) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
  directive @note(msg: Str) on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION
    | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE
    | INPUT_OBJECT | INPUT_FIELD_DEFINITION

  type Todo {
    task: String! @length(max: 15)
  }

  type Query {
    todos: [Todo]
  }

  type Mutation @length(max: 15) {
    add_todo(task: String! @length(max: 15)): Todo
  }

  schema @note(msg: "Test") {
    query: Query
    mutation: Mutation
  }

>;

ok my $schema = GraphQL::Schema->from_doc($doc);

my @orig_data = my @data = (
  {task => 'Exercise!'},
  {task => 'Bulk Milk'},
  {task => 'Walk Dogs'},
);
my $add_task = { task => 'milk1' };
my $bad_task = { task => 'milk1milk1milk1milk1' };

my %root_value = (
  todos => sub {
    return \@data;
  },
  add_todo => sub {
    my ($args, $context, $info) = @_;
    my $task = $args->{task}; # hardcoding arg name for simplicity
    my $length_dir = _get_directive($info, 'task', 'length');
    if ($length_dir) {
      my $max = $length_dir->{arguments}{max};
      die "Length of '$task' > max=$max\n" if length $task > $max;
    }
    my $data = { task => $task };
    push @data, $data;
    return $data;
  }
);

sub _get_directive {
  my ($info, $arg, $name) = @_;
  my $parent_type = $info->{parent_type};
  my $field_def = $parent_type->fields->{$info->{field_name}};
  my $arg_directives = $field_def->{args}{$arg}{directives}; # would autovivify
  my ($directive) = grep $_->{name} eq $name, @$arg_directives;
  $directive;
}

my $q = q<
  mutation m($task: String!) {
    add_todo(task: $task) { task }
  }
  query q {
    todos { task }
  }
>;

subtest 'basic directives' => sub {
  run_test(
    [$schema, $q, \%root_value, undef, undef, 'q'],
    { data => { todos => \@orig_data } },
  );
  run_test(
    [$schema, $q, \%root_value, undef, $add_task, 'm'],
    { data => { add_todo => $add_task } },
  );
  run_test(
    [$schema, $q, \%root_value, undef, $bad_task, 'm'],
    {
      data => { add_todo => undef },
      errors => [
        {
          locations => [ { column => 3, line => 4 } ],
          message => "Length of 'milk1milk1milk1milk1' > max=15\n",
          path => [ 'add_todo' ]
        }
      ]
    },
  );
  run_test(
    [$schema, $q, \%root_value, undef, undef, 'q'],
    { data => { todos => [ @orig_data, $add_task ] } },
  );
};

done_testing;