Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions src/GraphQl/Tests/Type/TypeConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string>' => [Type::array(Type::string(), Type::int()), GraphQLType::listOf(GraphQLType::string())],
'iterable of int' => [Type::iterable(Type::int()), GraphQLType::listOf(GraphQLType::int())],
'dictionary array<string, string>' => [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');
Expand Down
37 changes: 36 additions & 1 deletion src/GraphQl/Type/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
Loading