| Total Complexity | 55 |
| Total Lines | 455 |
| 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 |
||
| 47 | class SchemaExtender implements SchemaExtenderInterface |
||
| 48 | { |
||
| 49 | use CacheAwareTrait; |
||
| 50 | |||
| 51 | private const CACHE_PREFIX = 'GraphQL_SchemaExtender_'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var DefinitionBuilderCreatorInterface |
||
| 55 | */ |
||
| 56 | protected $definitionBuilderCreator; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var DefinitionBuilderInterface |
||
| 60 | */ |
||
| 61 | protected $definitionBuilder; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var TypeExtensionNodeInterface[][] |
||
| 65 | */ |
||
| 66 | protected $typeExtensionsMap; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * SchemaExtender constructor. |
||
| 70 | * @param DefinitionBuilderCreatorInterface $definitionBuilderCreator |
||
| 71 | */ |
||
| 72 | public function __construct(DefinitionBuilderCreatorInterface $definitionBuilderCreator, CacheInterface $cache) |
||
| 73 | { |
||
| 74 | $this->definitionBuilderCreator = $definitionBuilderCreator; |
||
| 75 | $this->typeExtensionsMap = []; |
||
| 76 | $this->cache = $cache; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param SchemaInterface $schema |
||
| 81 | * @param DocumentNode $document |
||
| 82 | * @param array $options |
||
| 83 | * @return SchemaInterface |
||
| 84 | * @throws InvariantException |
||
| 85 | * @throws ExtensionException |
||
| 86 | * @throws InvalidArgumentException |
||
| 87 | */ |
||
| 88 | public function extend(SchemaInterface $schema, DocumentNode $document, array $options = []): SchemaInterface |
||
| 89 | { |
||
| 90 | $typeDefinitionMap = []; |
||
| 91 | $directiveDefinitions = []; |
||
| 92 | |||
| 93 | foreach ($document->getDefinitions() as $definition) { |
||
| 94 | if ($definition instanceof TypeDefinitionNodeInterface) { |
||
| 95 | // Sanity check that none of the defined types conflict with the schema's existing types. |
||
| 96 | $typeName = $definition->getNameValue(); |
||
| 97 | $existingType = $schema->getType($typeName); |
||
| 98 | |||
| 99 | if (null !== $existingType) { |
||
| 100 | throw new ExtensionException( |
||
| 101 | \sprintf( |
||
| 102 | 'Type "%s" already exists in the schema. It cannot also ' . |
||
| 103 | 'be defined in this type definition.', |
||
| 104 | $typeName |
||
| 105 | ), |
||
| 106 | [$definition] |
||
| 107 | ); |
||
| 108 | } |
||
| 109 | |||
| 110 | $typeDefinitionMap[$typeName] = $definition; |
||
| 111 | |||
| 112 | continue; |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($definition instanceof ObjectTypeExtensionNode || $definition instanceof InterfaceTypeExtensionNode) { |
||
| 116 | // Sanity check that this type extension exists within the schema's existing types. |
||
| 117 | $extendedTypeName = $definition->getNameValue(); |
||
| 118 | $existingType = $schema->getType($extendedTypeName); |
||
| 119 | |||
| 120 | if (null === $existingType) { |
||
| 121 | throw new ExtensionException( |
||
| 122 | \sprintf( |
||
| 123 | 'Cannot extend type "%s" because it does not exist in the existing schema.', |
||
| 124 | $extendedTypeName |
||
| 125 | ), |
||
| 126 | [$definition] |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->checkExtensionNode($existingType, $definition); |
||
| 131 | |||
| 132 | $existingTypeExtensions = $this->typeExtensionsMap[$extendedTypeName] ?? []; |
||
| 133 | $this->typeExtensionsMap[$extendedTypeName] = \array_merge($existingTypeExtensions, [$definition]); |
||
| 134 | |||
| 135 | continue; |
||
| 136 | } |
||
| 137 | |||
| 138 | if ($definition instanceof DirectiveDefinitionNode) { |
||
| 139 | $directiveName = $definition->getNameValue(); |
||
| 140 | $existingDirective = $schema->getDirective($directiveName); |
||
| 141 | |||
| 142 | if (null !== $existingDirective) { |
||
| 143 | throw new ExtensionException( |
||
| 144 | \sprintf( |
||
| 145 | 'Directive "%s" already exists in the schema. It cannot be redefined.', |
||
| 146 | $directiveName |
||
| 147 | ), |
||
| 148 | [$definition] |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | |||
| 152 | $directiveDefinitions[] = $definition; |
||
| 153 | |||
| 154 | continue; |
||
| 155 | } |
||
| 156 | |||
| 157 | if ($definition instanceof ScalarTypeExtensionNode || |
||
| 158 | $definition instanceof UnionTypeExtensionNode || |
||
| 159 | $definition instanceof EnumTypeExtensionNode || |
||
| 160 | $definition instanceof InputObjectTypeExtensionNode) { |
||
| 161 | throw new ExtensionException( |
||
| 162 | \sprintf('The %s kind is not yet supported by extendSchema().', $definition->getKind()) |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | // If this document contains no new types, extensions, or directives then |
||
| 168 | // return the same unmodified GraphQLSchema instance. |
||
| 169 | if (empty($typeDefinitionMap) && empty($this->typeExtensionsMap) && empty($directiveDefinitions)) { |
||
| 170 | return $schema; |
||
| 171 | } |
||
| 172 | |||
| 173 | $resolveTypeFunction = function (NamedTypeNode $node) use ($schema): ?TypeInterface { |
||
| 174 | $typeName = $node->getNameValue(); |
||
| 175 | $existingType = $schema->getType($typeName); |
||
| 176 | |||
| 177 | if (null !== $existingType) { |
||
| 178 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
| 179 | /** @noinspection PhpParamsInspection */ |
||
| 180 | return $this->getExtendedType($existingType); |
||
| 181 | } |
||
| 182 | |||
| 183 | throw new ExecutionException( |
||
| 184 | \sprintf( |
||
| 185 | 'Unknown type: "%s". Ensure that this type exists ' . |
||
| 186 | 'either in the original schema, or is added in a type definition.', |
||
| 187 | $typeName |
||
| 188 | ), |
||
| 189 | [$node] |
||
| 190 | ); |
||
| 191 | }; |
||
| 192 | |||
| 193 | $this->definitionBuilder = $this->definitionBuilderCreator->create( |
||
| 194 | $typeDefinitionMap, |
||
| 195 | $resolveTypeFunction |
||
| 196 | ); |
||
| 197 | |||
| 198 | $this->cache->clear(); |
||
| 199 | |||
| 200 | $existingQueryType = $schema->getQueryType(); |
||
| 201 | $existingMutationType = $schema->getMutationType(); |
||
| 202 | $existingSubscriptionType = $schema->getSubscriptionType(); |
||
| 203 | |||
| 204 | /** @noinspection PhpParamsInspection */ |
||
| 205 | return GraphQLSchema([ |
||
| 206 | 'query' => null !== $existingQueryType |
||
| 207 | ? $this->getExtendedType($existingQueryType) |
||
| 208 | : null, |
||
| 209 | 'mutation' => null !== $existingMutationType ? |
||
| 210 | $this->getExtendedType($existingMutationType) |
||
| 211 | : null, |
||
| 212 | 'subscription' => null !== $existingSubscriptionType |
||
| 213 | ? $this->getExtendedType($existingSubscriptionType) |
||
| 214 | : null, |
||
| 215 | 'types' => \array_merge( |
||
| 216 | \array_map(function ($type) { |
||
| 217 | return $this->getExtendedType($type); |
||
| 218 | }, \array_values($schema->getTypeMap())), |
||
| 219 | $this->definitionBuilder->buildTypes(\array_values($typeDefinitionMap)) |
||
| 220 | ), |
||
| 221 | 'directives' => $this->getMergedDirectives($schema, $directiveDefinitions), |
||
| 222 | 'astNode' => $schema->getAstNode(), |
||
| 223 | ]); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param SchemaInterface $schema |
||
| 228 | * @param array $directiveDefinitions |
||
| 229 | * @return Directive[] |
||
| 230 | * @throws InvariantException |
||
| 231 | */ |
||
| 232 | protected function getMergedDirectives(SchemaInterface $schema, array $directiveDefinitions): array |
||
| 233 | { |
||
| 234 | $existingDirectives = $schema->getDirectives(); |
||
| 235 | |||
| 236 | invariant(!empty($existingDirectives), 'schema must have default directives'); |
||
| 237 | |||
| 238 | return \array_merge( |
||
| 239 | $existingDirectives, |
||
| 240 | \array_map(function (DirectiveDefinitionNode $node) { |
||
| 241 | return $this->definitionBuilder->buildDirective($node); |
||
| 242 | }, $directiveDefinitions) |
||
| 243 | ); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param TypeInterface $type |
||
| 248 | * @param NodeInterface $node |
||
| 249 | * @throws ExtensionException |
||
| 250 | */ |
||
| 251 | protected function checkExtensionNode(TypeInterface $type, NodeInterface $node): void |
||
| 252 | { |
||
| 253 | if ($node instanceof ObjectTypeExtensionNode && !($type instanceof ObjectType)) { |
||
| 254 | throw new ExtensionException( |
||
| 255 | \sprintf('Cannot extend non-object type "%s".', toString($type)), |
||
| 256 | [$node] |
||
| 257 | ); |
||
| 258 | } |
||
| 259 | |||
| 260 | if ($node instanceof InterfaceTypeExtensionNode && !($type instanceof InterfaceType)) { |
||
| 261 | throw new ExtensionException( |
||
| 262 | \sprintf('Cannot extend non-interface type "%s".', toString($type)), |
||
| 263 | [$node] |
||
| 264 | ); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param NamedTypeInterface $type |
||
| 270 | * @return NamedTypeInterface |
||
| 271 | * @throws InvalidArgumentException |
||
| 272 | * @throws InvariantException |
||
| 273 | */ |
||
| 274 | protected function getExtendedType(NamedTypeInterface $type): NamedTypeInterface |
||
| 275 | { |
||
| 276 | $typeName = $type->getName(); |
||
| 277 | |||
| 278 | if (!$this->isInCache($typeName)) { |
||
| 279 | $this->setInCache($typeName, $this->extendType($type)); |
||
| 280 | } |
||
| 281 | |||
| 282 | return $this->getFromCache($typeName); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param NamedTypeInterface $type |
||
| 287 | * @return NamedTypeInterface |
||
| 288 | * @throws InvariantException |
||
| 289 | */ |
||
| 290 | protected function extendType(NamedTypeInterface $type): NamedTypeInterface |
||
| 291 | { |
||
| 292 | /** @noinspection PhpParamsInspection */ |
||
| 293 | if (isIntrospectionType($type)) { |
||
| 294 | // Introspection types are not extended. |
||
| 295 | return $type; |
||
| 296 | } |
||
| 297 | |||
| 298 | if ($type instanceof ObjectType) { |
||
| 299 | return $this->extendObjectType($type); |
||
| 300 | } |
||
| 301 | |||
| 302 | if ($type instanceof InterfaceType) { |
||
| 303 | return $this->extendInterfaceType($type); |
||
| 304 | } |
||
| 305 | |||
| 306 | if ($type instanceof UnionType) { |
||
| 307 | return $this->extendUnionType($type); |
||
| 308 | } |
||
| 309 | |||
| 310 | // This type is not yet extendable. |
||
| 311 | return $type; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param ObjectType $type |
||
| 316 | * @return ObjectType |
||
| 317 | */ |
||
| 318 | protected function extendObjectType(ObjectType $type): ObjectType |
||
| 319 | { |
||
| 320 | $typeName = $type->getName(); |
||
| 321 | $extensionASTNodes = $type->getExtensionAstNodes(); |
||
| 322 | |||
| 323 | if (isset($this->typeExtensionsMap[$typeName])) { |
||
| 324 | $extensionASTNodes = !empty($extensionASTNodes) |
||
| 325 | ? \array_merge($this->typeExtensionsMap[$typeName], $extensionASTNodes) |
||
| 326 | : $this->typeExtensionsMap[$typeName]; |
||
| 327 | } |
||
| 328 | |||
| 329 | return GraphQLObjectType([ |
||
| 330 | 'name' => $typeName, |
||
| 331 | 'description' => $type->getDescription(), |
||
| 332 | 'interfaces' => function () use ($type) { |
||
| 333 | return $this->extendImplementedInterfaces($type); |
||
| 334 | }, |
||
| 335 | 'fields' => function () use ($type) { |
||
| 336 | return $this->extendFieldMap($type); |
||
| 337 | }, |
||
| 338 | 'astNode' => $type->getAstNode(), |
||
| 339 | 'extensionASTNodes' => $extensionASTNodes, |
||
| 340 | 'isTypeOf' => $type->getIsTypeOf(), |
||
| 341 | ]); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param InterfaceType $type |
||
| 346 | * @return InterfaceType |
||
| 347 | */ |
||
| 348 | protected function extendInterfaceType(InterfaceType $type): InterfaceType |
||
| 349 | { |
||
| 350 | $typeName = $type->getName(); |
||
| 351 | $extensionASTNodes = $this->typeExtensionsMap[$typeName] ?? []; |
||
| 352 | |||
| 353 | if (isset($this->typeExtensionsMap[$typeName])) { |
||
| 354 | $extensionASTNodes = !empty($extensionASTNodes) |
||
| 355 | ? \array_merge($this->typeExtensionsMap[$typeName], $extensionASTNodes) |
||
| 356 | : $this->typeExtensionsMap[$typeName]; |
||
| 357 | } |
||
| 358 | |||
| 359 | return GraphQLInterfaceType([ |
||
| 360 | 'name' => $typeName, |
||
| 361 | 'description' => $type->getDescription(), |
||
| 362 | 'fields' => function () use ($type) { |
||
| 363 | return $this->extendFieldMap($type); |
||
| 364 | }, |
||
| 365 | 'astNode' => $type->getAstNode(), |
||
| 366 | 'extensionASTNodes' => $extensionASTNodes, |
||
| 367 | 'resolveType' => $type->getResolveType(), |
||
| 368 | ]); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param UnionType $type |
||
| 373 | * @return UnionType |
||
| 374 | * @throws InvariantException |
||
| 375 | */ |
||
| 376 | protected function extendUnionType(UnionType $type): UnionType |
||
| 377 | { |
||
| 378 | return GraphQLUnionType([ |
||
| 379 | 'name' => $type->getName(), |
||
| 380 | 'description' => $type->getDescription(), |
||
| 381 | 'types' => \array_map(function ($unionType) { |
||
| 382 | return $this->getExtendedType($unionType); |
||
| 383 | }, $type->getTypes()), |
||
| 384 | 'astNode' => $type->getAstNode(), |
||
| 385 | 'resolveType' => $type->getResolveType(), |
||
| 386 | ]); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param ObjectType $type |
||
| 391 | * @return array |
||
| 392 | * @throws InvariantException |
||
| 393 | */ |
||
| 394 | protected function extendImplementedInterfaces(ObjectType $type): array |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param TypeInterface|ObjectType|InterfaceType $type |
||
| 419 | * @return array |
||
| 420 | * @throws InvalidTypeException |
||
| 421 | * @throws InvariantException |
||
| 422 | * @throws ExtensionException |
||
| 423 | * @throws InvalidArgumentException |
||
| 424 | */ |
||
| 425 | protected function extendFieldMap(TypeInterface $type): array |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param TypeInterface $typeDefinition |
||
| 476 | * @return TypeInterface |
||
| 477 | * @throws InvalidArgumentException |
||
| 478 | * @throws InvalidTypeException |
||
| 479 | * @throws InvariantException |
||
| 480 | */ |
||
| 481 | protected function extendFieldType(TypeInterface $typeDefinition): TypeInterface |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @inheritdoc |
||
| 498 | */ |
||
| 499 | protected function getCachePrefix(): string |
||
| 502 | } |
||
| 503 | } |
||
| 504 |