Skip to content

Commit 624fa15

Browse files
committed
Make TypeBuilder extendable
1 parent 51ca461 commit 624fa15

10 files changed

+199
-18
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator;
6+
7+
use GraphQL\Type\Definition\Type;
8+
use Overblog\GraphQLBundle\Generator\TypeBaseClassProvider\TypeBaseClassProviderInterface;
9+
10+
final class AwareTypeBaseClassProvider
11+
{
12+
/**
13+
* @var TypeBaseClassProviderInterface[]
14+
*/
15+
private array $providers = [];
16+
17+
public function __construct(iterable $providers)
18+
{
19+
if ($providers instanceof \Traversable) {
20+
$providers = iterator_to_array($providers);
21+
}
22+
array_walk($providers, fn (TypeBaseClassProviderInterface $x) => $this->addProvider($x));
23+
}
24+
25+
public function addProvider(TypeBaseClassProviderInterface $provider): void
26+
{
27+
$this->providers[$provider::getType()] = $provider;
28+
}
29+
30+
/**
31+
* @return class-string<Type>
32+
*/
33+
public function getFQCN(string $type): string
34+
{
35+
if (!\array_key_exists($type, $this->providers)) {
36+
throw new \InvalidArgumentException(sprintf('Not configured type required: "%s"', $type));
37+
}
38+
39+
return $this->providers[$type]->getBaseClass();
40+
}
41+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator\TypeBaseClassProvider;
6+
7+
use Overblog\GraphQLBundle\Definition\Type\CustomScalarType;
8+
use Overblog\GraphQLBundle\Enum\TypeEnum;
9+
10+
final class CustomScalarTypeBaseClassProvider implements TypeBaseClassProviderInterface
11+
{
12+
public static function getType(): string
13+
{
14+
return TypeEnum::CUSTOM_SCALAR;
15+
}
16+
17+
public function getBaseClass(): string
18+
{
19+
return CustomScalarType::class;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator\TypeBaseClassProvider;
6+
7+
use GraphQL\Type\Definition\EnumType;
8+
use Overblog\GraphQLBundle\Enum\TypeEnum;
9+
10+
final class EnumTypeBaseClassProvider implements TypeBaseClassProviderInterface
11+
{
12+
public static function getType(): string
13+
{
14+
return TypeEnum::ENUM;
15+
}
16+
17+
public function getBaseClass(): string
18+
{
19+
return EnumType::class;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator\TypeBaseClassProvider;
6+
7+
use GraphQL\Type\Definition\InputObjectType;
8+
use Overblog\GraphQLBundle\Enum\TypeEnum;
9+
10+
final class InputObjectTypeBaseClassProvider implements TypeBaseClassProviderInterface
11+
{
12+
public static function getType(): string
13+
{
14+
return TypeEnum::INPUT_OBJECT;
15+
}
16+
17+
public function getBaseClass(): string
18+
{
19+
return InputObjectType::class;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator\TypeBaseClassProvider;
6+
7+
use GraphQL\Type\Definition\InterfaceType;
8+
use Overblog\GraphQLBundle\Enum\TypeEnum;
9+
10+
final class InterfaceTypeBaseClassProvider implements TypeBaseClassProviderInterface
11+
{
12+
public static function getType(): string
13+
{
14+
return TypeEnum::INTERFACE;
15+
}
16+
17+
public function getBaseClass(): string
18+
{
19+
return InterfaceType::class;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator\TypeBaseClassProvider;
6+
7+
use GraphQL\Type\Definition\ObjectType;
8+
use Overblog\GraphQLBundle\Enum\TypeEnum;
9+
10+
final class ObjectTypeBaseClassProvider implements TypeBaseClassProviderInterface
11+
{
12+
public static function getType(): string
13+
{
14+
return TypeEnum::OBJECT;
15+
}
16+
17+
public function getBaseClass(): string
18+
{
19+
return ObjectType::class;
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator\TypeBaseClassProvider;
6+
7+
use GraphQL\Type\Definition\Type;
8+
9+
interface TypeBaseClassProviderInterface
10+
{
11+
public static function getType(): string;
12+
13+
/**
14+
* @return class-string<Type>
15+
*/
16+
public function getBaseClass(): string;
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Generator\TypeBaseClassProvider;
6+
7+
use GraphQL\Type\Definition\UnionType;
8+
use Overblog\GraphQLBundle\Enum\TypeEnum;
9+
10+
final class UnionTypeBaseClassProvider implements TypeBaseClassProviderInterface
11+
{
12+
public static function getType(): string
13+
{
14+
return TypeEnum::UNION;
15+
}
16+
17+
public function getBaseClass(): string
18+
{
19+
return UnionType::class;
20+
}
21+
}

src/Generator/TypeBuilder.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66

77
use GraphQL\Language\AST\NodeKind;
88
use GraphQL\Language\Parser;
9-
use GraphQL\Type\Definition\EnumType;
10-
use GraphQL\Type\Definition\InputObjectType;
11-
use GraphQL\Type\Definition\InterfaceType;
12-
use GraphQL\Type\Definition\ObjectType;
139
use GraphQL\Type\Definition\ResolveInfo;
1410
use GraphQL\Type\Definition\Type;
15-
use GraphQL\Type\Definition\UnionType;
1611
use Murtukov\PHPCodeGenerator\ArrowFunction;
1712
use Murtukov\PHPCodeGenerator\Closure;
1813
use Murtukov\PHPCodeGenerator\Config;
@@ -25,7 +20,6 @@
2520
use Overblog\GraphQLBundle\Definition\ConfigProcessor;
2621
use Overblog\GraphQLBundle\Definition\GraphQLServices;
2722
use Overblog\GraphQLBundle\Definition\Resolver\AliasedInterface;
28-
use Overblog\GraphQLBundle\Definition\Type\CustomScalarType;
2923
use Overblog\GraphQLBundle\Definition\Type\GeneratedTypeInterface;
3024
use Overblog\GraphQLBundle\Enum\TypeEnum;
3125
use Overblog\GraphQLBundle\Error\ResolveErrors;
@@ -35,7 +29,6 @@
3529
use Overblog\GraphQLBundle\Validator\InputValidator;
3630
use function array_map;
3731
use function class_exists;
38-
use function count;
3932
use function explode;
4033
use function in_array;
4134
use function is_array;
@@ -66,15 +59,7 @@ class TypeBuilder
6659
protected const DOCBLOCK_TEXT = 'THIS FILE WAS GENERATED AND SHOULD NOT BE EDITED MANUALLY.';
6760
protected const BUILT_IN_TYPES = [Type::STRING, Type::INT, Type::FLOAT, Type::BOOLEAN, Type::ID];
6861

69-
protected const EXTENDS = [
70-
TypeEnum::OBJECT => ObjectType::class,
71-
TypeEnum::INPUT_OBJECT => InputObjectType::class,
72-
TypeEnum::INTERFACE => InterfaceType::class,
73-
TypeEnum::UNION => UnionType::class,
74-
TypeEnum::ENUM => EnumType::class,
75-
TypeEnum::CUSTOM_SCALAR => CustomScalarType::class,
76-
];
77-
62+
protected AwareTypeBaseClassProvider $baseClassProvider;
7863
protected ExpressionConverter $expressionConverter;
7964
protected PhpFile $file;
8065
protected string $namespace;
@@ -83,8 +68,9 @@ class TypeBuilder
8368
protected string $currentField;
8469
protected string $gqlServices = '$'.TypeGenerator::GRAPHQL_SERVICES;
8570

86-
public function __construct(ExpressionConverter $expressionConverter, string $namespace)
71+
public function __construct(AwareTypeBaseClassProvider $baseClassProvider, ExpressionConverter $expressionConverter, string $namespace)
8772
{
73+
$this->baseClassProvider = $baseClassProvider;
8874
$this->expressionConverter = $expressionConverter;
8975
$this->namespace = $namespace;
9076

@@ -120,7 +106,7 @@ public function build(array $config, string $type): PhpFile
120106

121107
$class = $this->file->createClass($config['class_name'])
122108
->setFinal()
123-
->setExtends(static::EXTENDS[$type])
109+
->setExtends($this->baseClassProvider->getFQCN($type))
124110
->addImplements(GeneratedTypeInterface::class, AliasedInterface::class)
125111
->addConst('NAME', $config['name'])
126112
->setDocBlock(static::DOCBLOCK_TEXT);

src/Resources/config/services.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ services:
55
_defaults:
66
autowire: true
77

8+
_instanceof:
9+
Overblog\GraphQLBundle\Generator\TypeBaseClassProvider\TypeBaseClassProviderInterface:
10+
tags: [ 'overblog_graphql.type_base_class.provider' ]
11+
812
Overblog\GraphQLBundle\Executor\Executor: ~
913
Overblog\GraphQLBundle\Request\Parser: ~
1014
Overblog\GraphQLBundle\Request\BatchParser: ~
@@ -106,6 +110,13 @@ services:
106110
arguments:
107111
$namespace: '%overblog_graphql.class_namespace%'
108112

113+
Overblog\GraphQLBundle\Generator\AwareTypeBaseClassProvider:
114+
arguments:
115+
$providers: !tagged_iterator 'overblog_graphql.type_base_class.provider'
116+
117+
Overblog\GraphQLBundle\Generator\TypeBaseClassProvider\:
118+
resource: '../../Generator/TypeBaseClassProvider/*'
119+
109120
Overblog\GraphQLBundle\Validator\InputValidatorFactory:
110121
arguments:
111122
- '@?validator.validator_factory'

0 commit comments

Comments
 (0)