|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Type\Definition; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\InvariantException; |
|
6
|
|
|
use function Digia\GraphQL\Type\isAssocArray; |
|
7
|
|
|
use function Digia\GraphQL\Type\resolveThunk; |
|
8
|
|
|
use function Digia\GraphQL\Util\invariant; |
|
9
|
|
|
use function Digia\GraphQL\Util\toString; |
|
10
|
|
|
|
|
11
|
|
|
trait FieldsTrait |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array|callable |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $fieldsOrThunk; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Field[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $fieldMap; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return Field[] |
|
25
|
|
|
* @throws InvariantException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getFields(): array |
|
28
|
|
|
{ |
|
29
|
|
|
// Fields are built lazily to avoid concurrency issues. |
|
30
|
|
|
if (!isset($this->fieldMap)) { |
|
31
|
|
|
$this->fieldMap = $this->buildFieldMap($this->fieldsOrThunk ?? []); |
|
32
|
|
|
} |
|
33
|
|
|
return $this->fieldMap; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Fields are created using the `ConfigAwareTrait` constructor which will automatically |
|
38
|
|
|
* call this method when setting arguments from `$config['fields']`. |
|
39
|
|
|
* |
|
40
|
|
|
* @param array|callable $fieldsOrThunk |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function setFields($fieldsOrThunk) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->fieldsOrThunk = $fieldsOrThunk; |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param mixed $fieldsOrThunk |
|
51
|
|
|
* @return array |
|
52
|
|
|
* @throws InvariantException |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function buildFieldMap($fieldsOrThunk): array |
|
55
|
|
|
{ |
|
56
|
|
|
$fields = resolveThunk($fieldsOrThunk); |
|
57
|
|
|
|
|
58
|
|
|
invariant( |
|
59
|
|
|
isAssocArray($fields), |
|
60
|
|
|
\sprintf( |
|
61
|
|
|
'%s fields must be an associative array with field names as key or a callable which returns such an array.', |
|
62
|
|
|
$this->getName() |
|
|
|
|
|
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$fieldMap = []; |
|
67
|
|
|
|
|
68
|
|
|
foreach ($fields as $fieldName => $fieldConfig) { |
|
69
|
|
|
invariant( |
|
70
|
|
|
\is_array($fieldConfig), |
|
71
|
|
|
\sprintf('%s.%s field config must be an array', $this->getName(), $fieldName) |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
invariant( |
|
75
|
|
|
!isset($fieldConfig['isDeprecated']), |
|
76
|
|
|
\sprintf( |
|
77
|
|
|
'%s.%s should provide "deprecationReason" instead of "isDeprecated".', |
|
78
|
|
|
$this->getName(), |
|
79
|
|
|
$fieldName |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
if (isset($fieldConfig['resolve'])) { |
|
84
|
|
|
invariant( |
|
85
|
|
|
null === $fieldConfig['resolve'] || \is_callable($fieldConfig['resolve']), |
|
86
|
|
|
\sprintf( |
|
87
|
|
|
'%s.%s field resolver must be a function if provided, but got: %s', |
|
88
|
|
|
$this->getName(), |
|
89
|
|
|
$fieldName, |
|
90
|
|
|
toString($fieldConfig['resolve']) |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$fieldMap[$fieldName] = new Field(\array_merge($fieldConfig, ['name' => $fieldName])); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $fieldMap; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|