File: execution-sync.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 (56 lines) | stat: -rw-r--r-- 1,458 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
use lib 't/lib';
use strict;
use warnings;
use GQLTest;

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

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

my $schema = GraphQL::Schema->new(
  query => GraphQL::Type::Object->new(
    name => 'Query',
    fields => {
      syncField => {
        type => $String,
        resolve => sub {
          $_[0];
        }
      },
      asyncField => {
        type => $String,
        resolve => sub {
          my ($root_value, $args, $context, $info) = @_;
          $info->{promise_code}{resolve}->($root_value);
        }
      },
    }
  ),
);

subtest 'does not return a Promise for initial errors' => sub {
  run_test([
    $schema, "fragment Example on Query { syncField }", 'rootValue',
  ], +{ errors => [ {
    message => "No operations supplied.\n",
  } ] }, 0);
};

subtest 'does not return a Promise if fields are all synchronous' => sub {
  run_test([
    $schema, "query Example { syncField }", 'rootValue',
  ], +{ data => { syncField => 'rootValue' } }, 0);
};

subtest 'returns a Promise if any field is asynchronous' => sub {
  run_test([
    $schema, "query Example { asyncField }", 'rootValue',
  ], +{ data => { asyncField => 'rootValue' } }, 1);
};

done_testing;