Skip to content

Commit c339d55

Browse files
authored
Merge pull request #7117 from soyuka/type-info/graphql
1 parent 9b0ea61 commit c339d55

11 files changed

+476
-175
lines changed

Resolver/Factory/ResolverFactory.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use ApiPlatform\State\ProviderInterface;
2727
use GraphQL\Type\Definition\ResolveInfo;
2828
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
30+
use Symfony\Component\TypeInfo\Type\CollectionType;
2931

3032
class ResolverFactory implements ResolverFactoryInterface
3133
{
@@ -58,10 +60,21 @@ public function __invoke(?string $resourceClass = null, ?string $rootClass = nul
5860
}
5961

6062
$propertyMetadata = $rootClass ? $propertyMetadataFactory?->create($rootClass, $info->fieldName) : null;
61-
$type = $propertyMetadata?->getBuiltinTypes()[0] ?? null;
62-
// Data already fetched and normalized (field or nested resource)
63-
if ($body || null === $resourceClass || ($type && !$type->isCollection())) {
64-
return $body;
63+
64+
if (method_exists(PropertyInfoExtractor::class, 'getType')) {
65+
$type = $propertyMetadata?->getNativeType();
66+
67+
// Data already fetched and normalized (field or nested resource)
68+
if ($body || null === $resourceClass || ($type && !$type->isSatisfiedBy(fn ($t) => $t instanceof CollectionType))) {
69+
return $body;
70+
}
71+
} else {
72+
$type = $propertyMetadata?->getBuiltinTypes()[0] ?? null;
73+
74+
// Data already fetched and normalized (field or nested resource)
75+
if ($body || null === $resourceClass || ($type && !$type->isCollection())) {
76+
return $body;
77+
}
6578
}
6679
}
6780

Tests/Type/FieldsBuilderTest.php

+46-53
Large diffs are not rendered by default.

Tests/Type/TypeBuilderTest.php

+32-13
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@
3636
use GraphQL\Type\Definition\ObjectType;
3737
use GraphQL\Type\Definition\ResolveInfo;
3838
use GraphQL\Type\Definition\Type as GraphQLType;
39+
use PHPUnit\Framework\Attributes\DataProvider;
40+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
3941
use PHPUnit\Framework\TestCase;
4042
use Prophecy\Argument;
4143
use Prophecy\PhpUnit\ProphecyTrait;
4244
use Prophecy\Prophecy\ObjectProphecy;
4345
use Psr\Container\ContainerInterface;
44-
use Symfony\Component\PropertyInfo\Type;
46+
use Symfony\Component\PropertyInfo\Type as LegacyType;
47+
use Symfony\Component\TypeInfo\Type;
4548

4649
/**
4750
* @author Alan Poulain <contact@alanpoulain.eu>
@@ -125,7 +128,7 @@ public function testGetResourceObjectTypeOutputClass(): void
125128
$resourceObjectType->config['fields']();
126129
}
127130

128-
#[\PHPUnit\Framework\Attributes\DataProvider('resourceObjectTypeQuerySerializationGroupsProvider')]
131+
#[DataProvider('resourceObjectTypeQuerySerializationGroupsProvider')]
129132
public function testGetResourceObjectTypeQuerySerializationGroups(string $itemSerializationGroup, string $collectionSerializationGroup, Operation $operation, string $shortName): void
130133
{
131134
$resourceMetadata = new ResourceMetadataCollection('resourceClass', [(new ApiResource())->withGraphQlOperations([
@@ -622,24 +625,40 @@ public function testGetEnumType(): void
622625
]), $this->typeBuilder->getEnumType($operation));
623626
}
624627

625-
#[\PHPUnit\Framework\Attributes\DataProvider('typesProvider')]
626-
public function testIsCollection(Type $type, bool $expectedIsCollection): void
628+
#[DataProvider('legacyTypesProvider')]
629+
#[IgnoreDeprecations]
630+
public function testIsCollectionLegacy(LegacyType $type, bool $expectedIsCollection): void
627631
{
632+
$this->expectUserDeprecationMessage('Since api-platform/graphql 4.2: The "ApiPlatform\GraphQl\Type\TypeBuilder::isCollection()" method is deprecated and will be removed.');
628633
$this->assertSame($expectedIsCollection, $this->typeBuilder->isCollection($type));
629634
}
630635

636+
public static function legacyTypesProvider(): array
637+
{
638+
return [
639+
[new LegacyType(LegacyType::BUILTIN_TYPE_BOOL), false],
640+
[new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT), false],
641+
[new LegacyType(LegacyType::BUILTIN_TYPE_RESOURCE, false, null, false), false],
642+
[new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, null, true), false],
643+
[new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, false, null, true), false],
644+
[new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, false, null, true, null, new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT)), false],
645+
[new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'className', true), false],
646+
[new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, null, true, null, new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'className')), true],
647+
[new LegacyType(LegacyType::BUILTIN_TYPE_ARRAY, false, null, true, null, new LegacyType(LegacyType::BUILTIN_TYPE_OBJECT, false, 'className')), true],
648+
];
649+
}
650+
631651
public static function typesProvider(): array
632652
{
633653
return [
634-
[new Type(Type::BUILTIN_TYPE_BOOL), false],
635-
[new Type(Type::BUILTIN_TYPE_OBJECT), false],
636-
[new Type(Type::BUILTIN_TYPE_RESOURCE, false, null, false), false],
637-
[new Type(Type::BUILTIN_TYPE_OBJECT, false, null, true), false],
638-
[new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true), false],
639-
[new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT)), false],
640-
[new Type(Type::BUILTIN_TYPE_OBJECT, false, 'className', true), false],
641-
[new Type(Type::BUILTIN_TYPE_OBJECT, false, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, 'className')), true],
642-
[new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, 'className')), true],
654+
[Type::bool(), false],
655+
[Type::object(), false],
656+
[Type::resource(), false],
657+
[Type::collection(Type::object(\Stringable::class)), false],
658+
[Type::array(), false],
659+
[Type::array(Type::object()), false],
660+
[Type::collection(Type::object(\Traversable::class), Type::object(\Stringable::class)), true],
661+
[Type::array(Type::object(\Stringable::class)), true],
643662
];
644663
}
645664
}

0 commit comments

Comments
 (0)