Skip to content

Commit 731ebb0

Browse files
authored
Merge pull request #1 from 99designs/symfony5
Add Symfony 4 & 5 support
2 parents 5f192e7 + 79a7233 commit 731ebb0

File tree

10 files changed

+109
-77
lines changed

10 files changed

+109
-77
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22
/.idea/
3-
composer.lock
3+
composer.lock
4+
.phpunit.result.cache

Command/GraphQLConfigureCommand.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22

33
namespace Youshido\GraphQLBundle\Command;
44

5-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
65
use Symfony\Component\Config\Resource\DirectoryResource;
76
use Symfony\Component\Config\Resource\FileResource;
7+
use Symfony\Component\Console\Command\Command;
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;
12+
use Symfony\Component\DependencyInjection\ContainerInterface;
1113

12-
class GraphQLConfigureCommand extends ContainerAwareCommand
14+
class GraphQLConfigureCommand extends Command
1315
{
1416
const PROJECT_NAMESPACE = 'App';
1517

18+
/** @var Container */
19+
protected $container;
20+
21+
public function __construct(ContainerInterface $container)
22+
{
23+
$this->container = $container;
24+
25+
parent::__construct();
26+
}
27+
1628
/**
1729
* {@inheritdoc}
1830
*/
@@ -31,8 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
3143
{
3244
$isComposerCall = $input->getOption('composer');
3345

34-
$container = $this->getContainer();
35-
$rootDir = $container->getParameter('kernel.root_dir');
46+
$rootDir = $this->container->getParameter('kernel.root_dir');
3647
$configFile = $rootDir . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config/packages/graphql.yml';
3748

3849
$className = 'Schema';
@@ -105,7 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
105116
*/
106117
protected function getMainRouteConfig()
107118
{
108-
$routerResources = $this->getContainer()->get('router')->getRouteCollection()->getResources();
119+
$routerResources = $this->container->get('router')->getRouteCollection()->getResources();
109120
foreach ($routerResources as $resource) {
110121
/** @var FileResource|DirectoryResource $resource */
111122
if (method_exists($resource, 'getResource') && substr($resource->getResource(), -11) == 'routes.yaml') {
@@ -122,7 +133,7 @@ protected function getMainRouteConfig()
122133
*/
123134
protected function graphQLRouteExists()
124135
{
125-
$routerResources = $this->getContainer()->get('router')->getRouteCollection()->getResources();
136+
$routerResources = $this->container->get('router')->getRouteCollection()->getResources();
126137
foreach ($routerResources as $resource) {
127138
/** @var FileResource|DirectoryResource $resource */
128139
if (method_exists($resource, 'getResource') && strpos($resource->getResource(), 'GraphQLController.php') !== false) {
@@ -135,7 +146,6 @@ protected function graphQLRouteExists()
135146

136147
protected function generateRoutes()
137148
{
138-
139149
}
140150

141151
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
}

DependencyInjection/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class Configuration implements ConfigurationInterface
1818
*/
1919
public function getConfigTreeBuilder()
2020
{
21-
$treeBuilder = new TreeBuilder();
22-
$rootNode = $treeBuilder->root('graphql');
21+
$treeBuilder = new TreeBuilder('graphql');
22+
$rootNode = $treeBuilder->getRootNode();
2323

2424
$rootNode
2525
->children()

DependencyInjection/GraphQLExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function getDefaultHeaders()
5858
];
5959
}
6060

61-
public function getAlias()
61+
public function getAlias(): string
6262
{
6363
return "graphql";
6464
}

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151

5252
<service id="graphql.command.configure" class="Youshido\GraphQLBundle\Command\GraphQLConfigureCommand">
5353
<tag name="console.command" command="graphql:configure"/>
54+
<argument type="service" id="service_container"/>
55+
</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" />
5460
</service>
5561
</services>
5662
</container>

Tests/DependencyInjection/GraphQLExtensionTest.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
namespace Youshido\GraphQLBundle\Tests\DependencyInjection;
44

5-
5+
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Config\FileLocator;
7-
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
7+
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
1010
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1111
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1212
use Youshido\GraphQLBundle\DependencyInjection\GraphQLExtension;
1313

14-
class GraphQLExtensionTest extends \PHPUnit_Framework_TestCase
14+
class GraphQLExtensionTest extends TestCase
1515
{
1616
public function testDefaultConfigIsUsed()
1717
{
@@ -22,15 +22,17 @@ public function testDefaultConfigIsUsed()
2222
$this->assertEquals(null, $container->getParameter('graphql.logger'));
2323
$this->assertEmpty($container->getParameter('graphql.security.white_list'));
2424
$this->assertEmpty($container->getParameter('graphql.security.black_list'));
25-
$this->assertEquals([
26-
'field' => false,
27-
'operation' => false,
28-
],
25+
$this->assertEquals(
26+
[
27+
'field' => false,
28+
'operation' => false,
29+
],
2930
$container->getParameter('graphql.security.guard_config')
3031
);
3132

3233
$this->assertTrue($container->getParameter('graphql.response.json_pretty'));
33-
$this->assertEquals([
34+
$this->assertEquals(
35+
[
3436
'Access-Control-Allow-Origin' => '*',
3537
'Access-Control-Allow-Headers' => 'Content-Type',
3638
],
@@ -47,20 +49,21 @@ public function testDefaultCanBeOverridden()
4749

4850
$this->assertEquals(['hello'], $container->getParameter('graphql.security.black_list'));
4951
$this->assertEquals(['world'], $container->getParameter('graphql.security.white_list'));
50-
$this->assertEquals([
51-
'field' => true,
52-
'operation' => true,
53-
],
52+
$this->assertEquals(
53+
[
54+
'field' => true,
55+
'operation' => true,
56+
],
5457
$container->getParameter('graphql.security.guard_config')
5558
);
5659

5760
$this->assertFalse($container->getParameter('graphql.response.json_pretty'));
58-
$this->assertEquals([
59-
'X-Powered-By' => 'GraphQL',
60-
],
61+
$this->assertEquals(
62+
[
63+
'X-Powered-By' => 'GraphQL',
64+
],
6165
$container->getParameter('graphql.response.headers')
6266
);
63-
6467
}
6568

6669
private function loadContainerFromFile($file, $type, array $services = array(), $skipEnvVars = false)
@@ -75,7 +78,7 @@ private function loadContainerFromFile($file, $type, array $services = array(),
7578
$container->set($id, $service);
7679
}
7780
$container->registerExtension(new GraphQLExtension());
78-
$locator = new FileLocator(__DIR__.'/Fixtures/config/'.$type);
81+
$locator = new FileLocator(__DIR__ . '/Fixtures/config/' . $type);
7982

8083
switch ($type) {
8184
case 'xml':
@@ -91,12 +94,12 @@ private function loadContainerFromFile($file, $type, array $services = array(),
9194
throw new \InvalidArgumentException('Invalid file type');
9295
}
9396

94-
$loader->load($file.'.'.$type);
97+
$loader->load($file . '.' . $type);
9598
$container->getCompilerPassConfig()->setOptimizationPasses(array(
96-
new ResolveDefinitionTemplatesPass(),
99+
new ResolveChildDefinitionsPass(),
97100
));
98101
$container->getCompilerPassConfig()->setRemovingPasses(array());
99102
$container->compile();
100103
return $container;
101104
}
102-
}
105+
}

composer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "youshido/graphql-bundle",
2+
"name": "99designs/graphql-bundle",
33
"description": "Symfony GraphQl Bundle",
44
"license": "MIT",
55
"authors": [
@@ -18,12 +18,14 @@
1818
}
1919
},
2020
"require": {
21-
"php": ">=5.6",
22-
"youshido/graphql": "~1.4"
21+
"99designs/graphql": "~1",
22+
"symfony/security-core": "~4.4 || ~5.4",
23+
"symfony/framework-bundle": "~4.4 || ~5.4",
24+
"php": ">=5.6"
2325
},
2426
"require-dev": {
25-
"phpunit/phpunit": "~4.7",
27+
"phpunit/phpunit": "~9.6",
2628
"composer/composer": "~1.2",
27-
"symfony/framework-bundle": "~2.7|~3.0"
29+
"symfony/yaml": "~4.4 || ~5.4"
2830
}
2931
}

0 commit comments

Comments
 (0)