Skip to content
Closed
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
2 changes: 1 addition & 1 deletion Command/GraphQLDumpSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private function createFile(InputInterface $input)
$modern = $this->useModernJsonFormat($input);

$result = $this->getRequestExecutor()
->execute($request, [], $schemaName)
->execute($schemaName, $request)
->toArray();

$content = json_encode($modern ? $result : $result['data'], \JSON_PRETTY_PRINT);
Expand Down
265 changes: 265 additions & 0 deletions Config/Parser/GraphQLParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
<?php

namespace Overblog\GraphQLBundle\Config\Parser;

use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Language\AST\ValueNode;
use GraphQL\Language\Parser;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

class GraphQLParser implements ParserInterface
{
/** @var self */
private static $parser;

const DEFINITION_TYPE_MAPPING = [
NodeKind::OBJECT_TYPE_DEFINITION => 'object',
NodeKind::INTERFACE_TYPE_DEFINITION => 'interface',
NodeKind::ENUM_TYPE_DEFINITION => 'enum',
NodeKind::UNION_TYPE_DEFINITION => 'union',
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => 'input-object',
];

/**
* {@inheritdoc}
*/
public static function parse(\SplFileInfo $file, ContainerBuilder $container)
{
$container->addResource(new FileResource($file->getRealPath()));
$content = trim(file_get_contents($file->getPathname()));
$typesConfig = [];

// allow empty files
if (empty($content)) {
return [];
}
if (!self::$parser) {
self::$parser = new static();
}
try {
$ast = Parser::parse($content);
} catch (\Exception $e) {
throw new InvalidArgumentException(sprintf('An error occurred while parsing the file "%s".', $file), $e->getCode(), $e);
}

/** @var Node $typeDef */
foreach ($ast->definitions as $typeDef) {
$typeConfig = self::$parser->typeDefinitionToConfig($typeDef);
$typesConfig[$typeDef->name->value] = $typeConfig;
}

return $typesConfig;
}

protected function typeDefinitionToConfig(Node $typeDef)
{
switch ($typeDef->kind) {
case NodeKind::OBJECT_TYPE_DEFINITION:
case NodeKind::INTERFACE_TYPE_DEFINITION:
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION:
case NodeKind::ENUM_TYPE_DEFINITION:
case NodeKind::UNION_TYPE_DEFINITION:
$config = [];
$this->addTypeFields($typeDef, $config);
$this->addDescription($typeDef, $config);
$this->addInterfaces($typeDef, $config);
$this->addTypes($typeDef, $config);
$this->addValues($typeDef, $config);

return [
'type' => self::DEFINITION_TYPE_MAPPING[$typeDef->kind],
'config' => $config,
];

default:
throw new InvalidArgumentException(
sprintf(
'%s definition is not supported right now.',
preg_replace('@Definition$@', '', $typeDef->kind)
)
);
}
}

/**
* @param Node $typeDef
* @param array $config
*/
private function addTypeFields(Node $typeDef, array &$config)
{
if (!empty($typeDef->fields)) {
$fields = [];
/** @var FieldDefinitionNode|InputValueDefinitionNode $fieldDef */
foreach ($typeDef->fields as $fieldDef) {
$fieldName = $fieldDef->name->value;
$fields[$fieldName] = [];
$this->addType($fieldDef, $fields[$fieldName]);
$this->addDescription($fieldDef, $fields[$fieldName]);
$this->addDefaultValue($fieldDef, $fields[$fieldName]);
$this->addFieldArguments($fieldDef, $fields[$fieldName]);
}
$config['fields'] = $fields;
}
}

/**
* @param Node $fieldDef
* @param array $fieldConf
*/
private function addFieldArguments(Node $fieldDef, array &$fieldConf)
{
if (!empty($fieldDef->arguments)) {
$arguments = [];
/** @var InputValueDefinitionNode $definition */
foreach ($fieldDef->arguments as $definition) {
$name = $definition->name->value;
$arguments[$name] = [];
$this->addType($definition, $arguments[$name]);
$this->addDescription($definition, $arguments[$name]);
$this->addDefaultValue($definition, $arguments[$name]);
}
$fieldConf['arguments'] = $arguments;
}
}

/**
* @param Node $typeDef
* @param array $config
*/
private function addInterfaces(Node $typeDef, array &$config)
{
if (!empty($typeDef->interfaces)) {
$interfaces = [];
foreach ($typeDef->interfaces as $interface) {
$interfaces[] = $this->astTypeNodeToString($interface);
}
$config['interfaces'] = $interfaces;
}
}

/**
* @param Node $typeDef
* @param array $config
*/
private function addTypes(Node $typeDef, array &$config)
{
if (!empty($typeDef->types)) {
$types = [];
foreach ($typeDef->types as $type) {
$types[] = $this->astTypeNodeToString($type);
}
$config['types'] = $types;
}
}

/**
* @param Node $typeDef
* @param array $config
*/
private function addValues(Node $typeDef, array &$config)
{
if (!empty($typeDef->values)) {
$values = [];
foreach ($typeDef->values as $value) {
$values[] = ['value' => $value->name->value];
}
$config['values'] = $values;
}
}

/**
* @param Node $definition
* @param array $config
*/
private function addType(Node $definition, array &$config)
{
if (!empty($definition->type)) {
$config['type'] = $this->astTypeNodeToString($definition->type);
}
}

/**
* @param Node $definition
* @param array $config
*/
private function addDescription(Node $definition, array &$config)
{
if (
!empty($definition->description)
&& $description = $this->cleanAstDescription($definition->description)
) {
$config['description'] = $description;
}
}

/**
* @param InputValueDefinitionNode|FieldDefinitionNode $definition
* @param array $config
*/
private function addDefaultValue($definition, array &$config)
{
if (!empty($definition->defaultValue)) {
$config['defaultValue'] = $this->astValueNodeToConfig($definition->defaultValue);
}
}

private function astTypeNodeToString(TypeNode $typeNode)
{
$type = '';
switch ($typeNode->kind) {
case NodeKind::NAMED_TYPE:
$type = $typeNode->name->value;
break;

case NodeKind::NON_NULL_TYPE:
$type = $this->astTypeNodeToString($typeNode->type).'!';
break;

case NodeKind::LIST_TYPE:
$type = '['.$this->astTypeNodeToString($typeNode->type).']';
break;
}

return $type;
}

private function astValueNodeToConfig(ValueNode $valueNode)
{
$config = null;
switch ($valueNode->kind) {
case NodeKind::INT:
case NodeKind::FLOAT:
case NodeKind::STRING:
case NodeKind::BOOLEAN:
case NodeKind::ENUM:
$config = $valueNode->value;
break;

case NodeKind::LST:
$config = [];
foreach ($valueNode->values as $node) {
$config[] = $this->astValueNodeToConfig($node);
}
break;

case NodeKind::NULL:
$config = null;
break;
}

return $config;
}

private function cleanAstDescription($description)
{
$description = trim($description);

return empty($description) ? null : $description;
}
}
5 changes: 2 additions & 3 deletions Config/Parser/ParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace Overblog\GraphQLBundle\Config\Parser;

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

interface ParserInterface
{
/**
* @param SplFileInfo $file
* @param \SplFileInfo $file
* @param ContainerBuilder $container
*
* @return array
*/
public static function parse(SplFileInfo $file, ContainerBuilder $container);
public static function parse(\SplFileInfo $file, ContainerBuilder $container);
}
11 changes: 3 additions & 8 deletions Config/Parser/XmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@
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
* {@inheritdoc}
*/
public static function parse(SplFileInfo $file, ContainerBuilder $container)
public static function parse(\SplFileInfo $file, ContainerBuilder $container)
{
$typesConfig = [];

$container->addResource(new FileResource($file->getRealPath()));
try {
$xml = XmlUtils::loadFile($file->getRealPath());
foreach ($xml->documentElement->childNodes as $node) {
Expand All @@ -31,7 +27,6 @@ public static function parse(SplFileInfo $file, ContainerBuilder $container)
$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);
}
Expand Down
12 changes: 4 additions & 8 deletions Config/Parser/YamlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;

Expand All @@ -15,20 +14,17 @@ class YamlParser implements ParserInterface
private static $yamlParser;

/**
* @param SplFileInfo $file
* @param ContainerBuilder $container
*
* @return array
* {@inheritdoc}
*/
public static function parse(SplFileInfo $file, ContainerBuilder $container)
public static function parse(\SplFileInfo $file, ContainerBuilder $container)
{
if (null === self::$yamlParser) {
self::$yamlParser = new Parser();
}
$container->addResource(new FileResource($file->getRealPath()));

try {
$typesConfig = self::$yamlParser->parse($file->getContents());
$container->addResource(new FileResource($file->getRealPath()));
$typesConfig = self::$yamlParser->parse(file_get_contents($file->getPathname()));
} catch (ParseException $e) {
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
}
Expand Down
9 changes: 2 additions & 7 deletions Controller/GraphController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,7 @@ private function processBatchQuery(Request $request, $schemaName = null)

foreach ($queries as $query) {
$payloadResult = $this->requestExecutor->execute(
[
'query' => $query['query'],
'variables' => $query['variables'],
],
[],
$schemaName
$schemaName, ['query' => $query['query'], 'variables' => $query['variables']]
);
$payloads[] = $apolloBatching ? $payloadResult->toArray() : ['id' => $query['id'], 'payload' => $payloadResult->toArray()];
}
Expand All @@ -112,6 +107,6 @@ private function processNormalQuery(Request $request, $schemaName = null)
{
$params = $this->requestParser->parse($request);

return $this->requestExecutor->execute($params, [], $schemaName)->toArray();
return $this->requestExecutor->execute($schemaName, $params)->toArray();
}
}
Loading