Skip to content

Commit

Permalink
Merge pull request #1175 from sparklink-pro/master
Browse files Browse the repository at this point in the history
Validate only InputObject types fix #1170
  • Loading branch information
Vincz authored Apr 2, 2024
2 parents 0e5f6fa + 3cd1bf6 commit f990e0b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/Transformer/ArgumentsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,13 @@ public function getInstanceAndValidate(string $argType, $data, ResolveInfo $info
$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);
$gqlType = $this->getType($type, $info);
$result = $this->populateObject($gqlType, $data, $isMultiple, $info);

if (null !== $this->validator) {
// We only want to validate input object types and not the scalar or object types that are already validated by the GraphQL library.
$shouldValidate = $gqlType instanceof InputObjectType;

if (null !== $this->validator && $shouldValidate) {
$errors = new ConstraintViolationList();
if (is_object($result)) {
$errors = $this->validator->validate($result);
Expand Down
30 changes: 27 additions & 3 deletions tests/Transformer/ArgumentsTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
Expand Down Expand Up @@ -101,10 +102,17 @@ public static function getTypes(): array
],
]);

$types = [$t1, $t2, $t3, $t4, $t5];
$t6 = new ObjectType([
'name' => 'Type1',
'fields' => [
'field1' => Type::string(),
],
]);

$types = [$t1, $t2, $t3, $t4, $t5, $t6];

if (PHP_VERSION_ID >= 80100) {
$t5 = new PhpEnumType([
$types[] = new PhpEnumType([
'name' => 'EnumPhp',
'enumClass' => EnumPhp::class,
'values' => [
Expand All @@ -113,7 +121,6 @@ public static function getTypes(): array
'VALUE3' => 'VALUE3',
],
]);
$types[] = $t5;
}

return $types;
Expand Down Expand Up @@ -431,4 +438,21 @@ public function testInputObjectWithWrappingTypeList(Type $type): void
$this->assertEquals($inputValue->field2, $data['field2']);
$this->assertEquals($inputValue->field3, $data['field3']);
}

public function testIgnoreNonInputObjectValidation(): void
{
$validator = $this->createMock(RecursiveValidator::class);
$validator->expects($this->never())->method('validate');

$transformer = new ArgumentsTransformer($validator, [
'Type1' => ['type' => 'object', 'class' => Type1::class],
]);

$info = $this->getResolveInfo(self::getTypes());
$data = new Type1();

$typeValue = $transformer->getInstanceAndValidate('Type1', $data, $info, 'type1');

$this->assertEquals($data, $typeValue);
}
}
13 changes: 13 additions & 0 deletions tests/Transformer/Type1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Overblog\GraphQLBundle\Tests\Transformer;

final class Type1
{
/**
* @var mixed
*/
public $field1;
}

0 comments on commit f990e0b

Please sign in to comment.