|
13 | 13 | use GraphQL\Type\Definition\ResolveInfo; |
14 | 14 | use GraphQL\Type\Definition\Type; |
15 | 15 | use GraphQL\Type\Schema; |
| 16 | +use LogicException; |
| 17 | +use Overblog\GraphQLBundle\Definition\Omittable; |
16 | 18 | use Overblog\GraphQLBundle\Definition\Type\PhpEnumType; |
17 | 19 | use Overblog\GraphQLBundle\Error\InvalidArgumentError; |
18 | 20 | use Overblog\GraphQLBundle\Error\InvalidArgumentsError; |
@@ -110,7 +112,17 @@ public static function getTypes(): array |
110 | 112 | ], |
111 | 113 | ]); |
112 | 114 |
|
113 | | - $types = [$t1, $t2, $t3, $t4, $t5, $t6]; |
| 115 | + $t7 = new InputObjectType([ |
| 116 | + 'name' => 'InputTypeWithOmittable', |
| 117 | + 'fields' => [ |
| 118 | + 'nullableString' => Type::string(), |
| 119 | + 'nestedInput' => $t1, |
| 120 | + 'stringList' => Type::listOf(Type::string()), |
| 121 | + 'regularNullable' => Type::string(), |
| 122 | + ], |
| 123 | + ]); |
| 124 | + |
| 125 | + $types = [$t1, $t2, $t3, $t4, $t5, $t6, $t7]; |
114 | 126 |
|
115 | 127 | if (PHP_VERSION_ID >= 80100) { |
116 | 128 | $types[] = new PhpEnumType([ |
@@ -253,6 +265,58 @@ public function testPopulating(): void |
253 | 265 | $this->assertEquals('enum1', $res->field3->value); |
254 | 266 | } |
255 | 267 |
|
| 268 | + public function testPopulatingOmittableInputFields(): void |
| 269 | + { |
| 270 | + $transformer = $this->getTransformer([ |
| 271 | + 'InputType1' => ['type' => 'input', 'class' => InputType1::class], |
| 272 | + 'InputTypeWithOmittable' => ['type' => 'input', 'class' => InputTypeWithOmittable::class], |
| 273 | + ]); |
| 274 | + |
| 275 | + $info = $this->getResolveInfo(self::getTypes()); |
| 276 | + |
| 277 | + /** @var InputTypeWithOmittable $res */ |
| 278 | + $res = $transformer->getInstanceAndValidate( |
| 279 | + 'InputTypeWithOmittable', |
| 280 | + [ |
| 281 | + 'nullableString' => null, |
| 282 | + 'nestedInput' => ['field1' => 'nested value'], |
| 283 | + 'stringList' => ['first', 'second'], |
| 284 | + ], |
| 285 | + $info, |
| 286 | + 'input' |
| 287 | + ); |
| 288 | + |
| 289 | + $this->assertInstanceOf(InputTypeWithOmittable::class, $res); |
| 290 | + $this->assertInstanceOf(Omittable::class, $res->nullableString); |
| 291 | + $this->assertTrue($res->nullableString->isSet()); |
| 292 | + $this->assertNull($res->nullableString->value()); |
| 293 | + |
| 294 | + $this->assertTrue($res->nestedInput->isSet()); |
| 295 | + $this->assertInstanceOf(InputType1::class, $res->nestedInput->value()); |
| 296 | + $this->assertSame('nested value', $res->nestedInput->value()->field1); |
| 297 | + |
| 298 | + $this->assertTrue($res->stringList->isSet()); |
| 299 | + $this->assertSame(['first', 'second'], $res->stringList->value()); |
| 300 | + |
| 301 | + $this->assertNull($res->regularNullable); |
| 302 | + |
| 303 | + /** @var InputTypeWithOmittable $res */ |
| 304 | + $res = $transformer->getInstanceAndValidate('InputTypeWithOmittable', [], $info, 'input'); |
| 305 | + |
| 306 | + $this->assertFalse($res->nullableString->isSet()); |
| 307 | + $this->assertFalse($res->nestedInput->isSet()); |
| 308 | + $this->assertFalse($res->stringList->isSet()); |
| 309 | + $this->assertNull($res->regularNullable); |
| 310 | + } |
| 311 | + |
| 312 | + public function testOmittableValueCannotBeReadWhenOmitted(): void |
| 313 | + { |
| 314 | + $this->expectException(LogicException::class); |
| 315 | + $this->expectExceptionMessage('Cannot read the value of an omitted input field.'); |
| 316 | + |
| 317 | + Omittable::omitted()->value(); |
| 318 | + } |
| 319 | + |
256 | 320 | public function testRaisedErrors(): void |
257 | 321 | { |
258 | 322 | $violation = new ConstraintViolation('validation_error', 'validation_error', [], 'invalid', 'field2', 'invalid'); |
|
0 commit comments