Skip to content

Optimize config processing #772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 4, 2020
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
10 changes: 5 additions & 5 deletions src/Definition/ConfigProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Overblog\GraphQLBundle\Definition\ConfigProcessor\ConfigProcessorInterface;

final class ConfigProcessor implements ConfigProcessorInterface
final class ConfigProcessor
{
/**
* @var ConfigProcessorInterface[]
Expand All @@ -30,12 +30,12 @@ public function register(ConfigProcessorInterface $configProcessor): void
$this->processors[] = $configProcessor;
}

public function process(LazyConfig $lazyConfig): LazyConfig
public function process(array $config): array
{
foreach ($this->getProcessors() as $processor) {
$lazyConfig = $processor->process($lazyConfig);
foreach ($this->processors as $processor) {
$config = $processor->process($config);
}

return $lazyConfig;
return $config;
}
}
22 changes: 9 additions & 13 deletions src/Definition/ConfigProcessor/AclConfigProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Overblog\GraphQLBundle\Definition\ConfigProcessor;

use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Definition\LazyConfig;
use Overblog\GraphQLBundle\Error\UserWarning;
use Overblog\GraphQLBundle\Resolver\AccessResolver;
use function is_array;
Expand All @@ -29,6 +28,7 @@ public static function acl(array $fields, AccessResolver $accessResolver, callab
$deniedAccess = static function (): void {
throw new UserWarning('Access denied to this field.');
};

foreach ($fields as &$field) {
if (is_array($field) && isset($field['access']) && true !== $field['access']) {
$accessChecker = $field['access'];
Expand All @@ -47,21 +47,17 @@ public static function acl(array $fields, AccessResolver $accessResolver, callab
return $fields;
}

public function process(LazyConfig $lazyConfig): LazyConfig
public function process(array $config): array
{
$lazyConfig->addPostLoader(function ($config) {
if (isset($config['fields']) && is_callable($config['fields'])) {
$config['fields'] = function () use ($config) {
$fields = $config['fields']();

return static::acl($fields, $this->accessResolver, $this->defaultResolver);
};
}
if (isset($config['fields']) && is_callable($config['fields'])) {
$config['fields'] = function () use ($config) {
$fields = $config['fields']();

return $config;
});
return static::acl($fields, $this->accessResolver, $this->defaultResolver);
};
}

return $lazyConfig;
return $config;
}

private static function findFieldResolver(array $field, ResolveInfo $info, callable $defaultResolver): callable
Expand Down
4 changes: 1 addition & 3 deletions src/Definition/ConfigProcessor/ConfigProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Overblog\GraphQLBundle\Definition\ConfigProcessor;

use Overblog\GraphQLBundle\Definition\LazyConfig;

interface ConfigProcessorInterface
{
public function process(LazyConfig $lazyConfig): LazyConfig;
public function process(array $config): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Overblog\GraphQLBundle\Definition\ConfigProcessor;

use Overblog\GraphQLBundle\Definition\LazyConfig;
use function array_filter;
use function call_user_func;
use function is_array;
use function is_callable;
use const ARRAY_FILTER_USE_BOTH;
Expand All @@ -21,7 +19,7 @@ function ($field, $fieldName) {
$exposed = true;

if (is_array($field) && isset($field['public']) && is_callable($field['public'])) {
$exposed = (bool) call_user_func($field['public'], $fieldName);
$exposed = (bool) $field['public']($fieldName);
}

return $exposed;
Expand All @@ -30,20 +28,16 @@ function ($field, $fieldName) {
);
}

public function process(LazyConfig $lazyConfig): LazyConfig
public function process(array $config): array
{
$lazyConfig->addPostLoader(function ($config) {
if (isset($config['fields']) && is_callable($config['fields'])) {
$config['fields'] = function () use ($config) {
$fields = $config['fields']();
if (isset($config['fields']) && is_callable($config['fields'])) {
$config['fields'] = function () use ($config) {
$fields = $config['fields']();

return static::filter($fields);
};
}
return static::filter($fields);
};
}

return $config;
});

return $lazyConfig;
return $config;
}
}
32 changes: 14 additions & 18 deletions src/Definition/ConfigProcessor/WrapArgumentConfigProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Overblog\GraphQLBundle\Definition\ConfigProcessor;

use Overblog\GraphQLBundle\Definition\ArgumentFactory;
use Overblog\GraphQLBundle\Definition\LazyConfig;
use function is_array;
use function is_callable;

Expand All @@ -18,28 +17,25 @@ public function __construct(ArgumentFactory $argumentFactory)
$this->argumentFactory = $argumentFactory;
}

public function process(LazyConfig $lazyConfig): LazyConfig
public function process(array $config): array
{
$lazyConfig->addPostLoader(function ($config) {
if (isset($config['resolveField']) && is_callable($config['resolveField'])) {
$config['resolveField'] = $this->argumentFactory->wrapResolverArgs($config['resolveField']);
}
if (isset($config['resolveField']) && is_callable($config['resolveField'])) {
$config['resolveField'] = $this->argumentFactory->wrapResolverArgs($config['resolveField']);
}

if (isset($config['fields'])) {
$config['fields'] = function () use ($config) {
$fields = $config['fields'];
if (is_callable($config['fields'])) {
$fields = $config['fields']();
}
if (isset($config['fields'])) {
$config['fields'] = function () use ($config) {
$fields = $config['fields'];

return $this->wrapFieldsArgument($fields);
};
}
if (is_callable($config['fields'])) {
$fields = $config['fields']();
}

return $config;
});
return $this->wrapFieldsArgument($fields);
};
}

return $lazyConfig;
return $config;
}

private function wrapFieldsArgument(array $fields): array
Expand Down
45 changes: 0 additions & 45 deletions src/Definition/LazyConfig.php

This file was deleted.

31 changes: 14 additions & 17 deletions src/Generator/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Murtukov\PHPCodeGenerator\Utils;
use Overblog\GraphQLBundle\Definition\ConfigProcessor;
use Overblog\GraphQLBundle\Definition\GlobalVariables;
use Overblog\GraphQLBundle\Definition\LazyConfig;
use Overblog\GraphQLBundle\Definition\Type\CustomScalarType;
use Overblog\GraphQLBundle\Definition\Type\GeneratedTypeInterface;
use Overblog\GraphQLBundle\Error\ResolveErrors;
Expand Down Expand Up @@ -135,12 +134,10 @@ public function build(array $config, string $type): PhpFile

$class->createConstructor()
->addArgument('configProcessor', ConfigProcessor::class)
->addArgument(TypeGenerator::GLOBAL_VARS, GlobalVariables::class, null)
->append('$configLoader = ', $this->buildConfigLoader($config))
->append('$config = $configProcessor->process(LazyConfig::create($configLoader, '.$this->globalVars.'))->load()')
->append('parent::__construct($config)');

$this->file->addUse(LazyConfig::class);
->addArgument(TypeGenerator::GLOBAL_VARS, GlobalVariables::class)
->append('$config = ', $this->buildConfig($config))
->emptyLine()
->append('parent::__construct($configProcessor->process($config))');

return $this->file;
}
Expand Down Expand Up @@ -201,12 +198,12 @@ protected function wrapTypeRecursive($typeNode)
}

/**
* Builds an arrow function with an array as the return value. Content of
* the array depends on the GraphQL type that is currently being generated.
* Builds a config array compatible with webonyx/graphql-php type system. The content
* of the array depends on the GraphQL type that is currently being generated.
*
* Render example (object):
*
* fn() => [
* [
* 'name' => self::NAME,
* 'description' => 'Root query type',
* 'fields' => fn() => [
Expand All @@ -223,7 +220,7 @@ protected function wrapTypeRecursive($typeNode)
*
* Render example (input-object):
*
* fn() => [
* [
* 'name' => self::NAME,
* 'description' => 'Some description.',
* 'validation' => {@see buildValidationRules}
Expand All @@ -235,7 +232,7 @@ protected function wrapTypeRecursive($typeNode)
*
* Render example (interface)
*
* fn() => [
* [
* 'name' => self::NAME,
* 'description' => 'Some description.',
* 'fields' => fn() => [
Expand All @@ -247,7 +244,7 @@ protected function wrapTypeRecursive($typeNode)
*
* Render example (union):
*
* fn() => [
* [
* 'name' => self::NAME,
* 'description' => 'Some description.',
* 'types' => fn() => [
Expand All @@ -259,7 +256,7 @@ protected function wrapTypeRecursive($typeNode)
*
* Render example (custom-scalar):
*
* fn() => [
* [
* 'name' => self::NAME,
* 'description' => 'Some description'
* 'serialize' => {@see buildScalarCallback},
Expand All @@ -269,7 +266,7 @@ protected function wrapTypeRecursive($typeNode)
*
* Render example (enum):
*
* fn() => [
* [
* 'name' => self::NAME,
* 'values' => [
* 'PUBLISHED' => ['value' => 1],
Expand All @@ -285,7 +282,7 @@ protected function wrapTypeRecursive($typeNode)
* @throws GeneratorException
* @throws UnrecognizedValueTypeException
*/
protected function buildConfigLoader(array $config): ArrowFunction
protected function buildConfig(array $config): Collection
{
// Convert to an object for a better readability
$c = (object) $config;
Expand Down Expand Up @@ -351,7 +348,7 @@ protected function buildConfigLoader(array $config): ArrowFunction
}
}

return new ArrowFunction($configLoader);
return $configLoader; // @phpstan-ignore-line
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Resolver/AccessResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function (Edge $edge) use ($accessChecker, $resolveArgs) {
private function hasAccess(callable $accessChecker, array $resolveArgs = [], $object = null)
{
$resolveArgs[] = $object;
$accessOrPromise = call_user_func_array($accessChecker, $resolveArgs);
$accessOrPromise = $accessChecker(...$resolveArgs);

return $accessOrPromise;
}
Expand Down