Skip to content

Commit

Permalink
fix(graphql): add filters from the nested resource metadata (#5171)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Nov 8, 2022
1 parent 6771f53 commit 2a58276
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 12 deletions.
Expand Up @@ -51,15 +51,6 @@ class: $resourceClass,
shortName: $shortName
);

if (class_exists($resourceClass)) {
$refl = new \ReflectionClass($resourceClass);
$attribute = $refl->getAttributes(ApiResource::class)[0] ?? null;
$attributeInstance = $attribute?->newInstance();
if ($filters = $attributeInstance?->getFilters()) {
$apiResource = $apiResource->withFilters($filters);
}
}

$resourceMetadataCollection[0] = $this->addDefaultGraphQlOperations($apiResource);

return $resourceMetadataCollection;
Expand Down
4 changes: 4 additions & 0 deletions src/GraphQl/Type/FieldsBuilder.php
Expand Up @@ -263,6 +263,10 @@ private function getResourceFieldConfiguration(?string $property, ?string $field
// If there is no query operation for a nested resource we force one to exist
$nestedResourceMetadataCollection = $this->graphQlNestedOperationResourceMetadataFactory->create($resourceClass);
$resourceOperation = $nestedResourceMetadataCollection->getOperation($isCollectionType ? 'collection_query' : 'item_query');
// Add filters from the metadata defined on the resource itself.
if ($filters = $resourceMetadataCollection[0]?->getFilters()) {
$resourceOperation = $resourceOperation->withFilters($filters);
}
}
}

Expand Down
Expand Up @@ -38,7 +38,6 @@ public function testCreateWithResource(): void
{
$metadataFactory = new GraphQlNestedOperationResourceMetadataFactory(['status' => 500]);
$apiResource = $metadataFactory->create(RelatedDummy::class)[0];
$this->assertNotEmpty($apiResource->getFilters());
$this->assertEquals('RelatedDummy', $apiResource->getShortName());
}
}
4 changes: 2 additions & 2 deletions tests/GraphQl/Type/FieldsBuilderTest.php
Expand Up @@ -508,10 +508,10 @@ public function testGetResourceObjectTypeFields(string $resourceClass, Operation
}
if ('propertyNestedResourceNoQuery' === $propertyName) {
$nestedResourceQueryOperation = new Query();
$this->resourceMetadataCollectionFactoryProphecy->create('nestedResourceNoQueryClass')->willReturn(new ResourceMetadataCollection('nestedResourceNoQueryClass', [(new ApiResource())->withDescription('A description.')->withGraphQlOperations([])]));
$this->resourceMetadataCollectionFactoryProphecy->create('nestedResourceNoQueryClass')->willReturn(new ResourceMetadataCollection('nestedResourceNoQueryClass', [(new ApiResource())->withDescription('A description.')->withFilters(['search_filter'])->withGraphQlOperations([])]));
$this->graphQlNestedOperationResourceMetadataFactoryProphecy->create('nestedResourceNoQueryClass')->shouldBeCalled()->willReturn(new ResourceMetadataCollection('nestedResourceNoQueryClass', [(new ApiResource())->withGraphQlOperations(['item_query' => $nestedResourceQueryOperation])]));
$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']));
$this->itemResolverFactoryProphecy->__invoke('nestedResourceNoQueryClass', $resourceClass, $nestedResourceQueryOperation)->willReturn(static function (): void {
$this->itemResolverFactoryProphecy->__invoke('nestedResourceNoQueryClass', $resourceClass, $nestedResourceQueryOperation->withFilters(['search_filter']))->willReturn(static function (): void {
});
}
}
Expand Down

0 comments on commit 2a58276

Please sign in to comment.