/
ModelField.php
200 lines (172 loc) · 4.95 KB
/
ModelField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace SilverStripe\GraphQL\Schema\Field;
use SilverStripe\GraphQL\Config\Configuration;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Interfaces\FieldPlugin;
use SilverStripe\GraphQL\Schema\Interfaces\ModelFieldPlugin;
use SilverStripe\GraphQL\Schema\Interfaces\SchemaModelInterface;
use SilverStripe\GraphQL\Schema\Schema;
use SilverStripe\GraphQL\Schema\Type\ModelType;
/**
* A field that appears on model type
*/
class ModelField extends Field
{
use ModelAware;
/**
* @var array|null
*/
private $modelTypeFields = null;
/**
* @var string|null
*/
private $resolvedModelClass = null;
/**
* @var string
*/
private $property;
/**
* @var Configuration
*/
private $metadata;
/**
* ModelField constructor.
* @param string $name
* @param $config
* @param SchemaModelInterface $model The model containing this field (different from the model this field might resolve to)
* @throws SchemaBuilderException
*/
public function __construct(string $name, $config, SchemaModelInterface $model)
{
$this->metadata = new Configuration();
$this->setModel($model);
Schema::invariant(
is_array($config) || is_string($config) || $config === true,
'Config for field %s must be a string representing a type, a map of config or a value of true
to for type introspection from the model.',
$name
);
if (is_string($config)) {
$config = ['type' => $config];
}
if (!is_array($config)) {
$config = [];
}
parent::__construct($name, $config);
}
public function applyConfig(array $config)
{
$type = $config['type'] ?? null;
if ($type) {
$this->setType($type);
}
if (isset($config['property'])) {
$this->setProperty($config['property']);
}
$resolver = $config['resolver'] ?? null;
if ($resolver) {
$this->setResolver($resolver);
} else {
$this->setResolver($this->getModel()->getDefaultResolver($this->getResolverContext()));
}
if (isset($config['resolvedModelClass'])) {
$this->resolvedModelClass = $config['resolvedModelClass'];
}
$this->modelTypeFields = $config['fields'] ?? null;
unset($config['fields']);
unset($config['operations']);
unset($config['property']);
parent::applyConfig($config);
}
/**
* For nested field definitions
* Blog:
* fields:
* Comments:
* fields:
* Author:
* fields:
* Name: String
* @return ModelType|null
* @throws SchemaBuilderException
*/
public function getModelType(): ?ModelType
{
$type = $this->getNamedType();
if (Schema::isInternalType($type)) {
return null;
}
$model = $this->getModel()->getModelTypeForField($this->getName(), $this->resolvedModelClass);
if ($model) {
$config = [];
if ($this->modelTypeFields) {
$config['fields'] = $this->modelTypeFields;
}
$model->applyConfig($config);
}
return $model;
}
/**
* @return string|null
*/
public function getProperty(): ?string
{
return $this->property;
}
/**
* @param string|null $property
* @return ModelField
*/
public function setProperty(?string $property): self
{
$this->property = $property;
return $this;
}
/**
* @return string
*/
public function getPropertyName(): string
{
return $this->getProperty() ?: $this->getModel()->getPropertyForField($this->getName());
}
/**
* @return Configuration
*/
public function getMetadata(): Configuration
{
return $this->metadata;
}
public function mergeWith(Field $field): Field
{
if ($field->getProperty()) {
$this->setProperty($field->getProperty());
}
return parent::mergeWith($field);
}
/**
* @return string
* @throws SchemaBuilderException
*/
public function getSignature(): string
{
$parentSignature = parent::getSignature();
if (!$this->getProperty()) {
return $parentSignature;
}
return md5($parentSignature . $this->getProperty());
}
/**
* @param string $pluginName
* @param $plugin
* @throws SchemaBuilderException
*/
public function validatePlugin(string $pluginName, $plugin): void
{
Schema::invariant(
$plugin && ($plugin instanceof ModelFieldPlugin || $plugin instanceof FieldPlugin),
'Plugin %s not found or does not apply to field "%s"',
$pluginName,
$this->getName()
);
}
}