|
40 | 40 | use TheCodingMachine\GraphQLite\Annotations\Mutation; |
41 | 41 | use TheCodingMachine\GraphQLite\Annotations\Parameter; |
42 | 42 | use TheCodingMachine\GraphQLite\Annotations\Query; |
43 | | -use TheCodingMachine\Graphqlite\Bundle\QueryProviders\ControllerQueryProvider; |
44 | 43 | use TheCodingMachine\GraphQLite\FieldsBuilder; |
45 | 44 | use TheCodingMachine\GraphQLite\FieldsBuilderFactory; |
46 | 45 | use TheCodingMachine\GraphQLite\GraphQLException; |
|
52 | 51 | use TheCodingMachine\GraphQLite\Mappers\Root\CompositeRootTypeMapper; |
53 | 52 | use TheCodingMachine\GraphQLite\Mappers\StaticTypeMapper; |
54 | 53 | use TheCodingMachine\GraphQLite\NamingStrategy; |
| 54 | +use TheCodingMachine\GraphQLite\SchemaFactory; |
55 | 55 | use TheCodingMachine\GraphQLite\TypeGenerator; |
56 | 56 | use TheCodingMachine\GraphQLite\Types\MutableObjectType; |
57 | 57 | use TheCodingMachine\GraphQLite\Types\ResolvableInputObjectType; |
@@ -86,10 +86,7 @@ public function process(ContainerBuilder $container) |
86 | 86 | $globTtl = 2; |
87 | 87 | } |
88 | 88 |
|
89 | | - /** |
90 | | - * @var array<string, array<int, string>> |
91 | | - */ |
92 | | - $classToServicesMap = []; |
| 89 | + $schemaFactory = $container->getDefinition(SchemaFactory::class); |
93 | 90 |
|
94 | 91 | foreach ($container->getDefinitions() as $id => $definition) { |
95 | 92 | if ($definition->isAbstract() || $definition->getClass() === null) { |
@@ -125,63 +122,19 @@ public function process(ContainerBuilder $container) |
125 | 122 | } |
126 | 123 |
|
127 | 124 | foreach ($controllersNamespaces as $controllersNamespace) { |
| 125 | + $schemaFactory->addMethodCall('addControllerNamespace', [ $controllersNamespace ]); |
128 | 126 | foreach ($this->getClassList($controllersNamespace) as $className => $refClass) { |
129 | 127 | $this->makePublicInjectedServices($refClass, $reader, $container); |
130 | 128 | } |
131 | 129 | } |
132 | 130 |
|
133 | 131 | foreach ($typesNamespaces as $typeNamespace) { |
| 132 | + $schemaFactory->addMethodCall('addTypeNamespace', [ $typeNamespace ]); |
134 | 133 | foreach ($this->getClassList($typeNamespace) as $className => $refClass) { |
135 | 134 | $this->makePublicInjectedServices($refClass, $reader, $container); |
136 | 135 | } |
137 | 136 | } |
138 | 137 |
|
139 | | - foreach ($container->findTaggedServiceIds('graphql.annotated.controller') as $id => $tag) { |
140 | | - $definition = $container->findDefinition($id); |
141 | | - $class = $definition->getClass(); |
142 | | - if ($class === null) { |
143 | | - throw new \RuntimeException(sprintf('Service %s has no class defined.', $id)); |
144 | | - } |
145 | | - |
146 | | - $reflectionClass = new ReflectionClass($class); |
147 | | - $isController = false; |
148 | | - $method = null; |
149 | | - foreach ($reflectionClass->getMethods() as $method) { |
150 | | - $query = $reader->getRequestAnnotation($method, Query::class); |
151 | | - if ($query !== null) { |
152 | | - $isController = true; |
153 | | - break; |
154 | | - } |
155 | | - $mutation = $reader->getRequestAnnotation($method, Mutation::class); |
156 | | - if ($mutation !== null) { |
157 | | - $isController = true; |
158 | | - break; |
159 | | - } |
160 | | - } |
161 | | - |
162 | | - if ($isController) { |
163 | | - // Let's create a QueryProvider from this controller |
164 | | - $controllerIdentifier = $class.'__QueryProvider'; |
165 | | - $queryProvider = new Definition(ControllerQueryProvider::class); |
166 | | - $queryProvider->setPrivate(true); |
167 | | - $queryProvider->setFactory([self::class, 'createQueryProvider']); |
168 | | - $queryProvider->addArgument(new Reference($id)); |
169 | | - $queryProvider->addArgument(new Reference(FieldsBuilder::class)); |
170 | | - $queryProvider->addTag('graphql.queryprovider'); |
171 | | - $container->setDefinition($controllerIdentifier, $queryProvider); |
172 | | - } |
173 | | - } |
174 | | - |
175 | | - foreach ($typesNamespaces as $typesNamespace) { |
176 | | - $definition = new Definition(GlobTypeMapper::class); |
177 | | - $definition->addArgument($typesNamespace); |
178 | | - $definition->setArgument('$globTtl', $globTtl); |
179 | | - $definition->setAutowired(true); |
180 | | - $definition->addTag('graphql.type_mapper'); |
181 | | - $container->setDefinition('globTypeMapper_'.str_replace('\\', '__', $typesNamespace), $definition); |
182 | | - } |
183 | | - |
184 | | - |
185 | 138 | // Register custom output types |
186 | 139 | $taggedServices = $container->findTaggedServiceIds('graphql.output_type'); |
187 | 140 |
|
@@ -210,12 +163,27 @@ public function process(ContainerBuilder $container) |
210 | 163 | $definition->addMethodCall('setNotMappedTypes', [$customNotMappedTypes]); |
211 | 164 | } |
212 | 165 |
|
213 | | - // Register type mappers |
214 | | - $typeMapperServices = $container->findTaggedServiceIds('graphql.type_mapper'); |
215 | | - $compositeTypeMapper = $container->getDefinition(CompositeTypeMapper::class); |
216 | | - foreach ($typeMapperServices as $id => $tags) { |
| 166 | + // Register graphql.queryprovider |
| 167 | + $this->mapAdderToTag('graphql.queryprovider', 'addQueryProvider', $container, $schemaFactory); |
| 168 | + $this->mapAdderToTag('graphql.root_type_mapper', 'addRootTypeMapper', $container, $schemaFactory); |
| 169 | + $this->mapAdderToTag('graphql.parameter_mapper', 'addParameterMapper', $container, $schemaFactory); |
| 170 | + $this->mapAdderToTag('graphql.field_middleware', 'addFieldMiddleware', $container, $schemaFactory); |
| 171 | + $this->mapAdderToTag('graphql.type_mapper', 'addTypeMapper', $container, $schemaFactory); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Register a method call on SchemaFactory for each tagged service, passing the service in parameter. |
| 176 | + * |
| 177 | + * @param string $tag |
| 178 | + * @param string $methodName |
| 179 | + */ |
| 180 | + private function mapAdderToTag(string $tag, string $methodName, ContainerBuilder $container, Definition $schemaFactory): void |
| 181 | + { |
| 182 | + $taggedServices = $container->findTaggedServiceIds($tag); |
| 183 | + |
| 184 | + foreach ($taggedServices as $id => $tags) { |
217 | 185 | // add the transport service to the TransportChain service |
218 | | - $compositeTypeMapper->addMethodCall('addTypeMapper', [new Reference($id)]); |
| 186 | + $schemaFactory->addMethodCall($methodName, [new Reference($id)]); |
219 | 187 | } |
220 | 188 | } |
221 | 189 |
|
@@ -296,14 +264,6 @@ private static function getParametersByName(ReflectionMethod $method): array |
296 | 264 | return $parameters; |
297 | 265 | } |
298 | 266 |
|
299 | | - /** |
300 | | - * @param object $controller |
301 | | - */ |
302 | | - public static function createQueryProvider($controller, FieldsBuilder $fieldsBuilder): ControllerQueryProvider |
303 | | - { |
304 | | - return new ControllerQueryProvider($controller, $fieldsBuilder); |
305 | | - } |
306 | | - |
307 | 267 | /** |
308 | 268 | * Returns a cached Doctrine annotation reader. |
309 | 269 | * Note: we cannot get the annotation reader service in the container as we are in a compiler pass. |
|
0 commit comments