-
-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathTypeWithFields.php
More file actions
80 lines (59 loc) · 1.76 KB
/
TypeWithFields.php
File metadata and controls
80 lines (59 loc) · 1.76 KB
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
<?php
declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Utils\Utils;
use function array_keys;
abstract class TypeWithFields extends Type implements HasFieldsType
{
/**
* Lazily initialized.
*
* @var array<string, FieldDefinition>
*/
protected array $fields;
protected function initializeFields(): void
{
if (isset($this->fields)) {
return;
}
$fields = $this->config['fields'] ?? [];
$this->fields = FieldDefinition::defineFieldMap($this, $fields);
}
public function getField(string $name): FieldDefinition
{
Utils::invariant($this->hasField($name), 'Field "%s" is not defined for type "%s"', $name, $this->name);
return $this->findField($name);
}
public function findField(string $name): ?FieldDefinition
{
$this->initializeFields();
if (! isset($this->fields[$name])) {
return null;
}
if ($this->fields[$name] instanceof UnresolvedFieldDefinition) {
$this->fields[$name] = $this->fields[$name]->resolve();
}
return $this->fields[$name];
}
public function hasField(string $name): bool
{
$this->initializeFields();
return isset($this->fields[$name]);
}
public function getFields(): array
{
$this->initializeFields();
foreach ($this->fields as $name => $field) {
if (! ($field instanceof UnresolvedFieldDefinition)) {
continue;
}
$this->fields[$name] = $field->resolve();
}
return $this->fields;
}
public function getFieldNames(): array
{
$this->initializeFields();
return array_keys($this->fields);
}
}