1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers\Types; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use GraphQL\Error\InvariantViolation; |
8
|
|
|
use GraphQL\Type\Definition\FieldDefinition; |
9
|
|
|
use GraphQL\Type\Definition\ObjectType; |
10
|
|
|
use TheCodingMachine\GraphQL\Controllers\Annotations\Type; |
11
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* An object type built from the Type annotation |
15
|
|
|
*/ |
16
|
|
|
class MutableObjectType extends ObjectType |
17
|
|
|
{ |
18
|
|
|
// In pending state, we can still add fields. |
19
|
|
|
public const STATUS_PENDING = 'pending'; |
20
|
|
|
public const STATUS_FROZEN = 'frozen'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $status; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array<callable> |
29
|
|
|
*/ |
30
|
|
|
private $fieldsCallables = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var FieldDefinition[]|null |
34
|
|
|
*/ |
35
|
|
|
private $finalFields; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param object $annotatedObject |
39
|
|
|
* @param RecursiveTypeMapperInterface $recursiveTypeMapper |
40
|
|
|
* @param Type $typeField |
41
|
|
|
*/ |
42
|
|
|
public function __construct(array $config) |
43
|
|
|
{ |
44
|
|
|
$this->status = self::STATUS_PENDING; |
|
|
|
|
45
|
|
|
|
46
|
|
|
parent::__construct($config); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function freeze(): void |
50
|
|
|
{ |
51
|
|
|
$this->status = self::STATUS_FROZEN; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getStatus(): string |
55
|
|
|
{ |
56
|
|
|
return $this->status; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function addFields(callable $fields): void |
60
|
|
|
{ |
61
|
|
|
if ($this->status !== self::STATUS_PENDING) { |
|
|
|
|
62
|
|
|
throw new \RuntimeException('Tried to add fields to a frozen MutableObjectType.'); |
63
|
|
|
} |
64
|
|
|
$this->fieldsCallables[] = $fields; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* |
70
|
|
|
* @return FieldDefinition |
71
|
|
|
* |
72
|
|
|
* @throws Exception |
73
|
|
|
*/ |
74
|
|
|
public function getField($name): FieldDefinition |
75
|
|
|
{ |
76
|
|
|
if ($this->status === self::STATUS_PENDING) { |
|
|
|
|
77
|
|
|
throw new \RuntimeException('You must freeze() a MutableObjectType before fetching its fields.'); |
78
|
|
|
} |
79
|
|
|
return parent::getField($name); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $name |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function hasField($name): bool |
88
|
|
|
{ |
89
|
|
|
if ($this->status === self::STATUS_PENDING) { |
|
|
|
|
90
|
|
|
throw new \RuntimeException('You must freeze() a MutableObjectType before fetching its fields.'); |
91
|
|
|
} |
92
|
|
|
return parent::hasField($name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return FieldDefinition[] |
97
|
|
|
* |
98
|
|
|
* @throws InvariantViolation |
99
|
|
|
*/ |
100
|
|
|
public function getFields(): array |
101
|
|
|
{ |
102
|
|
|
if ($this->finalFields === null) { |
103
|
|
|
if ($this->status === self::STATUS_PENDING) { |
|
|
|
|
104
|
|
|
throw new \RuntimeException('You must freeze() a MutableObjectType before fetching its fields.'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->finalFields = parent::getFields(); |
108
|
|
|
foreach ($this->fieldsCallables as $fieldsCallable) { |
109
|
|
|
$this->finalFields += $fieldsCallable(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->finalFields; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.