I try to implement a GraphQL read operation (api platform version : 3.3.2) with an output different from the resource and i have a problem with a subressource’s IRI id.
Doc : https://api-platform.com/docs/core/dto/
In the provider below i put the city with id 10 in the DTO, but in the query’s result the city have the id “/books/1” instead of “/cities/10”
How to reproduce :
DTO :
final class AnotherBookRepresentationDTO
{
public int $id;
public string $name;
public City $city;
}
Entity:
#[ApiResource(
graphQlOperations: [
new Query(
output: AnotherBookRepresentationDTO::class,
provider: AnotherBookRepresentationProvider::class,
),
]
)]
class Book
{
#[ORMColumn(type: 'integer')]
#[ORMId]
#[ORMGeneratedValue(strategy: 'IDENTITY')]
private int $id;
}
Provider :
final class AnotherBookRepresentationProvider implements ProviderInterface
{
public function __construct(private EntityManagerInterface $entityManager)
{
}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$dto = new AnotherBookRepresentationDTO();
$dto->id = 1;
$dto->name = 'The Book Name';
$dto->city = $this->entityManager->find(City::class, 10); //Find city with id = 10
return $dto;
}
}
Query :
book(id: "/books/1") {
id
name
city {
id
zipCode
}
}
Result :
{
"data": {
"book": {
"id": "/books/1",
"name": "The Book Name",
"city": {
"id": "/books/1", //Problem is here: id should be "/cities/10"
"zipCode": "40100"
}
}
}
}