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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
use lib 't/lib';
use GQLTest;
BEGIN {
use_ok( 'GraphQL::Schema' ) || print "Bail out!\n";
use_ok( 'GraphQL::Type::Enum' ) || print "Bail out!\n";
use_ok( 'GraphQL::Type::Object' ) || print "Bail out!\n";
use_ok( 'GraphQL::Type::Scalar', qw($String $Int $Boolean) ) || print "Bail out!\n";
use_ok( 'GraphQL::Execution', qw(execute) ) || print "Bail out!\n";
use_ok( 'GraphQL::Introspection' ) || print "Bail out!\n";
}
my $ColorType = GraphQL::Type::Enum->new(
name => 'Color',
values => {
RED => { value => 0 },
GREEN => { value => 1 },
BLUE => { value => 2 },
},
);
my $Complex1 = { someRandomFunction => sub { } };
my $Complex2 = { someRandomValue => 123 };
my $ComplexEnum = GraphQL::Type::Enum->new(
name => 'Complex',
values => {
ONE => { value => $Complex1 },
TWO => { value => $Complex2 },
},
);
my $QueryType = GraphQL::Type::Object->new(
name => 'Query',
fields => {
colorEnum => {
type => $ColorType,
args => {
fromEnum => { type => $ColorType },
fromInt => { type => $Int },
fromString => { type => $String },
},
resolve => sub {
$_[1]->{fromInt} // $_[1]->{fromString} // $_[1]->{fromEnum};
},
},
colorInt => {
type => $Int,
args => {
fromEnum => { type => $ColorType },
fromInt => { type => $Int },
},
resolve => sub {
$_[1]->{fromInt} // $_[1]->{fromEnum};
},
},
complexEnum => {
type => $ComplexEnum,
args => {
fromEnum => {
type => $ComplexEnum,
default_value => $Complex1, # internal not JSON
},
provideGoodValue => { type => $Boolean },
provideBadValue => { type => $Boolean },
},
resolve => sub {
return $Complex2 if $_[1]->{provideGoodValue};
return { %$Complex2 } if $_[1]->{provideBadValue}; # copy so not ==
$_[1]->{fromEnum};
},
},
}
);
my $MutationType = GraphQL::Type::Object->new(
name => 'Mutation',
fields => {
favoriteEnum => {
type => $ColorType,
args => { color => { type => $ColorType } },
resolve => sub { $_[1]->{color} },
},
},
);
my $SubscriptionType = GraphQL::Type::Object->new(
name => 'Subscription',
fields => {
subscribeToEnum => {
type => $ColorType,
args => { color => { type => $ColorType } },
resolve => sub { $_[1]->{color} },
},
},
);
my $schema = GraphQL::Schema->new(
query => $QueryType,
mutation => $MutationType,
subscription => $SubscriptionType,
);
subtest 'accepts enum literals as input', sub {
run_test(
[$schema, '{ colorInt(fromEnum: GREEN) }'],
{ data => { colorInt => 1 } },
);
done_testing;
};
subtest 'enum may be output type', sub {
run_test(
[$schema, '{ colorEnum(fromInt: 1) }'],
{ data => { colorEnum => 'GREEN' } },
);
done_testing;
};
subtest 'enum may be both input and output type', sub {
run_test(
[$schema, '{ colorEnum(fromEnum: GREEN) }'],
{ data => { colorEnum => 'GREEN' } },
);
done_testing;
};
subtest 'does not accept string literals', sub {
run_test(
[$schema, '{ colorEnum(fromEnum: "GREEN") }'],
{ data => { colorEnum => undef }, errors => [
{ message => "Argument 'fromEnum' of type 'Color' was given 'GREEN' which is not enum value.",
locations => [ { line => 1, column => 32 } ],
path => [ 'colorEnum' ],
} ] },
);
done_testing;
};
subtest 'does not accept incorrect internal value', sub {
run_test(
[$schema, '{ colorEnum(fromString: "GREEN") }'],
{ data => { colorEnum => undef }, errors => [
{ message => "Expected a value of type 'Color' but received: 'GREEN'.\n",
locations => [ { line => 1, column => 34 } ],
path => [ 'colorEnum' ],
} ] },
);
done_testing;
};
subtest 'does not accept internal value in place of enum literal', sub {
run_test(
[$schema, '{ colorEnum(fromEnum: 1) }'],
{ data => { colorEnum => undef }, errors => [
{ message => "Argument 'fromEnum' of type 'Color' was given '1' which is not enum value.",
locations => [ { line => 1, column => 26 } ],
path => [ 'colorEnum' ],
} ] },
);
done_testing;
};
subtest 'does not accept enum literal in place of int', sub {
run_test(
[$schema, '{ colorEnum(fromInt: GREEN) }'],
{ data => { colorEnum => undef }, errors => [
{ message => "Argument 'fromInt' of type 'Int' was given GREEN which is enum value.",
locations => [ { line => 1, column => 29 } ],
path => [ 'colorEnum' ],
} ] },
);
done_testing;
};
subtest 'accepts JSON string as enum variable', sub {
run_test(
[$schema, 'query test($color: Color!) { colorEnum(fromEnum: $color) }', undef, undef, { color => 'BLUE' }],
{ data => { colorEnum => 'BLUE' } },
);
done_testing;
};
subtest 'accepts enum literals as input arguments to mutations', sub {
run_test(
[$schema, 'mutation x($color: Color!) { favoriteEnum(color: $color) }', undef, undef, { color => 'GREEN' }],
{ data => { favoriteEnum => 'GREEN' } },
);
done_testing;
};
subtest 'accepts enum literals as input arguments to subscriptions', sub {
run_test(
[$schema, 'subscription x($color: Color!) { subscribeToEnum(color: $color) }', undef, undef, { color => 'GREEN' }],
{ data => { subscribeToEnum => 'GREEN' } },
);
done_testing;
};
subtest 'does not accept internal value as enum variable', sub {
run_test(
[$schema, 'query test($color: Color!) { colorEnum(fromEnum: $color) }', undef, undef, { color => 2 }],
{ errors => [
{ message => "Variable '\$color' got invalid value 2.\nExpected type 'Color', found 2.\n" }
] },
);
done_testing;
};
subtest 'does not accept string variables as enum input', sub {
run_test(
[$schema, 'query test($color: String!) { colorEnum(fromEnum: $color) }', undef, undef, { color => 'BLUE' }],
{ data => { colorEnum => undef }, errors => [
{ message => "Variable '\$color' of type 'String!' where expected 'Color'.",
locations => [ { line => 1, column => 59 } ],
path => [ 'colorEnum' ],
} ] },
);
done_testing;
};
subtest 'does not accept internal value variable as enum input', sub {
run_test(
[$schema, 'query test($color: Int!) { colorEnum(fromEnum: $color) }', undef, undef, { color => 2 }],
{ data => { colorEnum => undef }, errors => [
{ message => "Variable '\$color' of type 'Int!' where expected 'Color'.",
locations => [ { line => 1, column => 56 } ],
path => [ 'colorEnum' ],
} ] },
);
done_testing;
};
subtest 'enum value may have an internal value of 0', sub {
run_test(
[$schema, '{ colorEnum(fromEnum: RED) colorInt(fromEnum: RED) }'],
{ data => { colorEnum => 'RED', colorInt => 0 } },
);
done_testing;
};
subtest 'enum inputs may be nullable', sub {
run_test(
[$schema, '{ colorEnum colorInt }'],
{ data => { colorEnum => undef, colorInt => undef } },
);
done_testing;
};
subtest 'presents a getValues() API for complex enums', sub {
is_deeply $ComplexEnum->_name2value, {
ONE => $Complex1,
TWO => $Complex2,
};
done_testing;
};
subtest 'may be internally represented with complex values', sub {
run_test(
[$schema, '{
first: complexEnum
second: complexEnum(fromEnum: TWO)
good: complexEnum(provideGoodValue: true)
bad: complexEnum(provideBadValue: true)
}'],
{ data => {
first => 'ONE',
second => 'TWO',
good => 'TWO',
bad => undef,
}, errors => [ {
message => "Expected a value of type 'Complex' but received: HASH.\n",
locations => [ { line => 6, column => 5 } ],
path => [ 'bad' ],
} ] },
);
done_testing;
};
subtest 'can be introspected without error', sub {
my $got = execute($schema, $GraphQL::Introspection::QUERY);
ok !$got->{errors}, 'no query errors' or diag explain $got;
done_testing;
};
done_testing;
|