Skip to content

Commit

Permalink
Merge pull request overblog#815 from murtukov/refactor/input-validator
Browse files Browse the repository at this point in the history
Optimize validator
  • Loading branch information
murtukov authored Feb 1, 2021
2 parents 4d0aaf9 + 779caea commit 244ff31
Show file tree
Hide file tree
Showing 21 changed files with 396 additions and 384 deletions.
15 changes: 15 additions & 0 deletions src/Definition/GraphQLServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Overblog\GraphQLBundle\Definition;

use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use LogicException;
use Overblog\GraphQLBundle\Resolver\MutationResolver;
use Overblog\GraphQLBundle\Resolver\QueryResolver;
use Overblog\GraphQLBundle\Resolver\TypeResolver;
use Overblog\GraphQLBundle\Validator\InputValidator;

/**
* Container for special services to be passed to all generated types.
Expand Down Expand Up @@ -81,4 +83,17 @@ public function getType(string $typeName): ?Type
{
return $this->types->resolve($typeName);
}

/**
* Creates an instance of InputValidator
*
* @param mixed $value
* @param mixed $context
*/
public function createInputValidator($value, ArgumentInterface $args, $context, ResolveInfo $info): InputValidator
{
return $this->services['input_validator_factory']->create(
new ResolverArgs($value, $args, $context, $info)
);
}
}
29 changes: 29 additions & 0 deletions src/Definition/ResolverArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Overblog\GraphQLBundle\Definition;

use ArrayObject;
use GraphQL\Type\Definition\ResolveInfo;

class ResolverArgs
{
public ArgumentInterface $args;
public ResolveInfo $info;
public ArrayObject $context;

/** @var mixed */
public $value;

/**
* @param mixed $value
*/
public function __construct($value, ArgumentInterface $args, ArrayObject $context, ResolveInfo $info)
{
$this->value = $value;
$this->args = $args;
$this->context = $context;
$this->info = $info;
}
}
5 changes: 4 additions & 1 deletion src/DependencyInjection/Compiler/ConfigParserPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ private function mappingConfig(array $config, ContainerBuilder $container): arra

// app only config files (yml or xml or graphql)
if ($mappingConfig['auto_discover']['root_dir'] && $container->hasParameter('kernel.root_dir')) {
// @phpstan-ignore-next-line
$typesMappings[] = ['dir' => $container->getParameter('kernel.root_dir').'/config/graphql', 'types' => null];
}
if ($mappingConfig['auto_discover']['bundles']) {
Expand Down Expand Up @@ -212,10 +213,12 @@ function (array $typeMapping) use ($container) {
private function mappingFromBundles(ContainerBuilder $container): array
{
$typesMappings = [];

/** @var array<string, class-string> $bundles */
$bundles = $container->getParameter('kernel.bundles');

// auto detect from bundle
foreach ($bundles as $name => $class) {
foreach ($bundles as $class) {
// skip this bundle
if (OverblogGraphQLBundle::class === $class) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ final class ResolverMapTaggedServiceMappingPass implements CompilerPassInterface
public function process(ContainerBuilder $container): void
{
$resolverMapsSortedBySchema = [];
$resolverMapsBySchemas = $container->getParameter('overblog_graphql.resolver_maps');
$typeDecoratorListenerDefinition = $container->getDefinition(TypeDecoratorListener::class);

/** @var array $resolverMapsBySchemas */
$resolverMapsBySchemas = $container->getParameter('overblog_graphql.resolver_maps');

foreach ($container->findTaggedServiceIds(self::SERVICE_TAG, true) as $serviceId => $tags) {
foreach ($tags as $tag) {
if (!isset($tag['schema'])) {
Expand Down
30 changes: 5 additions & 25 deletions src/DependencyInjection/OverblogGraphQLExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@
use Overblog\GraphQLBundle\EventListener\ErrorHandlerListener;
use Overblog\GraphQLBundle\EventListener\ErrorLoggerListener;
use Overblog\GraphQLBundle\Request\Executor;
use Overblog\GraphQLBundle\Validator\ValidatorFactory;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Validator\Validation;
use function array_fill_keys;
use function class_exists;
use function realpath;
use function sprintf;

Expand All @@ -59,21 +56,22 @@ public function load(array $configs, ContainerBuilder $container): void
$this->setCompilerCacheWarmer($config, $container);
$this->registerForAutoconfiguration($container);
$this->setDefaultFieldResolver($config, $container);
$this->registerValidatorFactory($container);

$container->setParameter($this->getAlias().'.config', $config);
$container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
}

public function getAlias()
public function getAlias(): string
{
return Configuration::NAME;
}

public function getConfiguration(array $config, ContainerBuilder $container)
public function getConfiguration(array $config, ContainerBuilder $container): Configuration
{
return new Configuration(
// @phpstan-ignore-next-line
$container->getParameter('kernel.debug'),
// @phpstan-ignore-next-line
$container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null
);
}
Expand Down Expand Up @@ -104,24 +102,6 @@ private function registerForAutoconfiguration(ContainerBuilder $container): void
->addTag('overblog_graphql.type');
}

private function registerValidatorFactory(ContainerBuilder $container): void
{
if (class_exists(Validation::class)) {
$container->register(ValidatorFactory::class)
->setArguments([
new Reference('validator.validator_factory'),
new Reference('translator.default', $container::NULL_ON_INVALID_REFERENCE),
])
->addTag(
'overblog_graphql.service',
[
'alias' => 'validatorFactory',
'public' => false,
]
);
}
}

private function setDefaultFieldResolver(array $config, ContainerBuilder $container): void
{
$container->setAlias($this->getAlias().'.default_field_resolver', $config['definitions']['default_field_resolver']);
Expand Down Expand Up @@ -295,7 +275,7 @@ private function setServicesAliases(array $config, ContainerBuilder $container):
/**
* Returns a list of custom exceptions mapped to error/warning classes.
*
* @param array<string, string[]> $exceptionConfig
* @param array<string, array<string>> $exceptionConfig
*
* @return array<string, string> Custom exception map, [exception => UserError/UserWarning]
*/
Expand Down
Loading

0 comments on commit 244ff31

Please sign in to comment.