Skip to content

Commit

Permalink
Merge pull request #1 from 99designs/symfony5
Browse files Browse the repository at this point in the history
Add Symfony 4 & 5 support
  • Loading branch information
sinamt committed Aug 15, 2023
2 parents 5f192e7 + 79a7233 commit 731ebb0
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 77 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/.idea/
composer.lock
composer.lock
.phpunit.result.cache
24 changes: 17 additions & 7 deletions Command/GraphQLConfigureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@

namespace Youshido\GraphQLBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;

class GraphQLConfigureCommand extends ContainerAwareCommand
class GraphQLConfigureCommand extends Command
{
const PROJECT_NAMESPACE = 'App';

/** @var Container */
protected $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;

parent::__construct();
}

/**
* {@inheritdoc}
*/
Expand All @@ -31,8 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$isComposerCall = $input->getOption('composer');

$container = $this->getContainer();
$rootDir = $container->getParameter('kernel.root_dir');
$rootDir = $this->container->getParameter('kernel.root_dir');
$configFile = $rootDir . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config/packages/graphql.yml';

$className = 'Schema';
Expand Down Expand Up @@ -105,7 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function getMainRouteConfig()
{
$routerResources = $this->getContainer()->get('router')->getRouteCollection()->getResources();
$routerResources = $this->container->get('router')->getRouteCollection()->getResources();
foreach ($routerResources as $resource) {
/** @var FileResource|DirectoryResource $resource */
if (method_exists($resource, 'getResource') && substr($resource->getResource(), -11) == 'routes.yaml') {
Expand All @@ -122,7 +133,7 @@ protected function getMainRouteConfig()
*/
protected function graphQLRouteExists()
{
$routerResources = $this->getContainer()->get('router')->getRouteCollection()->getResources();
$routerResources = $this->container->get('router')->getRouteCollection()->getResources();
foreach ($routerResources as $resource) {
/** @var FileResource|DirectoryResource $resource */
if (method_exists($resource, 'getResource') && strpos($resource->getResource(), 'GraphQLController.php') !== false) {
Expand All @@ -135,7 +146,6 @@ protected function graphQLRouteExists()

protected function generateRoutes()
{

}

protected function getSchemaClassTemplate($nameSpace, $className = 'Schema')
Expand Down
50 changes: 33 additions & 17 deletions Controller/GraphQLController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Date: 25.11.15
*
Expand All @@ -7,16 +8,26 @@

namespace Youshido\GraphQLBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Youshido\GraphQL\Exception\ConfigurationException;
use Youshido\GraphQLBundle\Exception\UnableToInitializeSchemaServiceException;
use Youshido\GraphQLBundle\Execution\Processor;

class GraphQLController extends Controller
class GraphQLController extends AbstractController
{
protected $container;
protected $params;

public function __construct(ContainerInterface $container, ParameterBagInterface $params)
{
$this->container = $container;
$this->params = $params;
}

/**
* @Route("/graphql")
*
Expand All @@ -36,13 +47,13 @@ public function defaultAction()
);
}

if ($this->get('request_stack')->getCurrentRequest()->getMethod() == 'OPTIONS') {
if ($this->container->get('request_stack')->getCurrentRequest()->getMethod() == 'OPTIONS') {
return $this->createEmptyResponse();
}

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

$queryResponses = array_map(function($queryData) {
$queryResponses = array_map(function ($queryData) {
return $this->executeQuery($queryData['query'], $queryData['variables']);
}, $queries);

Expand All @@ -55,15 +66,15 @@ public function defaultAction()
return $response;
}

private function createEmptyResponse()
protected function createEmptyResponse()
{
return new JsonResponse([], 200, $this->getResponseHeaders());
}

private function executeQuery($query, $variables)
protected function executeQuery($query, $variables)
{
/** @var Processor $processor */
$processor = $this->get('graphql.processor');
$processor = $this->container->get('graphql.processor');
$processor->processPayload($query, $variables);

return $processor->getResponseData();
Expand All @@ -74,9 +85,9 @@ private function executeQuery($query, $variables)
*
* @throws \Exception
*/
private function getPayload()
protected function getPayload()
{
$request = $this->get('request_stack')->getCurrentRequest();
$request = $this->container->get('request_stack')->getCurrentRequest();
$query = $request->get('query', null);
$variables = $request->get('variables', []);
$isMultiQueryRequest = false;
Expand Down Expand Up @@ -135,7 +146,7 @@ private function getPayload()
/**
* @throws \Exception
*/
private function initializeSchemaService()
protected function initializeSchemaService()
{
if ($this->container->initialized('graphql.schema')) {
return;
Expand All @@ -149,9 +160,9 @@ private function initializeSchemaService()
*
* @throws \Exception
*/
private function makeSchemaService()
protected function makeSchemaService()
{
if ($this->container->has($this->getSchemaService())) {
if ($this->getSchemaService() && $this->container->has($this->getSchemaService())) {
return $this->container->get($this->getSchemaService());
}

Expand All @@ -175,27 +186,32 @@ private function makeSchemaService()
/**
* @return string
*/
private function getSchemaClass()
protected function getSchemaClass()
{
return $this->getParameter('graphql.schema_class');
}

/**
* @return string
*/
private function getSchemaService()
protected function getSchemaService()
{
$serviceName = $this->getParameter('graphql.schema_service');

if (substr($serviceName, 0, 1) === '@') {
if (substr($serviceName ?: '', 0, 1) === '@') {
return substr($serviceName, 1, strlen($serviceName) - 1);
}

return $serviceName;
}

private function getResponseHeaders()
protected function getResponseHeaders()
{
return $this->getParameter('graphql.response.headers');
}

protected function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null
{
return $this->params->get($name);
}
}
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('graphql');
$treeBuilder = new TreeBuilder('graphql');
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/GraphQLExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function getDefaultHeaders()
];
}

public function getAlias()
public function getAlias(): string
{
return "graphql";
}
Expand Down
2 changes: 1 addition & 1 deletion GraphQLBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function build(ContainerBuilder $container)
}


public function getContainerExtension()
public function getContainerExtension(): GraphQLExtension
{
if (null === $this->extension) {
$this->extension = new GraphQLExtension();
Expand Down
6 changes: 6 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@

<service id="graphql.command.configure" class="Youshido\GraphQLBundle\Command\GraphQLConfigureCommand">
<tag name="console.command" command="graphql:configure"/>
<argument type="service" id="service_container"/>
</service>

<service id="Youshido\GraphQLBundle\Controller\GraphQLController" public="true" >
<argument type="service" id="service_container" />
<argument type="service" id="parameter_bag" />
</service>
</services>
</container>
43 changes: 23 additions & 20 deletions Tests/DependencyInjection/GraphQLExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Youshido\GraphQLBundle\Tests\DependencyInjection;


use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Youshido\GraphQLBundle\DependencyInjection\GraphQLExtension;

class GraphQLExtensionTest extends \PHPUnit_Framework_TestCase
class GraphQLExtensionTest extends TestCase
{
public function testDefaultConfigIsUsed()
{
Expand All @@ -22,15 +22,17 @@ public function testDefaultConfigIsUsed()
$this->assertEquals(null, $container->getParameter('graphql.logger'));
$this->assertEmpty($container->getParameter('graphql.security.white_list'));
$this->assertEmpty($container->getParameter('graphql.security.black_list'));
$this->assertEquals([
'field' => false,
'operation' => false,
],
$this->assertEquals(
[
'field' => false,
'operation' => false,
],
$container->getParameter('graphql.security.guard_config')
);

$this->assertTrue($container->getParameter('graphql.response.json_pretty'));
$this->assertEquals([
$this->assertEquals(
[
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Headers' => 'Content-Type',
],
Expand All @@ -47,20 +49,21 @@ public function testDefaultCanBeOverridden()

$this->assertEquals(['hello'], $container->getParameter('graphql.security.black_list'));
$this->assertEquals(['world'], $container->getParameter('graphql.security.white_list'));
$this->assertEquals([
'field' => true,
'operation' => true,
],
$this->assertEquals(
[
'field' => true,
'operation' => true,
],
$container->getParameter('graphql.security.guard_config')
);

$this->assertFalse($container->getParameter('graphql.response.json_pretty'));
$this->assertEquals([
'X-Powered-By' => 'GraphQL',
],
$this->assertEquals(
[
'X-Powered-By' => 'GraphQL',
],
$container->getParameter('graphql.response.headers')
);

}

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

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

$loader->load($file.'.'.$type);
$loader->load($file . '.' . $type);
$container->getCompilerPassConfig()->setOptimizationPasses(array(
new ResolveDefinitionTemplatesPass(),
new ResolveChildDefinitionsPass(),
));
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->compile();
return $container;
}
}
}
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "youshido/graphql-bundle",
"name": "99designs/graphql-bundle",
"description": "Symfony GraphQl Bundle",
"license": "MIT",
"authors": [
Expand All @@ -18,12 +18,14 @@
}
},
"require": {
"php": ">=5.6",
"youshido/graphql": "~1.4"
"99designs/graphql": "~1",
"symfony/security-core": "~4.4 || ~5.4",
"symfony/framework-bundle": "~4.4 || ~5.4",
"php": ">=5.6"
},
"require-dev": {
"phpunit/phpunit": "~4.7",
"phpunit/phpunit": "~9.6",
"composer/composer": "~1.2",
"symfony/framework-bundle": "~2.7|~3.0"
"symfony/yaml": "~4.4 || ~5.4"
}
}
Loading

0 comments on commit 731ebb0

Please sign in to comment.