Skip to content

Commit 92a81f0

Browse files
authored
feat(graphql): allow to disable the introspection query (api-platform#5711)
1 parent 5bc422c commit 92a81f0

File tree

6 files changed

+64
-1
lines changed

6 files changed

+64
-1
lines changed

src/GraphQl/Executor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use GraphQL\Executor\ExecutionResult;
1717
use GraphQL\GraphQL;
1818
use GraphQL\Type\Schema;
19+
use GraphQL\Validator\DocumentValidator;
20+
use GraphQL\Validator\Rules\DisableIntrospection;
1921

2022
/**
2123
* Wrapper for the GraphQL facade.
@@ -24,6 +26,15 @@
2426
*/
2527
final class Executor implements ExecutorInterface
2628
{
29+
public function __construct(private readonly bool $graphQlIntrospectionEnabled = true)
30+
{
31+
DocumentValidator::addRule(
32+
new DisableIntrospection(
33+
$this->graphQlIntrospectionEnabled ? DisableIntrospection::DISABLED : DisableIntrospection::ENABLED
34+
)
35+
);
36+
}
37+
2738
/**
2839
* {@inheritdoc}
2940
*/

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,16 @@ private function registerGraphQlConfiguration(ContainerBuilder $container, array
499499
{
500500
$enabled = $this->isConfigEnabled($container, $config['graphql']);
501501

502+
$graphqlIntrospectionEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['introspection']);
503+
502504
$graphiqlEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['graphiql']);
503505
$graphqlPlayGroundEnabled = $enabled && $this->isConfigEnabled($container, $config['graphql']['graphql_playground']);
504506
if ($graphqlPlayGroundEnabled) {
505507
trigger_deprecation('api-platform/core', '3.1', 'GraphQL Playground is deprecated and will be removed in API Platform 4.0. Only GraphiQL will be available in the future. Set api_platform.graphql.graphql_playground to false in the configuration to remove this deprecation.');
506508
}
507509

508510
$container->setParameter('api_platform.graphql.enabled', $enabled);
511+
$container->setParameter('api_platform.graphql.introspection.enabled', $graphqlIntrospectionEnabled);
509512
$container->setParameter('api_platform.graphql.graphiql.enabled', $graphiqlEnabled);
510513
$container->setParameter('api_platform.graphql.graphql_playground.enabled', $graphqlPlayGroundEnabled);
511514
$container->setParameter('api_platform.graphql.collection.pagination', $config['graphql']['collection']['pagination']);

src/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ private function addGraphQlSection(ArrayNodeDefinition $rootNode): void
236236
->arrayNode('graphql_playground')
237237
->{class_exists(GraphQL::class) && class_exists(TwigBundle::class) ? 'canBeDisabled' : 'canBeEnabled'}()
238238
->end()
239+
->arrayNode('introspection')
240+
->canBeDisabled()
241+
->end()
239242
->scalarNode('nesting_separator')->defaultValue('_')->info('The separator to use to filter nested fields.')->end()
240243
->arrayNode('collection')
241244
->addDefaultsIfNotSet()

src/Symfony/Bundle/Resources/config/graphql.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8-
<service id="api_platform.graphql.executor" class="ApiPlatform\GraphQl\Executor" public="false" />
8+
<service id="api_platform.graphql.executor" class="ApiPlatform\GraphQl\Executor" public="false">
9+
<argument>%api_platform.graphql.introspection.enabled%</argument>
10+
</service>
911

1012
<!-- Resolvers -->
1113

tests/GraphQl/ExecutorTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\GraphQl;
15+
16+
use ApiPlatform\GraphQl\Executor;
17+
use GraphQL\Validator\DocumentValidator;
18+
use GraphQL\Validator\Rules\DisableIntrospection;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @author Julien Verger <julien.verger@gmail.com>
23+
*/
24+
class ExecutorTest extends TestCase
25+
{
26+
public function testEnableIntrospectionQuery(): void
27+
{
28+
$executor = new Executor(true);
29+
30+
$expected = new DisableIntrospection(DisableIntrospection::DISABLED);
31+
$this->assertEquals($expected, DocumentValidator::getRule(DisableIntrospection::class));
32+
}
33+
34+
public function testDisableIntrospectionQuery(): void
35+
{
36+
$executor = new Executor(false);
37+
38+
$expected = new DisableIntrospection(DisableIntrospection::ENABLED);
39+
$this->assertEquals($expected, DocumentValidator::getRule(DisableIntrospection::class));
40+
}
41+
}

tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
121121
'graphiql' => [
122122
'enabled' => true,
123123
],
124+
'introspection' => [
125+
'enabled' => true,
126+
],
124127
'nesting_separator' => '_',
125128
'collection' => [
126129
'pagination' => [

0 commit comments

Comments
 (0)