Skip to content

Commit ca90556

Browse files
committed
Controller fixes for symfony 5
1 parent 9fc3eb3 commit ca90556

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

Command/GraphQLConfigureCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Output\OutputInterface;
1010
use Symfony\Component\Console\Question\ConfirmationQuestion;
11+
use Symfony\Component\DependencyInjection\Container;
1112
use Symfony\Component\DependencyInjection\ContainerInterface;
1213

1314
class GraphQLConfigureCommand extends Command
1415
{
1516
const PROJECT_NAMESPACE = 'App';
1617

17-
public function __construct(private ContainerInterface $container)
18+
/** @var Container */
19+
protected $container;
20+
21+
public function __construct(ContainerInterface $container)
1822
{
23+
$this->container = $container;
24+
1925
parent::__construct();
2026
}
2127

@@ -140,7 +146,6 @@ protected function graphQLRouteExists()
140146

141147
protected function generateRoutes()
142148
{
143-
144149
}
145150

146151
protected function getSchemaClassTemplate($nameSpace, $className = 'Schema')

Controller/GraphQLController.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Date: 25.11.15
45
*
@@ -7,16 +8,26 @@
78

89
namespace Youshido\GraphQLBundle\Controller;
910

10-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11+
use Psr\Container\ContainerInterface;
12+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1113
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
14+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
1215
use Symfony\Component\HttpFoundation\JsonResponse;
1316
use Symfony\Component\Routing\Annotation\Route;
14-
use Youshido\GraphQL\Exception\ConfigurationException;
1517
use Youshido\GraphQLBundle\Exception\UnableToInitializeSchemaServiceException;
1618
use Youshido\GraphQLBundle\Execution\Processor;
1719

18-
class GraphQLController extends Controller
20+
class GraphQLController extends AbstractController
1921
{
22+
protected $container;
23+
protected $params;
24+
25+
public function __construct(ContainerInterface $container, ParameterBagInterface $params)
26+
{
27+
$this->container = $container;
28+
$this->params = $params;
29+
}
30+
2031
/**
2132
* @Route("/graphql")
2233
*
@@ -36,13 +47,13 @@ public function defaultAction()
3647
);
3748
}
3849

39-
if ($this->get('request_stack')->getCurrentRequest()->getMethod() == 'OPTIONS') {
50+
if ($this->container->get('request_stack')->getCurrentRequest()->getMethod() == 'OPTIONS') {
4051
return $this->createEmptyResponse();
4152
}
4253

4354
list($queries, $isMultiQueryRequest) = $this->getPayload();
4455

45-
$queryResponses = array_map(function($queryData) {
56+
$queryResponses = array_map(function ($queryData) {
4657
return $this->executeQuery($queryData['query'], $queryData['variables']);
4758
}, $queries);
4859

@@ -55,15 +66,15 @@ public function defaultAction()
5566
return $response;
5667
}
5768

58-
private function createEmptyResponse()
69+
protected function createEmptyResponse()
5970
{
6071
return new JsonResponse([], 200, $this->getResponseHeaders());
6172
}
6273

63-
private function executeQuery($query, $variables)
74+
protected function executeQuery($query, $variables)
6475
{
6576
/** @var Processor $processor */
66-
$processor = $this->get('graphql.processor');
77+
$processor = $this->container->get('graphql.processor');
6778
$processor->processPayload($query, $variables);
6879

6980
return $processor->getResponseData();
@@ -74,9 +85,9 @@ private function executeQuery($query, $variables)
7485
*
7586
* @throws \Exception
7687
*/
77-
private function getPayload()
88+
protected function getPayload()
7889
{
79-
$request = $this->get('request_stack')->getCurrentRequest();
90+
$request = $this->container->get('request_stack')->getCurrentRequest();
8091
$query = $request->get('query', null);
8192
$variables = $request->get('variables', []);
8293
$isMultiQueryRequest = false;
@@ -135,7 +146,7 @@ private function getPayload()
135146
/**
136147
* @throws \Exception
137148
*/
138-
private function initializeSchemaService()
149+
protected function initializeSchemaService()
139150
{
140151
if ($this->container->initialized('graphql.schema')) {
141152
return;
@@ -149,9 +160,9 @@ private function initializeSchemaService()
149160
*
150161
* @throws \Exception
151162
*/
152-
private function makeSchemaService()
163+
protected function makeSchemaService()
153164
{
154-
if ($this->container->has($this->getSchemaService())) {
165+
if ($this->getSchemaService() && $this->container->has($this->getSchemaService())) {
155166
return $this->container->get($this->getSchemaService());
156167
}
157168

@@ -175,27 +186,32 @@ private function makeSchemaService()
175186
/**
176187
* @return string
177188
*/
178-
private function getSchemaClass()
189+
protected function getSchemaClass()
179190
{
180191
return $this->getParameter('graphql.schema_class');
181192
}
182193

183194
/**
184195
* @return string
185196
*/
186-
private function getSchemaService()
197+
protected function getSchemaService()
187198
{
188199
$serviceName = $this->getParameter('graphql.schema_service');
189200

190-
if (substr($serviceName, 0, 1) === '@') {
201+
if (substr($serviceName ?: '', 0, 1) === '@') {
191202
return substr($serviceName, 1, strlen($serviceName) - 1);
192203
}
193204

194205
return $serviceName;
195206
}
196207

197-
private function getResponseHeaders()
208+
protected function getResponseHeaders()
198209
{
199210
return $this->getParameter('graphql.response.headers');
200211
}
212+
213+
protected function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
214+
{
215+
return $this->params->get($name);
216+
}
201217
}

GraphQLBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function build(ContainerBuilder $container)
2727
}
2828

2929

30-
public function getContainerExtension()
30+
public function getContainerExtension(): GraphQLExtension
3131
{
3232
if (null === $this->extension) {
3333
$this->extension = new GraphQLExtension();

Resources/config/services.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@
5353
<tag name="console.command" command="graphql:configure"/>
5454
<argument type="service" id="service_container"/>
5555
</service>
56+
57+
<service id="Youshido\GraphQLBundle\Controller\GraphQLController" public="true" >
58+
<argument type="service" id="service_container" />
59+
<argument type="service" id="parameter_bag" />
60+
</service>
5661
</services>
5762
</container>

0 commit comments

Comments
 (0)