Skip to content

Commit

Permalink
Merge branch 'master' into refactor/global-variables
Browse files Browse the repository at this point in the history
  • Loading branch information
murtukov authored Jan 14, 2021
2 parents e4d5dd9 + f9ed247 commit 9a0877f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/DependencyInjection/OverblogGraphQLExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion src/Transformer/ArgumentsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
74 changes: 74 additions & 0 deletions tests/Transformer/ArgumentsTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
}
}

0 comments on commit 9a0877f

Please sign in to comment.