9
9
use Psr \Container \ContainerInterface ;
10
10
use Psr \SimpleCache \CacheInterface ;
11
11
use ReflectionClass ;
12
+ use ReflectionMethod ;
12
13
use Symfony \Component \Cache \Adapter \Psr16Adapter ;
13
14
use Symfony \Contracts \Cache \CacheInterface as CacheContractInterface ;
14
15
use TheCodingMachine \ClassExplorer \Glob \GlobClassExplorer ;
16
+ use TheCodingMachine \GraphQLite \Annotations \Mutation ;
17
+ use TheCodingMachine \GraphQLite \Annotations \Query ;
15
18
use Webmozart \Assert \Assert ;
16
19
use function class_exists ;
17
20
use function interface_exists ;
@@ -45,13 +48,15 @@ final class GlobControllerQueryProvider implements QueryProviderInterface
45
48
private $ recursive ;
46
49
/** @var CacheContractInterface */
47
50
private $ cacheContract ;
51
+ /** @var AnnotationReader */
52
+ private $ annotationReader ;
48
53
49
54
/**
50
55
* @param string $namespace The namespace that contains the GraphQL types (they must have a `@Type` annotation)
51
56
* @param ContainerInterface $container The container we will fetch controllers from.
52
57
* @param bool $recursive Whether subnamespaces of $namespace must be analyzed.
53
58
*/
54
- public function __construct (string $ namespace , FieldsBuilder $ fieldsBuilder , ContainerInterface $ container , CacheInterface $ cache , ?ClassNameMapper $ classNameMapper = null , ?int $ cacheTtl = null , bool $ recursive = true )
59
+ public function __construct (string $ namespace , FieldsBuilder $ fieldsBuilder , ContainerInterface $ container , AnnotationReader $ annotationReader , CacheInterface $ cache , ?ClassNameMapper $ classNameMapper = null , ?int $ cacheTtl = null , bool $ recursive = true )
55
60
{
56
61
$ this ->namespace = $ namespace ;
57
62
$ this ->container = $ container ;
@@ -61,6 +66,7 @@ public function __construct(string $namespace, FieldsBuilder $fieldsBuilder, Con
61
66
$ this ->cacheTtl = $ cacheTtl ;
62
67
$ this ->fieldsBuilder = $ fieldsBuilder ;
63
68
$ this ->recursive = $ recursive ;
69
+ $ this ->annotationReader = $ annotationReader ;
64
70
}
65
71
66
72
private function getAggregateControllerQueryProvider (): AggregateControllerQueryProvider
@@ -105,6 +111,9 @@ private function buildInstancesList(): array
105
111
if (! $ refClass ->isInstantiable ()) {
106
112
continue ;
107
113
}
114
+ if (! $ this ->hasQueriesOrMutations ($ refClass )) {
115
+ continue ;
116
+ }
108
117
if (! $ this ->container ->has ($ className )) {
109
118
continue ;
110
119
}
@@ -115,6 +124,24 @@ private function buildInstancesList(): array
115
124
return $ instances ;
116
125
}
117
126
127
+ /**
128
+ * @param ReflectionClass<object> $reflectionClass
129
+ */
130
+ private function hasQueriesOrMutations (ReflectionClass $ reflectionClass ): bool
131
+ {
132
+ foreach ($ reflectionClass ->getMethods (ReflectionMethod::IS_PUBLIC ) as $ refMethod ) {
133
+ $ queryAnnotation = $ this ->annotationReader ->getRequestAnnotation ($ refMethod , Query::class);
134
+ if ($ queryAnnotation !== null ) {
135
+ return true ;
136
+ }
137
+ $ mutationAnnotation = $ this ->annotationReader ->getRequestAnnotation ($ refMethod , Mutation::class);
138
+ if ($ mutationAnnotation !== null ) {
139
+ return true ;
140
+ }
141
+ }
142
+ return false ;
143
+ }
144
+
118
145
/**
119
146
* @return FieldDefinition[]
120
147
*/
0 commit comments