Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate only InputObject types fix #1170 #1175

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading