Total Complexity | 51 |
Total Lines | 416 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SchemaExtender often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SchemaExtender, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class SchemaExtender implements SchemaExtenderInterface |
||
46 | { |
||
47 | use CacheAwareTrait; |
||
48 | |||
49 | private const CACHE_PREFIX = 'GraphQL_SchemaExtender_'; |
||
50 | |||
51 | /** |
||
52 | * @var DefinitionBuilderCreatorInterface |
||
53 | */ |
||
54 | protected $definitionBuilderCreator; |
||
55 | |||
56 | /** |
||
57 | * @var DefinitionBuilderInterface |
||
58 | */ |
||
59 | protected $definitionBuilder; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $typeDefinitionMap; |
||
65 | |||
66 | /** |
||
67 | * @var ObjectTypeExtensionNode[][] |
||
68 | */ |
||
69 | protected $typeExtensionMap; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $directiveDefinitions; |
||
75 | |||
76 | /** |
||
77 | * SchemaExtender constructor. |
||
78 | * @param DefinitionBuilderCreatorInterface $definitionBuilderCreator |
||
79 | */ |
||
80 | public function __construct(DefinitionBuilderCreatorInterface $definitionBuilderCreator, CacheInterface $cache) |
||
81 | { |
||
82 | $this->definitionBuilderCreator = $definitionBuilderCreator; |
||
83 | $this->cache = $cache; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param SchemaInterface $schema |
||
88 | * @param DocumentNode $document |
||
89 | * @param array $options |
||
90 | * @return SchemaInterface |
||
91 | * @throws ExecutionException |
||
92 | */ |
||
93 | public function extend(SchemaInterface $schema, DocumentNode $document, array $options = []): SchemaInterface |
||
94 | { |
||
95 | $this->typeDefinitionMap = []; |
||
96 | $this->typeExtensionsMap = []; |
||
|
|||
97 | $this->directiveDefinitions = []; |
||
98 | |||
99 | foreach ($document->getDefinitions() as $definition) { |
||
100 | if ($definition instanceof TypeDefinitionNodeInterface) { |
||
101 | // Sanity check that none of the defined types conflict with the schema's existing types. |
||
102 | $typeName = $definition->getNameValue(); |
||
103 | $existingType = $schema->getType($typeName); |
||
104 | |||
105 | if (null !== $existingType) { |
||
106 | throw new ExecutionException( |
||
107 | \sprintf( |
||
108 | 'Type "%s" already exists in the schema. It cannot also ' . |
||
109 | 'be defined in this type definition.', |
||
110 | $typeName |
||
111 | ), |
||
112 | [$definition] |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | $typeDefinitionMap[$typeName] = $definition; |
||
117 | |||
118 | continue; |
||
119 | } |
||
120 | |||
121 | if ($definition instanceof ObjectTypeDefinitionNode || $definition instanceof InterfaceTypeDefinitionNode) { |
||
122 | // Sanity check that this type extension exists within the schema's existing types. |
||
123 | $extendedTypeName = $definition->getNameValue(); |
||
124 | $existingType = $schema->getType($extendedTypeName); |
||
125 | |||
126 | if (null === $existingType) { |
||
127 | throw new ExecutionException( |
||
128 | \sprintf( |
||
129 | 'Cannot extend type "%s" because it does not exist in the existing schema.', |
||
130 | $extendedTypeName |
||
131 | ), |
||
132 | [$definition] |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | $this->checkExtensionNode($existingType, $definition); |
||
137 | |||
138 | $existingTypeExtensions = $typeExtensionsMap[$extendedTypeName] ?? []; |
||
139 | $typeExtensionsMap[$extendedTypeName] = \array_merge($existingTypeExtensions, [$definition]); |
||
140 | |||
141 | continue; |
||
142 | } |
||
143 | |||
144 | if ($definition instanceof DirectiveDefinitionNode) { |
||
145 | $directiveName = $definition->getNameValue(); |
||
146 | $existingDirective = $schema->getDirective($directiveName); |
||
147 | |||
148 | if (null !== $existingDirective) { |
||
149 | throw new ExecutionException( |
||
150 | \sprintf( |
||
151 | 'Directive "%s" already exists in the schema. It cannot be redefined.', |
||
152 | $directiveName |
||
153 | ), |
||
154 | [$definition] |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | $directiveDefinitions[] = $definition; |
||
159 | |||
160 | continue; |
||
161 | } |
||
162 | |||
163 | if ($definition instanceof ScalarTypeExtensionNode || |
||
164 | $definition instanceof UnionTypeExtensionNode || |
||
165 | $definition instanceof EnumTypeExtensionNode || |
||
166 | $definition instanceof InputObjectTypeExtensionNode) { |
||
167 | throw new ExecutionException( |
||
168 | \sprintf('The %s kind is not yet supported by extendSchema().', $definition->getKind()) |
||
169 | ); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | // If this document contains no new types, extensions, or directives then |
||
174 | // return the same unmodified GraphQLSchema instance. |
||
175 | if (empty($typeDefinitionMap) && empty($typeExtensionsMap) && empty($directiveDefinitions)) { |
||
176 | return $schema; |
||
177 | } |
||
178 | |||
179 | $resolveTypeFunction = function (NamedTypeNode $node) use ($schema): ?TypeInterface { |
||
180 | $typeName = $node->getNameValue(); |
||
181 | $existingType = $schema->getType($typeName); |
||
182 | |||
183 | if (null !== $existingType) { |
||
184 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
185 | /** @noinspection PhpParamsInspection */ |
||
186 | return $this->getExtendedType($existingType); |
||
187 | } |
||
188 | |||
189 | throw new ExecutionException( |
||
190 | \sprintf( |
||
191 | 'Unknown type: "%s". Ensure that this type exists ' . |
||
192 | 'either in the original schema, or is added in a type definition.', |
||
193 | $typeName |
||
194 | ), |
||
195 | [$node] |
||
196 | ); |
||
197 | }; |
||
198 | |||
199 | $this->definitionBuilder = $this->definitionBuilderCreator->create( |
||
200 | $typeDefinitionMap, |
||
201 | [], |
||
202 | $resolveTypeFunction |
||
203 | ); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param TypeInterface $type |
||
208 | * @param NodeInterface $node |
||
209 | * @throws ExecutionException |
||
210 | */ |
||
211 | protected function checkExtensionNode(TypeInterface $type, NodeInterface $node): void |
||
212 | { |
||
213 | if ($node instanceof ObjectTypeExtensionNode && !($type instanceof ObjectType)) { |
||
214 | throw new ExecutionException( |
||
215 | \sprintf('Cannot extend non-object type "%s".', toString($type)), |
||
216 | [$node] |
||
217 | ); |
||
218 | } |
||
219 | |||
220 | if ($node instanceof InterfaceTypeExtensionNode && !($type instanceof InterfaceType)) { |
||
221 | throw new ExecutionException( |
||
222 | \sprintf('Cannot extend non-interface type "%s".', toString($type)), |
||
223 | [$node] |
||
224 | ); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @param NamedTypeInterface $type |
||
230 | * @return NamedTypeInterface |
||
231 | * @throws InvalidArgumentException |
||
232 | * @throws InvariantException |
||
233 | */ |
||
234 | protected function getExtendedType(NamedTypeInterface $type): NamedTypeInterface |
||
235 | { |
||
236 | $typeName = $type->getName(); |
||
237 | |||
238 | if ($this->isInCache($typeName)) { |
||
239 | $this->setInCache($typeName, $this->extendType($type)); |
||
240 | } |
||
241 | |||
242 | return $this->getFromCache($typeName); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param NamedTypeInterface $type |
||
247 | * @return NamedTypeInterface |
||
248 | * @throws InvariantException |
||
249 | */ |
||
250 | protected function extendType(NamedTypeInterface $type): NamedTypeInterface |
||
251 | { |
||
252 | /** @noinspection PhpParamsInspection */ |
||
253 | if (isIntrospectionType($type)) { |
||
254 | // Introspection types are not extended. |
||
255 | return $type; |
||
256 | } |
||
257 | |||
258 | if ($type instanceof ObjectType) { |
||
259 | return $this->extendObjectType($type); |
||
260 | } |
||
261 | |||
262 | if ($type instanceof InterfaceType) { |
||
263 | return $this->extendInterfaceType($type); |
||
264 | } |
||
265 | |||
266 | if ($type instanceof UnionType) { |
||
267 | return $this->extendUnionType($type); |
||
268 | } |
||
269 | |||
270 | // This type is not yet extendable. |
||
271 | return $type; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @param ObjectType $type |
||
276 | * @return ObjectType |
||
277 | */ |
||
278 | protected function extendObjectType(ObjectType $type): ObjectType |
||
279 | { |
||
280 | $typeName = $type->getName(); |
||
281 | $extensionASTNodes = $type->getExtensionAstNodes(); |
||
282 | |||
283 | if (isset($this->typeExtensionMap[$typeName])) { |
||
284 | $extensionASTNodes = !empty($extensionASTNodes) |
||
285 | ? \array_merge($this->typeExtensionMap[$typeName], $extensionASTNodes) |
||
286 | : $this->typeExtensionMap[$typeName]; |
||
287 | } |
||
288 | |||
289 | return GraphQLObjectType([ |
||
290 | 'name' => $typeName, |
||
291 | 'description' => $type->getDescription(), |
||
292 | 'interfaces' => function () use ($type) { |
||
293 | return $this->extendImplementedInterfaces($type); |
||
294 | }, |
||
295 | 'fields' => function () use ($type) { |
||
296 | return $this->extendFieldMap($type); |
||
297 | }, |
||
298 | 'astNode' => $type->getAstNode(), |
||
299 | 'extensionASTNodes' => $extensionASTNodes, |
||
300 | 'isTypeOf' => $type->getIsTypeOf(), |
||
301 | ]); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param InterfaceType $type |
||
306 | * @return InterfaceType |
||
307 | */ |
||
308 | protected function extendInterfaceType(InterfaceType $type): InterfaceType |
||
309 | { |
||
310 | $typeName = $type->getName(); |
||
311 | $extensionASTNodes = $this->typeExtensionMap[$typeName]; |
||
312 | |||
313 | if (isset($this->typeExtensionMap[$typeName])) { |
||
314 | $extensionASTNodes = !empty($extensionASTNodes) |
||
315 | ? \array_merge($this->typeExtensionMap[$typeName], $extensionASTNodes) |
||
316 | : $this->typeExtensionMap[$typeName]; |
||
317 | } |
||
318 | |||
319 | return GraphQLInterfaceType([ |
||
320 | 'name' => $typeName, |
||
321 | 'description' => $type->getDescription(), |
||
322 | 'fields' => function () use ($type) { |
||
323 | return $this->extendFieldMap($type); |
||
324 | }, |
||
325 | 'astNode' => $type->getAstNode(), |
||
326 | 'extensionASTNodes' => $extensionASTNodes, |
||
327 | 'resolveType' => $type->getResolveType(), |
||
328 | ]); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param UnionType $type |
||
333 | * @return UnionType |
||
334 | * @throws InvariantException |
||
335 | */ |
||
336 | protected function extendUnionType(UnionType $type): UnionType |
||
337 | { |
||
338 | return GraphQLUnionType([ |
||
339 | 'name' => $type->getName(), |
||
340 | 'description' => $type->getDescription(), |
||
341 | 'types' => \array_map(function ($unionType) { |
||
342 | return $this->getExtendedType($unionType); |
||
343 | }, $type->getTypes()), |
||
344 | 'astNode' => $type->getAstNode(), |
||
345 | 'resolveType' => $type->getResolveType(), |
||
346 | ]); |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * @param ObjectType $type |
||
351 | * @return array |
||
352 | * @throws InvariantException |
||
353 | */ |
||
354 | protected function extendImplementedInterfaces(ObjectType $type): array |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @param TypeInterface|ObjectType|InterfaceType $type |
||
379 | * @return array |
||
380 | * @throws InvalidTypeException |
||
381 | * @throws InvariantException |
||
382 | * @throws ExtensionException |
||
383 | * @throws InvalidArgumentException |
||
384 | */ |
||
385 | protected function extendFieldMap(TypeInterface $type): array |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @param TypeInterface $typeDefinition |
||
435 | * @return TypeInterface |
||
436 | * @throws InvalidArgumentException |
||
437 | * @throws InvalidTypeException |
||
438 | * @throws InvariantException |
||
439 | */ |
||
440 | protected function extendFieldType(TypeInterface $typeDefinition): TypeInterface |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * @inheritdoc |
||
457 | */ |
||
458 | protected function getCachePrefix(): string |
||
461 | } |
||
462 | } |
||
463 |