1010use phpDocumentor \Reflection \Types \Mixed_ ;
1111use phpDocumentor \Reflection \Types \Object_ ;
1212use phpDocumentor \Reflection \Types \String_ ;
13+ use Psr \Container \ContainerInterface ;
1314use Roave \BetterReflection \Reflection \ReflectionClass ;
1415use Roave \BetterReflection \Reflection \ReflectionMethod ;
1516use Doctrine \Common \Annotations \Reader ;
1617use phpDocumentor \Reflection \Types \Integer ;
18+ use TheCodingMachine \GraphQL \Controllers \Annotations \AbstractRequest ;
1719use TheCodingMachine \GraphQL \Controllers \Annotations \Logged ;
1820use TheCodingMachine \GraphQL \Controllers \Annotations \Mutation ;
1921use TheCodingMachine \GraphQL \Controllers \Annotations \Query ;
2022use TheCodingMachine \GraphQL \Controllers \Annotations \Right ;
2123use TheCodingMachine \GraphQL \Controllers \Reflection \CommentParser ;
24+ use TheCodingMachine \GraphQL \Controllers \Registry \EmptyContainer ;
25+ use TheCodingMachine \GraphQL \Controllers \Registry \Registry ;
2226use TheCodingMachine \GraphQL \Controllers \Security \AuthenticationServiceInterface ;
2327use TheCodingMachine \GraphQL \Controllers \Security \AuthorizationServiceInterface ;
2428use Youshido \GraphQL \Field \Field ;
3034use Youshido \GraphQL \Type \Scalar \IntType ;
3135use Youshido \GraphQL \Type \Scalar \StringType ;
3236use Youshido \GraphQL \Type \TypeInterface ;
33- use Youshido \GraphQL \Type \Union \UnionType ;
3437
3538/**
3639 * A query provider that looks for queries in a "controller"
@@ -61,18 +64,23 @@ class ControllerQueryProvider implements QueryProviderInterface
6164 * @var AuthorizationServiceInterface
6265 */
6366 private $ authorizationService ;
67+ /**
68+ * @var ContainerInterface
69+ */
70+ private $ registry ;
6471
6572 /**
6673 * @param object $controller
6774 */
68- public function __construct ($ controller , Reader $ annotationReader , TypeMapperInterface $ typeMapper , HydratorInterface $ hydrator , AuthenticationServiceInterface $ authenticationService , AuthorizationServiceInterface $ authorizationService )
75+ public function __construct ($ controller , Reader $ annotationReader , TypeMapperInterface $ typeMapper , HydratorInterface $ hydrator , AuthenticationServiceInterface $ authenticationService , AuthorizationServiceInterface $ authorizationService, ? ContainerInterface $ container = null )
6976 {
7077 $ this ->controller = $ controller ;
7178 $ this ->annotationReader = $ annotationReader ;
7279 $ this ->typeMapper = $ typeMapper ;
7380 $ this ->hydrator = $ hydrator ;
7481 $ this ->authenticationService = $ authenticationService ;
7582 $ this ->authorizationService = $ authorizationService ;
83+ $ this ->registry = new Registry ($ container ?: new EmptyContainer ());
7684 }
7785
7886 /**
@@ -106,6 +114,7 @@ private function getFieldsByAnnotations(string $annotationName): array
106114 $ standardPhpMethod = new \ReflectionMethod (get_class ($ this ->controller ), $ refMethod ->getName ());
107115 // First, let's check the "Query" annotation
108116 $ queryAnnotation = $ this ->annotationReader ->getMethodAnnotation ($ standardPhpMethod , $ annotationName );
117+ /* @var $queryAnnotation AbstractRequest */
109118
110119 if ($ queryAnnotation !== null ) {
111120 $ docBlock = new CommentParser ($ refMethod ->getDocComment ());
@@ -119,11 +128,16 @@ private function getFieldsByAnnotations(string $annotationName): array
119128
120129 $ phpdocType = $ typeResolver ->resolve ((string ) $ refMethod ->getReturnType ());
121130
122- try {
123- $ type = $ this ->mapType ($ phpdocType , $ refMethod ->getDocBlockReturnTypes (), $ standardPhpMethod ->getReturnType ()->allowsNull (), false );
124- } catch (TypeMappingException $ e ) {
125- throw TypeMappingException::wrapWithReturnInfo ($ e , $ refMethod );
131+ if ($ queryAnnotation ->getReturnType ()) {
132+ $ type = $ this ->registry ->get ($ queryAnnotation ->getReturnType ());
133+ } else {
134+ try {
135+ $ type = $ this ->mapType ($ phpdocType , $ refMethod ->getDocBlockReturnTypes (), $ standardPhpMethod ->getReturnType ()->allowsNull (), false );
136+ } catch (TypeMappingException $ e ) {
137+ throw TypeMappingException::wrapWithReturnInfo ($ e , $ refMethod );
138+ }
126139 }
140+
127141 $ queryList [] = new QueryField ($ methodName , $ type , $ args , [$ this ->controller , $ methodName ], $ this ->hydrator , $ docBlock ->getComment ());
128142 }
129143 }
0 commit comments