99use Psr \Container \ContainerInterface ;
1010use Psr \SimpleCache \CacheInterface ;
1111use ReflectionClass ;
12+ use ReflectionMethod ;
1213use Symfony \Component \Cache \Adapter \Psr16Adapter ;
1314use Symfony \Contracts \Cache \CacheInterface as CacheContractInterface ;
1415use TheCodingMachine \ClassExplorer \Glob \GlobClassExplorer ;
16+ use TheCodingMachine \GraphQLite \Annotations \Mutation ;
17+ use TheCodingMachine \GraphQLite \Annotations \Query ;
1518use Webmozart \Assert \Assert ;
1619use function class_exists ;
1720use function interface_exists ;
@@ -45,13 +48,15 @@ final class GlobControllerQueryProvider implements QueryProviderInterface
4548 private $ recursive ;
4649 /** @var CacheContractInterface */
4750 private $ cacheContract ;
51+ /** @var AnnotationReader */
52+ private $ annotationReader ;
4853
4954 /**
5055 * @param string $namespace The namespace that contains the GraphQL types (they must have a `@Type` annotation)
5156 * @param ContainerInterface $container The container we will fetch controllers from.
5257 * @param bool $recursive Whether subnamespaces of $namespace must be analyzed.
5358 */
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 )
5560 {
5661 $ this ->namespace = $ namespace ;
5762 $ this ->container = $ container ;
@@ -61,6 +66,7 @@ public function __construct(string $namespace, FieldsBuilder $fieldsBuilder, Con
6166 $ this ->cacheTtl = $ cacheTtl ;
6267 $ this ->fieldsBuilder = $ fieldsBuilder ;
6368 $ this ->recursive = $ recursive ;
69+ $ this ->annotationReader = $ annotationReader ;
6470 }
6571
6672 private function getAggregateControllerQueryProvider (): AggregateControllerQueryProvider
@@ -105,6 +111,9 @@ private function buildInstancesList(): array
105111 if (! $ refClass ->isInstantiable ()) {
106112 continue ;
107113 }
114+ if (! $ this ->hasQueriesOrMutations ($ refClass )) {
115+ continue ;
116+ }
108117 if (! $ this ->container ->has ($ className )) {
109118 continue ;
110119 }
@@ -115,6 +124,24 @@ private function buildInstancesList(): array
115124 return $ instances ;
116125 }
117126
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+
118145 /**
119146 * @return FieldDefinition[]
120147 */
0 commit comments