Skip to content

Commit b664bf3

Browse files
authored
Merge pull request #52 from moufmouf/autowire_on_prefetch
Fixing @autowire in prefetch method
2 parents df74e32 + 64cd7ae commit b664bf3

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,16 @@ private function makePublicInjectedServices(ReflectionClass $refClass, Annotatio
291291
{
292292
$services = $this->getCodeCache()->get($refClass, function() use ($refClass, $reader, $container, $isController) {
293293
$services = [];
294-
foreach ($refClass->getMethods() as $method) {
294+
foreach ($refClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
295295
$field = $reader->getRequestAnnotation($method, AbstractRequest::class);
296296
if ($field !== null) {
297297
if ($isController) {
298298
$services[$refClass->getName()] = $refClass->getName();
299299
}
300300
$services += $this->getListOfInjectedServices($method, $container);
301+
if ($field instanceof Field && $field->getPrefetchMethod() !== null) {
302+
$services += $this->getListOfInjectedServices($refClass->getMethod($field->getPrefetchMethod()), $container);
303+
}
301304
}
302305
}
303306
return $services;

Tests/Fixtures/Entities/Contact.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,23 @@ public function injectService(TestGraphqlController $testService = null, stdClas
4747
}
4848
return 'OK';
4949
}
50+
51+
/**
52+
* @Field(prefetchMethod="prefetchData")
53+
*/
54+
public function injectServicePrefetch($prefetchData): string
55+
{
56+
return $prefetchData;
57+
}
58+
59+
/**
60+
* @Autowire(for="$someOtherService", identifier="someOtherService")
61+
*/
62+
public function prefetchData(iterable $iterable, stdClass $someOtherService = null)
63+
{
64+
if ($someOtherService === null) {
65+
return 'KO';
66+
}
67+
return 'OK';
68+
}
5069
}

Tests/Fixtures/config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ services:
2323
someAlias:
2424
alias: someService
2525

26+
someOtherService:
27+
class: stdClass
28+
2629
Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler:
2730
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler
2831
# controllers are imported separately to make sure services can be injected

Tests/FunctionalTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function testServiceAutowiring(): void
8585
{
8686
contact {
8787
injectService
88+
injectServicePrefetch
8889
}
8990
}']);
9091

@@ -96,6 +97,7 @@ public function testServiceAutowiring(): void
9697
'data' => [
9798
'contact' => [
9899
'injectService' => 'OK',
100+
'injectServicePrefetch' => 'OK',
99101
]
100102
]
101103
], $result);

0 commit comments

Comments
 (0)