Skip to content

Commit 2a58276

Browse files
authored
fix(graphql): add filters from the nested resource metadata (#5171)
1 parent 6771f53 commit 2a58276

File tree

4 files changed

+6
-12
lines changed

4 files changed

+6
-12
lines changed

src/GraphQl/Metadata/Factory/GraphQlNestedOperationResourceMetadataFactory.php

-9
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,6 @@ class: $resourceClass,
5151
shortName: $shortName
5252
);
5353

54-
if (class_exists($resourceClass)) {
55-
$refl = new \ReflectionClass($resourceClass);
56-
$attribute = $refl->getAttributes(ApiResource::class)[0] ?? null;
57-
$attributeInstance = $attribute?->newInstance();
58-
if ($filters = $attributeInstance?->getFilters()) {
59-
$apiResource = $apiResource->withFilters($filters);
60-
}
61-
}
62-
6354
$resourceMetadataCollection[0] = $this->addDefaultGraphQlOperations($apiResource);
6455

6556
return $resourceMetadataCollection;

src/GraphQl/Type/FieldsBuilder.php

+4
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ private function getResourceFieldConfiguration(?string $property, ?string $field
263263
// If there is no query operation for a nested resource we force one to exist
264264
$nestedResourceMetadataCollection = $this->graphQlNestedOperationResourceMetadataFactory->create($resourceClass);
265265
$resourceOperation = $nestedResourceMetadataCollection->getOperation($isCollectionType ? 'collection_query' : 'item_query');
266+
// Add filters from the metadata defined on the resource itself.
267+
if ($filters = $resourceMetadataCollection[0]?->getFilters()) {
268+
$resourceOperation = $resourceOperation->withFilters($filters);
269+
}
266270
}
267271
}
268272

tests/GraphQl/Metadata/Factory/GraphQlNestedOperationResourceMetadataFactoryTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function testCreateWithResource(): void
3838
{
3939
$metadataFactory = new GraphQlNestedOperationResourceMetadataFactory(['status' => 500]);
4040
$apiResource = $metadataFactory->create(RelatedDummy::class)[0];
41-
$this->assertNotEmpty($apiResource->getFilters());
4241
$this->assertEquals('RelatedDummy', $apiResource->getShortName());
4342
}
4443
}

tests/GraphQl/Type/FieldsBuilderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,10 @@ public function testGetResourceObjectTypeFields(string $resourceClass, Operation
508508
}
509509
if ('propertyNestedResourceNoQuery' === $propertyName) {
510510
$nestedResourceQueryOperation = new Query();
511-
$this->resourceMetadataCollectionFactoryProphecy->create('nestedResourceNoQueryClass')->willReturn(new ResourceMetadataCollection('nestedResourceNoQueryClass', [(new ApiResource())->withDescription('A description.')->withGraphQlOperations([])]));
511+
$this->resourceMetadataCollectionFactoryProphecy->create('nestedResourceNoQueryClass')->willReturn(new ResourceMetadataCollection('nestedResourceNoQueryClass', [(new ApiResource())->withDescription('A description.')->withFilters(['search_filter'])->withGraphQlOperations([])]));
512512
$this->graphQlNestedOperationResourceMetadataFactoryProphecy->create('nestedResourceNoQueryClass')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('nestedResourceNoQueryClass', [(new ApiResource())->withGraphQlOperations(['item_query' => $nestedResourceQueryOperation])]));
513513
$this->typeConverterProphecy->convertType(Argument::type(Type::class), Argument::type('bool'), Argument::that(static fn (Operation $arg): bool => $arg->getName() === $operation->getName()), 'nestedResourceNoQueryClass', $resourceClass, $propertyName, $depth + 1)->willReturn(new ObjectType(['name' => 'objectType']));
514-
$this->itemResolverFactoryProphecy->__invoke('nestedResourceNoQueryClass', $resourceClass, $nestedResourceQueryOperation)->willReturn(static function (): void {
514+
$this->itemResolverFactoryProphecy->__invoke('nestedResourceNoQueryClass', $resourceClass, $nestedResourceQueryOperation->withFilters(['search_filter']))->willReturn(static function (): void {
515515
});
516516
}
517517
}

0 commit comments

Comments
 (0)