Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions Command/DebugCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Command;

use Overblog\GraphQLBundle\Resolver\ResolverInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class DebugCommand extends ContainerAwareCommand
{
private static $categories = ['type', 'mutation', 'resolver'];

protected function configure()
{
$this
->setName('graphql:debug')
->setAliases(['debug:graphql'])
->addOption(
'category',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
sprintf('filter by a category (%s).', implode(', ', self::$categories))
)
->setDescription('Display current GraphQL services (types, resolvers and mutations)');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$categoriesOption = $input->getOption('category');
$categoriesOption = is_array($categoriesOption) ? $categoriesOption : [$categoriesOption];
$notAllowed = array_diff($categoriesOption, self::$categories);
if (!empty($notAllowed)) {
throw new \InvalidArgumentException(sprintf('Invalid category (%s)', implode(',', $notAllowed)));
}

$categories = empty($categoriesOption) ? self::$categories : $categoriesOption;

$io = new SymfonyStyle($input, $output);
$tableHeaders = ['id', 'aliases'];
foreach ($categories as $category) {
$io->title(sprintf('GraphQL %ss Services', ucfirst($category)));
/** @var ResolverInterface $resolver */
$resolver = $this->getContainer()->get(sprintf('overblog_graphql.%s_resolver', $category));
$solutions = $this->retrieveSolutions($resolver);
$this->renderTable($tableHeaders, $solutions, $io);
}
}

private function renderTable(array $tableHeaders, array $solutions, SymfonyStyle $io)
{
$tableRows = [];
foreach ($solutions as $id => &$options) {
ksort($options['aliases']);
$tableRows[] = [$id, implode("\n", $options['aliases'])];
}
$io->table($tableHeaders, $tableRows);
$io->write("\n\n");
}

private function retrieveSolutions(ResolverInterface $resolver)
{
$data = [];
foreach ($resolver->getSolutions() as $alias => $solution) {
$options = $resolver->getSolutionOptions($alias);

$id = $options['id'];
if (!isset($data[$id]['aliases'])) {
$data[$id]['aliases'] = [];
}
$data[$id]['aliases'][] = $options['alias'].(isset($options['method']) ? ' (method: '.$options['method'].')' : '');
}
ksort($data);

return $data;
}

public static function getCategories()
{
return self::$categories;
}
}
4 changes: 2 additions & 2 deletions Command/GraphQLDumpSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);
$io = new SymfonyStyle($input, $output);

$format = strtolower($input->getOption('format'));
$schemaName = $input->getOption('schema');
Expand All @@ -62,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$content = $this->createFileContent($requestExecutor, $format, $schemaName);
file_put_contents($file, $content);

$output->success(sprintf('GraphQL schema "%s" was successfully dumped.', realpath($file)));
$io->success(sprintf('GraphQL schema "%s" was successfully dumped.', realpath($file)));
}

private function createFileContent(Executor $requestExecutor, $format, $schemaName)
Expand Down
26 changes: 26 additions & 0 deletions Config/Parser/ParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Config\Parser;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Finder\SplFileInfo;

interface ParserInterface
{
/**
* @param SplFileInfo $file
* @param ContainerBuilder $container
*
* @return array
*/
public static function parse(SplFileInfo $file, ContainerBuilder $container);
}
49 changes: 49 additions & 0 deletions Config/Parser/XmlParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Config\Parser;

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Util\XmlUtils;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\Finder\SplFileInfo;

class XmlParser implements ParserInterface
{
/*
* @param SplFileInfo $file
* @param ContainerBuilder $container
*
* @return array
*/
public static function parse(SplFileInfo $file, ContainerBuilder $container)
{
$typesConfig = [];

try {
//@todo fix xml validateSchema
$xml = XmlUtils::loadFile($file->getRealPath());
foreach ($xml->documentElement->childNodes as $node) {
if (!$node instanceof \DOMElement) {
continue;
}
$values = XmlUtils::convertDomElementToArray($node);
$typesConfig = array_merge($typesConfig, $values);
}
$container->addResource(new FileResource($file->getRealPath()));
} catch (\InvalidArgumentException $e) {
throw new InvalidArgumentException(sprintf('Unable to parse file "%s".', $file), $e->getCode(), $e);
}

return $typesConfig;
}
}
46 changes: 46 additions & 0 deletions Config/Parser/YmlParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Config\Parser;

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;

class YmlParser implements ParserInterface
{
private static $yamlParser;

/**
* @param SplFileInfo $file
* @param ContainerBuilder $container
*
* @return array
*/
public static function parse(SplFileInfo $file, ContainerBuilder $container)
{
if (null === self::$yamlParser) {
self::$yamlParser = new YamlParser();
}

try {
$typesConfig = self::$yamlParser->parse($file->getContents());
$container->addResource(new FileResource($file->getRealPath()));
} catch (ParseException $e) {
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
}

return $typesConfig;
}
}
25 changes: 25 additions & 0 deletions Definition/Resolver/AliasedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Definition\Resolver;

interface AliasedInterface
{
/**
* Returns methods aliases.
*
* For instance:
* array('myMethod' => 'myAlias')
*
* @return array
*/
public static function getAliases();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Relay\Node;
namespace Overblog\GraphQLBundle\Definition\Resolver;

class NodeFieldResolver
interface MutationInterface
{
public function resolve($args, $context, $info, \Closure $idFetcherCallback)
{
return $idFetcherCallback($args['id'], $context, $info);
}
}
16 changes: 16 additions & 0 deletions Definition/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Definition\Resolver;

interface ResolverInterface
{
}
16 changes: 16 additions & 0 deletions Definition/Type/GeneratedTypeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Definition\Type;

interface GeneratedTypeInterface
{
}
Loading