Skip to content

Commit 8f77a03

Browse files
authored
Merge pull request #837 from mcg-web/unchange-service-accessible
Keep original accessibility option for services tagged as resolver
2 parents 750d778 + 3e07b7b commit 8f77a03

File tree

6 files changed

+15
-87
lines changed

6 files changed

+15
-87
lines changed

src/DependencyInjection/Compiler/TaggedServiceMappingPass.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler;
66

77
use InvalidArgumentException;
8+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
89
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
910
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1011
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -63,13 +64,11 @@ public function process(ContainerBuilder $container): void
6364
$serviceID = $attributes['id'];
6465

6566
$solutionDefinition = $container->findDefinition($serviceID);
66-
// make solution service public to improve lazy loading
67-
$solutionDefinition->setPublic(true);
6867
$this->autowireSolutionImplementingContainerAwareInterface($solutionDefinition, empty($attributes['generated']));
6968

7069
$resolverDefinition->addMethodCall(
7170
'addSolution',
72-
[$solutionID, [[new Reference('service_container'), 'get'], [$serviceID]], $aliases, $attributes]
71+
[$solutionID, new ServiceClosureArgument(new Reference($serviceID)), $aliases, $attributes]
7372
);
7473
}
7574
}

src/Resolver/AbstractResolver.php

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44

55
namespace Overblog\GraphQLBundle\Resolver;
66

7-
use GraphQL\Type\Definition\Type;
87
use function array_keys;
9-
use function call_user_func_array;
10-
use function get_class;
11-
use function is_array;
12-
use function is_callable;
13-
use function sprintf;
148

159
abstract class AbstractResolver implements FluentResolverInterface
1610
{
@@ -19,23 +13,12 @@ abstract class AbstractResolver implements FluentResolverInterface
1913
private array $solutionOptions = [];
2014
private array $fullyLoadedSolutions = [];
2115

22-
public function addSolution(string $id, $solutionOrFactory, array $aliases = [], array $options = []): self
16+
public function addSolution(string $id, callable $factory, array $aliases = [], array $options = []): self
2317
{
2418
$this->fullyLoadedSolutions[$id] = false;
2519
$this->addAliases($id, $aliases);
2620

27-
$this->solutions[$id] = function () use ($id, $solutionOrFactory) {
28-
$solution = $solutionOrFactory;
29-
if (self::isSolutionFactory($solutionOrFactory)) {
30-
if (!isset($solutionOrFactory[1])) {
31-
$solutionOrFactory[1] = [];
32-
}
33-
$solution = call_user_func_array(...$solutionOrFactory);
34-
}
35-
$this->checkSolution($id, $solution);
36-
37-
return $solution;
38-
};
21+
$this->solutions[$id] = $factory;
3922
$this->solutionOptions[$id] = $options;
4023

4124
return $this;
@@ -78,7 +61,7 @@ public function getSolutionOptions(string $id)
7861
{
7962
$id = $this->resolveAlias($id);
8063

81-
return isset($this->solutionOptions[$id]) ? $this->solutionOptions[$id] : [];
64+
return $this->solutionOptions[$id] ?? [];
8265
}
8366

8467
/**
@@ -117,17 +100,9 @@ private function addAliases(string $id, array $aliases): void
117100
}
118101
}
119102

120-
/**
121-
* @param object|array $solutionOrFactory
122-
*/
123-
private static function isSolutionFactory($solutionOrFactory): bool
124-
{
125-
return is_array($solutionOrFactory) && isset($solutionOrFactory[0]) && is_callable($solutionOrFactory[0]);
126-
}
127-
128103
private function resolveAlias(string $alias): string
129104
{
130-
return isset($this->aliases[$alias]) ? $this->aliases[$alias] : $alias;
105+
return $this->aliases[$alias] ?? $alias;
131106
}
132107

133108
/**
@@ -141,36 +116,4 @@ private function loadSolutions(): array
141116

142117
return $this->solutions;
143118
}
144-
145-
/**
146-
* @param mixed $solution
147-
*/
148-
protected function supportsSolution($solution): bool
149-
{
150-
$supportedClass = $this->supportedSolutionClass();
151-
152-
return null === $supportedClass || $solution instanceof $supportedClass;
153-
}
154-
155-
/**
156-
* @param mixed $solution
157-
*/
158-
protected function checkSolution(string $id, $solution): void
159-
{
160-
if (!$this->supportsSolution($solution)) {
161-
throw new UnsupportedResolverException(
162-
sprintf('Resolver "%s" must be "%s" "%s" given.', $id, $this->supportedSolutionClass(), get_class($solution))
163-
);
164-
}
165-
}
166-
167-
/**
168-
* default return null to accept mixed type.
169-
*
170-
* @return string|null supported class name
171-
*/
172-
protected function supportedSolutionClass(): ?string
173-
{
174-
return null;
175-
}
176119
}

src/Resolver/FluentResolverInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public function resolve($input);
1616
/**
1717
* Add a solution to resolver.
1818
*
19-
* @param string $id the solution identifier
20-
* @param array|mixed $solutionOrFactory the solution itself or array with a factory and it arguments if needed [$factory] or [$factory, $factoryArgs]
21-
* @param string[] $aliases the solution aliases
22-
* @param array $options extra options
19+
* @param string $id the solution identifier
20+
* @param callable $factory the solution factory
21+
* @param string[] $aliases the solution aliases
22+
* @param array $options extra options
2323
*
2424
* @return $this
2525
*/
26-
public function addSolution(string $id, $solutionOrFactory, array $aliases = [], array $options = []): self;
26+
public function addSolution(string $id, callable $factory, array $aliases = [], array $options = []): self;
2727

2828
/**
2929
* @return mixed

tests/DependencyInjection/Compiler/ResolverTaggedServiceMappingPassTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function testCompilationWorksPassConfigDirective(): void
4040
{
4141
$testResolver = new Definition(ResolverTestService::class);
4242
$testResolver
43+
->setPublic(true)
4344
->addTag('overblog_graphql.query', [
4445
'alias' => 'test_resolver', 'method' => 'doSomethingWithContainer',
4546
]);

tests/Resolver/AbstractProxyResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class AbstractProxyResolverTest extends AbstractResolverTest
1212
protected function getResolverSolutionsMapping(): array
1313
{
1414
return [
15-
'Toto' => ['factory' => [[$this, 'createToto'], []], 'aliases' => ['foo', 'bar', 'baz'], 'method' => 'resolve'],
15+
'Toto' => ['factory' => [$this, 'createToto'], 'aliases' => ['foo', 'bar', 'baz'], 'method' => 'resolve'],
1616
];
1717
}
1818

tests/Resolver/TypeResolverTest.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
namespace Overblog\GraphQLBundle\Tests\Resolver;
66

77
use GraphQL\Type\Definition\ObjectType;
8-
use GraphQL\Type\Definition\Type;
98
use Overblog\GraphQLBundle\Resolver\TypeResolver;
109
use Overblog\GraphQLBundle\Resolver\UnresolvableException;
11-
use Overblog\GraphQLBundle\Resolver\UnsupportedResolverException;
12-
use stdClass;
13-
use function sprintf;
1410

1511
class TypeResolverTest extends AbstractResolverTest
1612
{
@@ -22,8 +18,8 @@ protected function createResolver(): TypeResolver
2218
protected function getResolverSolutionsMapping(): array
2319
{
2420
return [
25-
'Toto' => ['factory' => [[$this, 'createObjectType'], [['name' => 'Toto']]], 'aliases' => ['foo']],
26-
'Tata' => ['factory' => [[$this, 'createObjectType'], [['name' => 'Tata']]], 'aliases' => ['bar']],
21+
'Toto' => ['factory' => fn () => $this->createObjectType(['name' => 'Toto']), 'aliases' => ['foo']],
22+
'Tata' => ['factory' => fn () => $this->createObjectType(['name' => 'Tata']), 'aliases' => ['bar']],
2723
];
2824
}
2925

@@ -32,17 +28,6 @@ public function createObjectType(array $config): ObjectType
3228
return new ObjectType($config);
3329
}
3430

35-
public function testAddNotSupportedSolution(): void
36-
{
37-
$this->expectException(UnsupportedResolverException::class);
38-
$this->expectExceptionMessage(sprintf(
39-
'Resolver "not-supported" must be "%s" "stdClass" given.',
40-
Type::class
41-
));
42-
$this->resolver->addSolution('not-supported', new stdClass());
43-
$this->resolver->getSolution('not-supported');
44-
}
45-
4631
public function testResolveKnownType(): void
4732
{
4833
$type = $this->resolver->resolve('Toto');

0 commit comments

Comments
 (0)