Skip to content

Commit

Permalink
fix(metadata): graphql can be disabled but with an existing operation
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 4, 2024
1 parent df701da commit f9d96e5
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ public function register(): void
$config->get('api-platform.graphql.enabled'),
),
)
)
),
$config->get('api-platform.graphql.enabled')
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public function create(string $resourceClass): ResourceMetadataCollection
$resourceMetadataCollection[$i] = $resourceMetadata->withOperations($operations);

$graphQlOperations = $resourceMetadata->getGraphQlOperations();

foreach ($graphQlOperations ?? [] as $operationName => $graphQlOperation) {
if (!$graphQlOperation->getPolicy() && ($policy = Gate::getPolicyFor($model))) {
if (($policyMethod = self::POLICY_METHODS[$graphQlOperation::class] ?? null) && method_exists($policy, $policyMethod)) {
Expand All @@ -122,7 +121,9 @@ public function create(string $resourceClass): ResourceMetadataCollection
$graphQlOperations[$operationName] = $graphQlOperation;
}

$resourceMetadata = $resourceMetadata->withGraphQlOperations($graphQlOperations);
if ($graphQlOperations) {
$resourceMetadata = $resourceMetadata->withGraphQlOperations($graphQlOperations);
}

$resourceMetadataCollection[$i] = $resourceMetadata;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
final class LinkResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
public function __construct(private readonly LinkFactoryInterface $linkFactory, private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null)
public function __construct(private readonly LinkFactoryInterface $linkFactory, private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, private bool $graphQlEnabled = false)
{
}

Expand All @@ -37,6 +37,10 @@ public function create(string $resourceClass): ResourceMetadataCollection
$resourceMetadataCollection = $this->decorated->create($resourceClass);
}

if (!$this->graphQlEnabled) {
return $resourceMetadataCollection;
}

foreach ($resourceMetadataCollection as $i => $resource) {
$graphQlOperations = [];
foreach ($resource->getGraphQlOperations() ?? [] as $graphQlOperation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ private function buildResourceOperations(array $metadataCollection, string $reso
$resources[$index] = $resource = $resource->withOperations(new Operations($operations)); // @phpstan-ignore-line
}

$graphQlOperations = $resource->getGraphQlOperations();
if (!$this->graphQlEnabled) {
continue;
}

$graphQlOperations = $resource->getGraphQlOperations();
if (null === $graphQlOperations) {
if (!$hasApiResource) {
$resources[$index] = $resources[$index]->withGraphQlOperations([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class: AttributeResource::class,
]),
);

$linkResourceMetadataCollectionFactory = new LinkResourceMetadataCollectionFactory($linkFactory, $resourceCollectionMetadataFactoryProphecy->reveal());
$linkResourceMetadataCollectionFactory = new LinkResourceMetadataCollectionFactory($linkFactory, $resourceCollectionMetadataFactoryProphecy->reveal(), true);

$this->assertEquals(
new ResourceMetadataCollection(AttributeResource::class, [
Expand Down Expand Up @@ -123,7 +123,7 @@ class: AttributeResource::class,
]),
);

$linkResourceMetadataCollectionFactory = new LinkResourceMetadataCollectionFactory($linkFactory, $resourceCollectionMetadataFactoryProphecy->reveal());
$linkResourceMetadataCollectionFactory = new LinkResourceMetadataCollectionFactory($linkFactory, $resourceCollectionMetadataFactoryProphecy->reveal(), true);

$this->assertEquals(
new ResourceMetadataCollection(AttributeResource::class, [
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/Resources/config/metadata/resource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<service id="api_platform.metadata.resource.metadata_collection_factory.link" class="ApiPlatform\Metadata\Resource\Factory\LinkResourceMetadataCollectionFactory" decorates="api_platform.metadata.resource.metadata_collection_factory" decoration-priority="500" public="false">
<argument type="service" id="api_platform.metadata.resource.link_factory" />
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory.link.inner" />
<argument>%api_platform.graphql.enabled%</argument>
</service>

<!-- must run after uri template, do not change uriTemplate neither the operation names in < 500 priorities -->
Expand Down

0 comments on commit f9d96e5

Please sign in to comment.