-
-
Notifications
You must be signed in to change notification settings - Fork 576
Expand file tree
/
Copy pathInputObjectType.php
More file actions
211 lines (176 loc) · 6.15 KB
/
InputObjectType.php
File metadata and controls
211 lines (176 loc) · 6.15 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
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
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
use GraphQL\Utils\Utils;
/**
* @phpstan-import-type UnnamedInputObjectFieldConfig from InputObjectField
*
* @phpstan-type EagerFieldConfig InputObjectField|(Type&InputType)|UnnamedInputObjectFieldConfig
* @phpstan-type LazyFieldConfig callable(): EagerFieldConfig
* @phpstan-type FieldConfig EagerFieldConfig|LazyFieldConfig
* @phpstan-type InputObjectConfig array{
* name?: string|null,
* description?: string|null,
* fields: iterable<FieldConfig>|callable(): iterable<FieldConfig>,
* parseValue?: callable(array<string, mixed>): mixed,
* astNode?: InputObjectTypeDefinitionNode|null,
* extensionASTNodes?: array<InputObjectTypeExtensionNode>|null
* }
*/
class InputObjectType extends Type implements InputType, NullableType, NamedType
{
use NamedTypeImplementation;
public ?InputObjectTypeDefinitionNode $astNode;
/** @var array<InputObjectTypeExtensionNode> */
public array $extensionASTNodes;
/** @phpstan-var InputObjectConfig */
public array $config;
/**
* Lazily initialized.
*
* @var array<string, InputObjectField>
*/
private array $fields;
/**
* @throws InvariantViolation
*
* @phpstan-param InputObjectConfig $config
*
* @throws InvariantViolation
*/
public function __construct(array $config)
{
$this->name = $config['name'] ?? $this->inferName();
$this->description = $config['description'] ?? null;
$this->astNode = $config['astNode'] ?? null;
$this->extensionASTNodes = $config['extensionASTNodes'] ?? [];
$this->config = $config;
}
/** @throws InvariantViolation */
public function getField(string $name): InputObjectField
{
$field = $this->findField($name);
if ($field === null) {
throw new InvariantViolation("Field \"{$name}\" is not defined for type \"{$this->name}\"");
}
return $field;
}
/** @throws InvariantViolation */
public function findField(string $name): ?InputObjectField
{
if (! isset($this->fields)) {
$this->initializeFields();
}
return $this->fields[$name] ?? null;
}
/** @throws InvariantViolation */
public function hasField(string $name): bool
{
if (! isset($this->fields)) {
$this->initializeFields();
}
return isset($this->fields[$name]);
}
/**
* @throws InvariantViolation
*
* @return array<string, InputObjectField>
*/
public function getFields(): array
{
if (! isset($this->fields)) {
$this->initializeFields();
}
return $this->fields;
}
/** @throws InvariantViolation */
protected function initializeFields(): void
{
$fields = $this->config['fields'];
if (is_callable($fields)) {
$fields = $fields();
}
$this->fields = [];
foreach ($fields as $nameOrIndex => $field) {
$this->initializeField($nameOrIndex, $field);
}
}
/**
* @param string|int $nameOrIndex
*
* @phpstan-param FieldConfig $field
*
* @throws InvariantViolation
*/
protected function initializeField($nameOrIndex, $field): void
{
if (is_callable($field)) {
$field = $field();
}
assert($field instanceof Type || is_array($field) || $field instanceof InputObjectField);
if ($field instanceof Type) {
$field = ['type' => $field];
}
assert(is_array($field) || $field instanceof InputObjectField); // @phpstan-ignore-line TODO remove when using actual union types
if (is_array($field)) {
$field['name'] ??= $nameOrIndex;
if (! is_string($field['name'])) {
throw new InvariantViolation("{$this->name} fields must be an associative array with field names as keys, an array of arrays with a name attribute, or a callable which returns one of those.");
}
$field = new InputObjectField($field); // @phpstan-ignore-line array type is wrongly inferred
}
assert($field instanceof InputObjectField); // @phpstan-ignore-line TODO remove when using actual union types
$this->fields[$field->name] = $field;
}
/**
* Parses an externally provided value (query variable) to use as an input.
*
* Should throw an exception with a client friendly message on invalid values, @see ClientAware.
*
* @param array<string, mixed> $value
*
* @return mixed
*/
public function parseValue(array $value)
{
if (isset($this->config['parseValue'])) {
return $this->config['parseValue']($value);
}
return $value;
}
/**
* Validates type config and throws if one of type options is invalid.
* Note: this method is shallow, it won't validate object fields and their arguments.
*
* @throws Error
* @throws InvariantViolation
*/
public function assertValid(): void
{
Utils::assertValidName($this->name);
$fields = $this->config['fields'] ?? null; // @phpstan-ignore nullCoalesce.initializedProperty (unnecessary according to types, but can happen during runtime)
if (is_callable($fields)) {
$fields = $fields();
}
if (! is_iterable($fields)) {
$invalidFields = Utils::printSafe($fields);
throw new InvariantViolation("{$this->name} fields must be an iterable or a callable which returns an iterable, got: {$invalidFields}.");
}
$resolvedFields = $this->getFields();
foreach ($resolvedFields as $field) {
$field->assertValid($this);
}
}
public function astNode(): ?InputObjectTypeDefinitionNode
{
return $this->astNode;
}
/** @return array<InputObjectTypeExtensionNode> */
public function extensionASTNodes(): array
{
return $this->extensionASTNodes;
}
}