diff --git a/src/Laravel/ApiPlatformProvider.php b/src/Laravel/ApiPlatformProvider.php index 0a888c00b1d..6100c806292 100644 --- a/src/Laravel/ApiPlatformProvider.php +++ b/src/Laravel/ApiPlatformProvider.php @@ -366,7 +366,8 @@ public function register(): void $config->get('api-platform.graphql.enabled'), ), ) - ) + ), + $config->get('api-platform.graphql.enabled') ) ) ) diff --git a/src/Laravel/Eloquent/Metadata/Factory/Resource/EloquentResourceCollectionMetadataFactory.php b/src/Laravel/Eloquent/Metadata/Factory/Resource/EloquentResourceCollectionMetadataFactory.php index 5babbfb8a1a..551a08576aa 100644 --- a/src/Laravel/Eloquent/Metadata/Factory/Resource/EloquentResourceCollectionMetadataFactory.php +++ b/src/Laravel/Eloquent/Metadata/Factory/Resource/EloquentResourceCollectionMetadataFactory.php @@ -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)) { @@ -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; } diff --git a/src/Metadata/Resource/Factory/LinkResourceMetadataCollectionFactory.php b/src/Metadata/Resource/Factory/LinkResourceMetadataCollectionFactory.php index d2f3afcae72..dd5107f08d8 100644 --- a/src/Metadata/Resource/Factory/LinkResourceMetadataCollectionFactory.php +++ b/src/Metadata/Resource/Factory/LinkResourceMetadataCollectionFactory.php @@ -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) { } @@ -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) { diff --git a/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php b/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php index 6a7212a69e6..7561858e799 100644 --- a/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php +++ b/src/Metadata/Resource/Factory/MetadataCollectionFactoryTrait.php @@ -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([]); diff --git a/src/Metadata/Tests/Resource/Factory/LinkResourceMetadataCollectionFactoryTest.php b/src/Metadata/Tests/Resource/Factory/LinkResourceMetadataCollectionFactoryTest.php index d2bc4a64450..9edf2b70006 100644 --- a/src/Metadata/Tests/Resource/Factory/LinkResourceMetadataCollectionFactoryTest.php +++ b/src/Metadata/Tests/Resource/Factory/LinkResourceMetadataCollectionFactoryTest.php @@ -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, [ @@ -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, [ diff --git a/src/Symfony/Bundle/Resources/config/metadata/resource.xml b/src/Symfony/Bundle/Resources/config/metadata/resource.xml index c21c5ca8328..59b9422a9df 100644 --- a/src/Symfony/Bundle/Resources/config/metadata/resource.xml +++ b/src/Symfony/Bundle/Resources/config/metadata/resource.xml @@ -52,6 +52,7 @@ + %api_platform.graphql.enabled%