diff --git a/src/DependencyInjection/OverblogGraphQLExtension.php b/src/DependencyInjection/OverblogGraphQLExtension.php index ae12e20e4..1d391e814 100644 --- a/src/DependencyInjection/OverblogGraphQLExtension.php +++ b/src/DependencyInjection/OverblogGraphQLExtension.php @@ -220,7 +220,8 @@ private function setErrorHandler(array $config, ContainerBuilder $container): vo $container->register(ErrorHandler::class) ->setArgument(0, new Reference(EventDispatcherInterface::class)) - ->setArgument(1, new Reference(ExceptionConverterInterface::class)); + ->setArgument(1, new Reference(ExceptionConverterInterface::class)) + ->setArgument(2, $config['errors_handler']['internal_error_message']); $container->register(ErrorHandlerListener::class) ->setArgument(0, new Reference(ErrorHandler::class)) diff --git a/src/Transformer/ArgumentsTransformer.php b/src/Transformer/ArgumentsTransformer.php index 9c310a0db..148e2681b 100644 --- a/src/Transformer/ArgumentsTransformer.php +++ b/src/Transformer/ArgumentsTransformer.php @@ -132,7 +132,12 @@ public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info { $isRequired = '!' === $argType[strlen($argType) - 1]; $isMultiple = '[' === $argType[0]; - $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0); + $isStrictMultiple = false; + if ($isMultiple) { + $isStrictMultiple = '!' === $argType[strpos($argType, ']') - 1]; + } + + $endIndex = ($isRequired ? 1 : 0) + ($isMultiple ? 1 : 0) + ($isStrictMultiple ? 1 : 0); $type = substr($argType, $isMultiple ? 1 : 0, $endIndex > 0 ? -$endIndex : strlen($argType)); $result = $this->populateObject($this->getType($type, $info), $data, $isMultiple, $info); diff --git a/tests/Transformer/ArgumentsTransformerTest.php b/tests/Transformer/ArgumentsTransformerTest.php index 71ef0b398..0071a8bc6 100644 --- a/tests/Transformer/ArgumentsTransformerTest.php +++ b/tests/Transformer/ArgumentsTransformerTest.php @@ -5,8 +5,11 @@ namespace Overblog\GraphQLBundle\Tests\Transformer; use Exception; +use Generator; use GraphQL\Type\Definition\EnumType; use GraphQL\Type\Definition\InputObjectType; +use GraphQL\Type\Definition\ListOfType; +use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; @@ -300,4 +303,75 @@ public function testRaisedErrorsForMultipleInputs(): void $this->assertEquals($e->toState(), $expected); } } + + public function getWrappedInputObject(): Generator + { + $inputObject = new InputObjectType([ + 'name' => 'InputType1', + 'fields' => [ + 'field1' => Type::string(), + 'field2' => Type::int(), + 'field3' => Type::boolean(), + ], + ]); + yield [$inputObject, false]; + yield [new NonNull($inputObject), false]; + } + + /** @dataProvider getWrappedInputObject */ + public function testInputObjectWithWrappingType(Type $type): void + { + $transformer = $this->getTransformer([ + 'InputType1' => ['type' => 'input', 'class' => InputType1::class], + ], new ConstraintViolationList() + ); + $info = $this->getResolveInfo(self::getTypes()); + + $data = ['field1' => 'hello', 'field2' => 12, 'field3' => true]; + + $inputValue = $transformer->getInstanceAndValidate($type->toString(), $data, $info, 'input1'); + + /** @var InputType1 $inputValue */ + $this->assertInstanceOf(InputType1::class, $inputValue); + $this->assertEquals($inputValue->field1, $data['field1']); + $this->assertEquals($inputValue->field2, $data['field2']); + $this->assertEquals($inputValue->field3, $data['field3']); + } + + public function getWrappedInputObjectList(): Generator + { + $inputObject = new InputObjectType([ + 'name' => 'InputType1', + 'fields' => [ + 'field1' => Type::string(), + 'field2' => Type::int(), + 'field3' => Type::boolean(), + ], + ]); + yield [new ListOfType($inputObject)]; + yield [new ListOfType(new NonNull($inputObject))]; + yield [new NonNull(new ListOfType($inputObject))]; + yield [new NonNull(new ListOfType(new NonNull($inputObject)))]; + } + + /** @dataProvider getWrappedInputObjectList */ + public function testInputObjectWithWrappingTypeList(Type $type): void + { + $transformer = $this->getTransformer( + ['InputType1' => ['type' => 'input', 'class' => InputType1::class]], + new ConstraintViolationList() + ); + $info = $this->getResolveInfo(self::getTypes()); + + $data = ['field1' => 'hello', 'field2' => 12, 'field3' => true]; + + $inputValue = $transformer->getInstanceAndValidate($type->toString(), [$data], $info, 'input1'); + $inputValue = reset($inputValue); + + /** @var InputType1 $inputValue */ + $this->assertInstanceOf(InputType1::class, $inputValue); + $this->assertEquals($inputValue->field1, $data['field1']); + $this->assertEquals($inputValue->field2, $data['field2']); + $this->assertEquals($inputValue->field3, $data['field3']); + } }