File: execution-resolve.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 (86 lines) | stat: -rw-r--r-- 2,593 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
use lib 't/lib';
use GQLTest;

BEGIN {
  use_ok( 'GraphQL::Schema' ) || print "Bail out!\n";
  use_ok( 'GraphQL::Type::Object' ) || print "Bail out!\n";
  use_ok( 'GraphQL::Type::Scalar', qw($String $Int) ) || print "Bail out!\n";
  use_ok( 'GraphQL::Execution', qw(execute) ) || print "Bail out!\n";
}

my $JSON = JSON::MaybeXS->new->allow_nonref->canonical;

sub make_schema {
  my ($field) = @_;
  GraphQL::Schema->new(
    query => GraphQL::Type::Object->new(
      name => 'Query',
      fields => { test => $field },
    ),
  );
}

subtest 'default function accesses properties', sub {
  my $schema = make_schema({ type => $String });
  my $root_value = { test => 'testvalue' };
  my $expected = { %$root_value }; # copy in case of mutations
  run_test([$schema, '{ test }', $root_value], { data => $expected });
  done_testing;
};

subtest 'default function calls methods', sub {
  my $schema = make_schema({ type => $String });
  use constant SECRETVAL => 'secretValue';
  {
    package MyTest1;
    sub new { bless { _secret => ::SECRETVAL }, shift; }
    sub test { shift->{_secret} }
  }
  my $root_value = MyTest1->new;
  is $root_value->test, SECRETVAL; # fingers and toes
  run_test([$schema, '{ test }', $root_value], { data => { test => SECRETVAL } });
  done_testing;
};

subtest 'default function passes args and context', sub {
  my $schema = make_schema({
    type => $Int,
    args => { addend1 => { type => $Int } },
  });
  {
    package Adder;
    sub new { bless { _num => $_[1] }, $_[0]; }
    sub test { shift->{_num} + shift->{addend1} + shift->{addend2} }
  }
  my $root_value = Adder->new(700);
  is $root_value->test({ addend1 => 80 }, { addend2 => 9 }), 789;
  run_test(
    [$schema, '{ test(addend1: 80) }', $root_value, { addend2 => 9 }],
    { data => { test => 789 } },
  );
  done_testing;
};

subtest 'uses provided resolve function', sub {
  my $schema = make_schema({
    type => $String,
    args => { aStr => { type => $String }, aInt => { type => $Int } },
    resolve => sub {
      my ($root_value, $args, $context, $info) = @_;
      $JSON->encode([$root_value, $args]);
    },
  });
  run_test([$schema, '{ test }'], { data => { test => '[null,{}]' } });
  run_test([$schema, '{ test }', '!'], { data => { test => '["!",{}]' } });
  run_test(
    [$schema, '{ test(aStr: "String!") }', 'Info'],
    { data => { test => '["Info",{"aStr":"String!"}]' } },
  );
  run_test(
    [$schema, '{ test(aStr: "String!", aInt: -123) }', 'Info'],
    { data => { test => '["Info",{"aInt":-123,"aStr":"String!"}]' } },
  );
  done_testing;
};

done_testing;