Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ private function getDeclaredOperationClasses(ResourceMetadataCollection $resourc
foreach ($resource->getOperations() ?? [] as $operation) {
$operationClasses[] = $operation::class;
}

foreach ($resource->getGraphQlOperations() ?? [] as $operation) {
$operationClasses[] = $operation::class;
}
}

return $operationClasses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use ApiPlatform\Metadata\FilterInterface;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\GraphQl\Mutation;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use ApiPlatform\Metadata\HeaderParameter;
use ApiPlatform\Metadata\ParameterProviderFilterInterface;
use ApiPlatform\Metadata\Parameters;
Expand Down Expand Up @@ -884,6 +886,67 @@ public function testHeaderParameterOnPropertiesWithOperations(): void
$this->assertTrue($getParameters->has('X-API-Key', HeaderParameter::class));
}

public function testQueryParameterOnPropertiesWithGraphQlOperations(): void
{
$nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
$nameCollection->method('create')->willReturn(new PropertyNameCollection(['name']));

$propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
$propertyMetadata->method('create')->willReturn(
new ApiProperty(readable: true),
);

$filterLocator = $this->createStub(ContainerInterface::class);
$filterLocator->method('has')->willReturn(false);

$parameterFactory = new ParameterResourceMetadataCollectionFactory(
$nameCollection,
$propertyMetadata,
new AttributesResourceMetadataCollectionFactory(graphQlEnabled: true),
$filterLocator
);

$resource = $parameterFactory->create(QueryParameterOnPropertiesWithGraphQlOperations::class)[0];

$operations = array_values(iterator_to_array($resource->getOperations()));
$this->assertCount(1, $operations);
$this->assertInstanceOf(GetCollection::class, $operations[0]);
$this->assertFalse($operations[0]->getParameters()?->has('search', QueryParameter::class) ?? false);

$graphQlCollectionOperations = array_values(array_filter(
$resource->getGraphQlOperations(),
static fn ($graphQlOperation): bool => $graphQlOperation instanceof QueryCollection,
));
$this->assertCount(1, $graphQlCollectionOperations);
$this->assertTrue($graphQlCollectionOperations[0]->getParameters()->has('search', QueryParameter::class));
}

public function testQueryParameterOnPropertiesThrowsExceptionWhenGraphQlOperationIsNotDeclared(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(\sprintf('Parameter attribute on property "name" is restricted to the operation "%s" which is not declared on the resource "%s".', Mutation::class, QueryParameterOnPropertiesWithUndeclaredGraphQlOperation::class));

$nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
$nameCollection->method('create')->willReturn(new PropertyNameCollection(['name']));

$propertyMetadata = $this->createStub(PropertyMetadataFactoryInterface::class);
$propertyMetadata->method('create')->willReturn(
new ApiProperty(readable: true),
);

$filterLocator = $this->createStub(ContainerInterface::class);
$filterLocator->method('has')->willReturn(false);

$parameterFactory = new ParameterResourceMetadataCollectionFactory(
$nameCollection,
$propertyMetadata,
new AttributesResourceMetadataCollectionFactory(graphQlEnabled: true),
$filterLocator
);

$parameterFactory->create(QueryParameterOnPropertiesWithUndeclaredGraphQlOperation::class);
}

public function testNestedPropertyWithNameConverter(): void
{
$nameCollection = $this->createStub(PropertyNameCollectionFactoryInterface::class);
Expand Down Expand Up @@ -1400,3 +1463,31 @@ class HeaderParameterOnPropertiesWithOperations
#[HeaderParameter(key: 'X-API-Key', description: 'API key header', operations: [new GetCollection(), new Get()])]
public string $apiKey = '';
}

#[ApiResource(
operations: [
new GetCollection(),
],
graphQlOperations: [
new QueryCollection(),
]
)]
class QueryParameterOnPropertiesWithGraphQlOperations
{
#[QueryParameter(key: 'search', description: 'Search by name', operations: [new QueryCollection()])]
public string $name = '';
}

#[ApiResource(
operations: [
new GetCollection(),
],
graphQlOperations: [
new QueryCollection(),
]
)]
class QueryParameterOnPropertiesWithUndeclaredGraphQlOperation
{
#[QueryParameter(key: 'search', description: 'Search by name', operations: [new Mutation(name: 'create')])]
public string $name = '';
}
Loading