|
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 string |
|
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
|
|
|
public function __construct(array $config) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->status = self::STATUS_PENDING; |
|
40
|
|
|
|
|
41
|
|
|
parent::__construct($config); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function freeze(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->status = self::STATUS_FROZEN; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getStatus(): string |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->status; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function addFields(callable $fields): void |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->status !== self::STATUS_PENDING) { |
|
57
|
|
|
throw new \RuntimeException('Tried to add fields to a frozen MutableObjectType.'); |
|
58
|
|
|
} |
|
59
|
|
|
$this->fieldsCallables[] = $fields; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* |
|
65
|
|
|
* @return FieldDefinition |
|
66
|
|
|
* |
|
67
|
|
|
* @throws Exception |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getField($name): FieldDefinition |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->status === self::STATUS_PENDING) { |
|
72
|
|
|
throw new \RuntimeException('You must freeze() a MutableObjectType before fetching its fields.'); |
|
73
|
|
|
} |
|
74
|
|
|
return parent::getField($name); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $name |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function hasField($name): bool |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->status === self::STATUS_PENDING) { |
|
85
|
|
|
throw new \RuntimeException('You must freeze() a MutableObjectType before fetching its fields.'); |
|
86
|
|
|
} |
|
87
|
|
|
return parent::hasField($name); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return FieldDefinition[] |
|
92
|
|
|
* |
|
93
|
|
|
* @throws InvariantViolation |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getFields(): array |
|
96
|
|
|
{ |
|
97
|
|
|
if ($this->finalFields === null) { |
|
98
|
|
|
if ($this->status === self::STATUS_PENDING) { |
|
99
|
|
|
throw new \RuntimeException('You must freeze() a MutableObjectType before fetching its fields.'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->finalFields = parent::getFields(); |
|
103
|
|
|
foreach ($this->fieldsCallables as $fieldsCallable) { |
|
104
|
|
|
$this->finalFields += $fieldsCallable(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->finalFields; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|