/
DataObjectModel.php
430 lines (380 loc) · 11.7 KB
/
DataObjectModel.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
namespace SilverStripe\GraphQL\Schema\DataObject;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\GraphQL\Config\ModelConfiguration;
use SilverStripe\GraphQL\Schema\Field\ModelField;
use SilverStripe\GraphQL\Schema\Field\ModelQuery;
use SilverStripe\GraphQL\Schema\Interfaces\BaseFieldsProvider;
use SilverStripe\GraphQL\Schema\Interfaces\DefaultFieldsProvider;
use SilverStripe\GraphQL\Schema\Interfaces\ExtraTypeProvider;
use SilverStripe\GraphQL\Schema\Interfaces\ModelBlacklist;
use SilverStripe\GraphQL\Schema\Resolver\ResolverReference;
use SilverStripe\GraphQL\Schema\SchemaConfig;
use SilverStripe\GraphQL\Schema\Type\ModelType;
use SilverStripe\GraphQL\Schema\Interfaces\OperationCreator;
use SilverStripe\GraphQL\Schema\Interfaces\OperationProvider;
use SilverStripe\GraphQL\Schema\Schema;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Interfaces\SchemaModelInterface;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\UnsavedRelationList;
/**
* Defines the model that generates types, queries, and mutations based on DataObjects
*/
class DataObjectModel implements
SchemaModelInterface,
OperationProvider,
DefaultFieldsProvider,
BaseFieldsProvider,
ModelBlacklist
{
use Injectable;
use Configurable;
/**
* @var SchemaConfig
*/
private $schemaConfig;
/**
* @var array
*/
private static $dependencies = [
'FieldAccessor' => '%$' . FieldAccessor::class,
];
/**
* @var array
* @config
*/
private $operations = [];
/**
* @var DataObject
*/
private $dataObject;
/**
* @var FieldAccessor
*/
private $fieldAccessor;
/**
* @return string
*/
public static function getIdentifier(): string
{
return 'DataObject';
}
/**
* DataObjectModel constructor.
* @param string $class
* @param SchemaConfig $config
* @throws SchemaBuilderException
*/
public function __construct(string $class, SchemaConfig $config)
{
Schema::invariant(
is_subclass_of($class, DataObject::class),
'%s only accepts %s subclasses',
static::class,
DataObject::class
);
$this->dataObject = Injector::inst()->get($class);
$this->schemaConfig = $config;
}
/**
* @param string $fieldName
* @return bool
*/
public function hasField(string $fieldName): bool
{
return $this->getFieldAccessor()->hasField($this->dataObject, $fieldName);
}
/**
* @param string $fieldName
* @param array $config
* @return ModelField|null
* @throws SchemaBuilderException
*/
public function getField(string $fieldName, array $config = []): ?ModelField
{
$result = $this->getFieldAccessor()->accessField($this->dataObject, $fieldName);
if (!$result) {
return null;
}
$hasExplicitType = isset($config['type']);
if ($result instanceof DBField) {
$fieldConfig = array_merge([
'type' => $result->config()->get('graphql_type'),
], $config);
$modelField = ModelField::create($fieldName, $fieldConfig, $this);
if (!$hasExplicitType) {
$this->applyMetadataClass($modelField, get_class($result));
}
return $modelField;
}
$class = $this->getModelClass($result);
if (!$class) {
if ($this->isList($result)) {
$modelField = ModelField::create($fieldName, $config, $this);
if (!$hasExplicitType) {
$this->applyMetadataClass($modelField, $class);
}
return $modelField;
}
return null;
}
$type = $this->getModelConfiguration()->getTypeName($class);
if ($this->isList($result)) {
$queryConfig = array_merge([
'type' => sprintf('[%s!]!', $type),
], $config);
$query = ModelQuery::create($this, $fieldName, $queryConfig);
$query->setDefaultPlugins($this->getModelConfiguration()->getNestedQueryPlugins());
return $query;
}
return ModelField::create($fieldName, $type, $this);
}
/**
* @return array
* @throws SchemaBuilderException
*/
public function getDefaultFields(): array
{
$fields = $this->getModelConfiguration()->getDefaultFields();
$map = [];
foreach ($fields as $name => $type) {
if ($type === false) {
continue;
}
$formatted = $this->getFieldAccessor()->formatField($name);
$map[$formatted] = $type;
}
return $map;
}
/**
* @return array
* @throws SchemaBuilderException
*/
public function getBaseFields(): array
{
$fields = $this->getModelConfiguration()->getBaseFields();
$map = [];
foreach ($fields as $name => $type) {
Schema::invariant(
$type,
'Default field %s cannot be falsy on %s',
$name,
$this->getSourceClass()
);
$formatted = $this->getFieldAccessor()->formatField($name);
$map[$formatted] = $type;
}
return $map;
}
/**
* @return array
* @throws SchemaBuilderException
*/
public function getBlacklistedFields(): array
{
$class = $this->getSourceClass();
$config = DataObject::singleton($class)->config()->get('graphql_blacklisted_fields') ?? [];
$blackList = [];
Schema::assertValidConfig($config);
foreach ($config as $fieldName => $bool) {
if ($bool === true && is_string($fieldName)) {
$blackList[] = $fieldName;
}
}
return array_map(function (string $field) {
return $this->getFieldAccessor()->formatField($field);
}, $blackList);
}
/**
* @return array
* @throws SchemaBuilderException
*/
public function getAllFields(): array
{
$blackList = $this->getBlacklistedFields();
$allFields = $this->getFieldAccessor()->getAllFields($this->dataObject);
return array_diff($allFields, $blackList);
}
/**
* @return array
* @throws SchemaBuilderException
*/
public function getUninheritedFields(): array
{
$blackList = $this->getBlacklistedFields();
$allFields = $this->getFieldAccessor()->getAllFields($this->dataObject, true, false);
return array_diff($allFields, $blackList);
}
/**
* @param array|null $context
* @return ResolverReference
*/
public function getDefaultResolver(?array $context = []): ResolverReference
{
$callable = empty($context)
? [Resolver::class, 'resolve']
: [Resolver::class, 'resolveContext'];
return ResolverReference::create($callable);
}
/**
* @return string
*/
public function getSourceClass(): string
{
return get_class($this->dataObject);
}
/**
* @return FieldAccessor
*/
public function getFieldAccessor(): FieldAccessor
{
return $this->fieldAccessor;
}
/**
* @param FieldAccessor $fieldAccessor
* @return DataObjectModel
*/
public function setFieldAccessor(FieldAccessor $fieldAccessor): self
{
$this->fieldAccessor = $fieldAccessor;
return $this;
}
/**
* @param string $id
* @return OperationCreator|null
* @throws SchemaBuilderException
*/
public function getOperationCreatorByIdentifier(string $id): ?OperationCreator
{
$registeredOperations = $this->getModelConfiguration()->get('operations', []);
$creator = $registeredOperations[$id]['class'] ?? null;
if (!$creator) {
return null;
}
Schema::invariant(
class_exists($creator),
'Operation creator %s does not exist',
$creator
);
/* @var OperationCreator $obj */
$obj = Injector::inst()->get($creator);
Schema::invariant(
$obj instanceof OperationCreator,
'Operation %s is not an instance of %s',
$creator,
OperationCreator::class
);
return $obj;
}
/**
* @return array
* @throws SchemaBuilderException
*/
public function getAllOperationIdentifiers(): array
{
$registeredOperations = $this->getModelConfiguration()->get('operations', []);
return array_keys($registeredOperations);
}
/**
* Gets a field that resolves to another model, (e.g. an ObjectType from a has_one).
* This method can be used to determine *if* a field is another model, and also to
* get that field.
*
* @param string $fieldName
* @param string $class Optional class name for model fields which would result in database queries.
* The database is not always available when the schema is built (e.g. on deployment servers).
* @return ModelType|null
* @throws SchemaBuilderException
*/
public function getModelTypeForField(string $fieldName, $class = null): ?ModelType
{
if (!$class) {
$result = $this->getFieldAccessor()->accessField($this->dataObject, $fieldName);
$class = $this->getModelClass($result);
}
if (!$class) {
return null;
}
$model = $this->getSchemaConfig()->createModel($class);
if (!$model) {
return null;
}
return ModelType::create($model);
}
/**
* @param string $field
* @return string
*/
public function getPropertyForField(string $field): string
{
$sng = DataObject::singleton($this->getSourceClass());
return FieldAccessor::singleton()->normaliseField($sng, $field) ?: $field;
}
/**
* @return DataObject
*/
public function getDataObject(): DataObject
{
return $this->dataObject;
}
/**
* @return string
* @throws SchemaBuilderException
*/
public function getTypeName(): string
{
return $this->getModelConfiguration()->getTypeName(get_class($this->dataObject));
}
/**
* @return SchemaConfig
*/
public function getSchemaConfig(): SchemaConfig
{
return $this->schemaConfig;
}
/**
* @return ModelConfiguration|null
* @throws SchemaBuilderException
*/
public function getModelConfiguration(): ?ModelConfiguration
{
return $this->getSchemaConfig()->getModelConfiguration(static::getIdentifier());
}
/**
* @param $result
* @return string|null
*/
private function getModelClass($result): ?string
{
if ($result instanceof DataObject) {
return get_class($result);
}
if ($result instanceof SS_List && method_exists($result, 'dataClass')) {
return $result->dataClass();
}
return null;
}
/**
* @param $result
* @return bool
*/
private function isList($result): bool
{
return $result instanceof SS_List || $result instanceof UnsavedRelationList;
}
/**
* @param ModelField $field
* @param string | null $class
* @throws SchemaBuilderException
*/
private function applyMetadataClass(ModelField $field, ?string $class = null): void
{
$field->getMetadata()
->set('dataClass', $class);
}
}