diff --git a/CHANGELOG.md b/CHANGELOG.md index 4debc273dda..a27ccb87b35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ API Platform 5.1 contains every change shipped in the v5.0.x releases, plus the * JSON:API: a `PATCH` or `PUT` whose `data.id` does not match the endpoint now returns `409 Conflict`, as the specification requires; it used to be applied to the endpoint's resource ([#8752](https://github.com/api-platform/core/pull/8752)). With `jsonapi.strict_resource_type` enabled, a mismatched `data.type` also returns `409` on `PATCH` and `PUT`, not only on `POST`. * JSON:API: `jsonapi.strict_member_names` (off by default) exposes the `id` and `type` properties as `resourceId` and `resourceType` attributes instead of `_id` and `_type`, which are not valid member names, and stops renaming `links`, `relationships` and `included` ([#8752](https://github.com/api-platform/core/pull/8752)). +* GraphQL: properties typed as a list of `bool`, `int`, `float` or `string` are now exposed as `[Boolean]`, `[Int]`, `[Float]` and `[String]` instead of the `Iterable` scalar; dictionaries and lists of other types still use `Iterable`, so schemas and clients using these fields must be updated ([#8619](https://github.com/api-platform/core/pull/8619)). ### Breaking changes diff --git a/src/GraphQl/Tests/Type/TypeConverterTest.php b/src/GraphQl/Tests/Type/TypeConverterTest.php index 62f87bbdeec..fa4187d3dd4 100644 --- a/src/GraphQl/Tests/Type/TypeConverterTest.php +++ b/src/GraphQl/Tests/Type/TypeConverterTest.php @@ -91,6 +91,34 @@ public static function convertTypeProvider(): array ]; } + #[DataProvider('convertScalarCollectionTypeProvider')] + public function testConvertScalarCollectionType(Type $type, GraphQLType|string $expectedGraphqlType): void + { + $operation = (new Query())->withName('test'); + $graphqlType = $this->typeConverter->convertPhpType($type, false, $operation, 'resourceClass', 'rootClass', null, 0); + + $this->assertEquals($expectedGraphqlType, $graphqlType); + } + + public static function convertScalarCollectionTypeProvider(): array + { + return [ + 'list of string' => [Type::list(Type::string()), GraphQLType::listOf(GraphQLType::string())], + 'list of int' => [Type::list(Type::int()), GraphQLType::listOf(GraphQLType::int())], + 'list of float' => [Type::list(Type::float()), GraphQLType::listOf(GraphQLType::float())], + 'list of bool' => [Type::list(Type::bool()), GraphQLType::listOf(GraphQLType::boolean())], + 'array of string (string[])' => [Type::array(Type::string()), GraphQLType::listOf(GraphQLType::string())], + 'array' => [Type::array(Type::string(), Type::int()), GraphQLType::listOf(GraphQLType::string())], + 'iterable of int' => [Type::iterable(Type::int()), GraphQLType::listOf(GraphQLType::int())], + 'dictionary array' => [Type::array(Type::string(), Type::string()), 'Iterable'], + 'array of mixed' => [Type::array(Type::mixed()), 'Iterable'], + 'nullable list of string' => [Type::nullable(Type::list(Type::string())), GraphQLType::listOf(GraphQLType::string())], + 'list of nullable string' => [Type::list(Type::nullable(Type::string())), GraphQLType::listOf(GraphQLType::string())], + 'list of int|string' => [Type::list(Type::union(Type::int(), Type::string())), 'Iterable'], + 'nested list' => [Type::list(Type::list(Type::string())), 'Iterable'], + ]; + } + public function testConvertTypeNoGraphQlResourceMetadata(): void { $type = Type::object('dummy'); diff --git a/src/GraphQl/Type/TypeConverter.php b/src/GraphQl/Type/TypeConverter.php index e8273dd5009..37f861ab32a 100644 --- a/src/GraphQl/Type/TypeConverter.php +++ b/src/GraphQl/Type/TypeConverter.php @@ -30,7 +30,9 @@ use GraphQL\Type\Definition\NullableType; use GraphQL\Type\Definition\Type as GraphQLType; use Symfony\Component\TypeInfo\Type; +use Symfony\Component\TypeInfo\Type\BuiltinType; use Symfony\Component\TypeInfo\Type\CollectionType; +use Symfony\Component\TypeInfo\Type\NullableType as TypeInfoNullableType; use Symfony\Component\TypeInfo\Type\ObjectType; use Symfony\Component\TypeInfo\TypeIdentifier; @@ -71,7 +73,7 @@ public function convertPhpType(Type $type, bool $input, Operation $rootOperation return $resourceType; } - return 'Iterable'; + return $this->getScalarListType($type) ?? 'Iterable'; } if ($type->isIdentifiedBy(TypeIdentifier::OBJECT)) { @@ -99,6 +101,39 @@ public function resolveType(string $type): GraphQLType throw new InvalidArgumentException(\sprintf('The type "%s" was not resolved.', $type)); } + private function getScalarListType(Type $type): ?GraphQLType + { + if ($type instanceof TypeInfoNullableType) { + $type = $type->getWrappedType(); + } + + if (!$type instanceof CollectionType) { + return null; + } + + $keyType = $type->getCollectionKeyType(); + if ($keyType->isIdentifiedBy(TypeIdentifier::STRING) && !$keyType->isIdentifiedBy(TypeIdentifier::INT)) { + return null; + } + + $valueType = $type->getCollectionValueType(); + if ($valueType instanceof TypeInfoNullableType) { + $valueType = $valueType->getWrappedType(); + } + + if (!$valueType instanceof BuiltinType) { + return null; + } + + return match ($valueType->getTypeIdentifier()) { + TypeIdentifier::BOOL => GraphQLType::listOf(GraphQLType::boolean()), + TypeIdentifier::INT => GraphQLType::listOf(GraphQLType::int()), + TypeIdentifier::FLOAT => GraphQLType::listOf(GraphQLType::float()), + TypeIdentifier::STRING => GraphQLType::listOf(GraphQLType::string()), + default => null, + }; + } + private function getResourceType(Type $type, bool $input, Operation $rootOperation, string $rootResource, ?string $property, int $depth): ?GraphQLType { $isCollection = $type->isSatisfiedBy(static fn ($t) => $t instanceof CollectionType);