-
-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathFieldDefinition.php
More file actions
258 lines (222 loc) · 8 KB
/
FieldDefinition.php
File metadata and controls
258 lines (222 loc) · 8 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
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
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Executor;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Type\Schema;
use GraphQL\Utils\Utils;
/**
* @see Executor
*
* @phpstan-import-type FieldResolver from Executor
* @phpstan-import-type ArgsMapper from Executor
* @phpstan-import-type ArgumentListConfig from Argument
*
* @phpstan-type FieldType (Type&OutputType)|callable(): (Type&OutputType)
* @phpstan-type ComplexityFn callable(int, array<string, mixed>): int
* @phpstan-type VisibilityFn callable(): bool
* @phpstan-type FieldDefinitionConfig array{
* name: string,
* type: FieldType,
* resolve?: FieldResolver|null,
* args?: ArgumentListConfig|null,
* argsMapper?: ArgsMapper|null,
* description?: string|null,
* visible?: VisibilityFn|bool,
* deprecationReason?: string|null,
* directives?: array<DirectiveNode>|null,
* astNode?: FieldDefinitionNode|null,
* complexity?: ComplexityFn|null
* }
* @phpstan-type UnnamedFieldDefinitionConfig array{
* type: FieldType,
* resolve?: FieldResolver|null,
* args?: ArgumentListConfig|null,
* argsMapper?: ArgsMapper|null,
* description?: string|null,
* visible?: VisibilityFn|bool,
* deprecationReason?: string|null,
* directives?: array<DirectiveNode>|null,
* astNode?: FieldDefinitionNode|null,
* complexity?: ComplexityFn|null
* }
* @phpstan-type FieldsConfig iterable<mixed>|callable(): iterable<mixed>
*/
/*
* TODO check if newer versions of PHPStan can handle the full definition, it currently crashes when it is used
* @phpstan-type EagerListEntry FieldDefinitionConfig|(Type&OutputType)
* @phpstan-type EagerMapEntry UnnamedFieldDefinitionConfig|FieldDefinition
* @phpstan-type FieldsList iterable<EagerListEntry|(callable(): EagerListEntry)>
* @phpstan-type FieldsMap iterable<string, EagerMapEntry|(callable(): EagerMapEntry)>
* @phpstan-type FieldsIterable FieldsList|FieldsMap
* @phpstan-type FieldsConfig FieldsIterable|(callable(): FieldsIterable)
*/
class FieldDefinition
{
public string $name;
/** @var array<int, Argument> */
public array $args;
/**
* Callback to transform args to value object.
*
* @var callable|null
*
* @phpstan-var ArgsMapper|null
*/
public $argsMapper;
/**
* Callback for resolving field value given parent value.
*
* @var callable|null
*
* @phpstan-var FieldResolver|null
*/
public $resolveFn;
public ?string $description;
/**
* @var callable|bool
*
* @phpstan-var VisibilityFn|bool
*/
public $visible;
public ?string $deprecationReason;
public ?FieldDefinitionNode $astNode;
/** @var array<DirectiveNode> */
public array $directives;
/**
* @var callable|null
*
* @phpstan-var ComplexityFn|null
*/
public $complexityFn;
/**
* Original field definition config.
*
* @phpstan-var FieldDefinitionConfig
*/
public array $config;
/** @var Type&OutputType */
private Type $type;
/** @param FieldDefinitionConfig $config */
public function __construct(array $config)
{
$this->name = $config['name'];
$this->resolveFn = $config['resolve'] ?? null;
$this->args = isset($config['args'])
? Argument::listFromConfig($config['args'])
: [];
$this->argsMapper = $config['argsMapper'] ?? null;
$this->description = $config['description'] ?? null;
$this->visible = $config['visible'] ?? true;
$this->deprecationReason = $config['deprecationReason'] ?? null;
$this->astNode = $config['astNode'] ?? null;
$this->directives = $config['directives'] ?? [];
$this->complexityFn = $config['complexity'] ?? null;
$this->config = $config;
}
/**
* @param ObjectType|InterfaceType $parentType
* @param callable|iterable $fields
*
* @phpstan-param FieldsConfig $fields
*
* @throws InvariantViolation
*
* @return array<string, self|UnresolvedFieldDefinition>
*/
public static function defineFieldMap(Type $parentType, $fields): array
{
if (is_callable($fields)) {
$fields = $fields();
}
if (! is_iterable($fields)) {
throw new InvariantViolation("{$parentType->name} fields must be an iterable or a callable which returns such an iterable.");
}
$map = [];
foreach ($fields as $maybeName => $field) {
if (is_array($field)) {
if (! isset($field['name'])) {
if (! is_string($maybeName)) {
throw new InvariantViolation("{$parentType->name} fields must be an associative array with field names as keys or a function which returns such an array.");
}
$field['name'] = $maybeName;
}
// @phpstan-ignore-next-line PHPStan won't let us define the whole type
$fieldDef = new self($field);
} elseif ($field instanceof self) {
$fieldDef = $field;
} elseif (is_callable($field)) {
if (! is_string($maybeName)) {
throw new InvariantViolation("{$parentType->name} lazy fields must be an associative array with field names as keys.");
}
$fieldDef = new UnresolvedFieldDefinition($maybeName, $field);
} elseif ($field instanceof Type) {
// @phpstan-ignore-next-line PHPStan won't let us define the whole type
$fieldDef = new self([
'name' => $maybeName,
'type' => $field,
]);
} else {
$invalidFieldConfig = Utils::printSafe($field);
throw new InvariantViolation("{$parentType->name}.{$maybeName} field config must be an array, but got: {$invalidFieldConfig}");
}
$map[$fieldDef->getName()] = $fieldDef;
}
return $map;
}
public function getArg(string $name): ?Argument
{
foreach ($this->args as $arg) {
if ($arg->name === $name) {
return $arg;
}
}
return null;
}
public function getName(): string
{
return $this->name;
}
/** @return Type&OutputType */
public function getType(): Type
{
return $this->type ??= Schema::resolveType($this->config['type']);
}
public function isVisible(): bool
{
if (is_bool($this->visible)) {
return $this->visible;
}
return $this->visible = ($this->visible)();
}
public function isDeprecated(): bool
{
return (bool) $this->deprecationReason;
}
/**
* @param Type&NamedType $parentType
*
* @throws InvariantViolation
*/
public function assertValid(Type $parentType): void
{
$error = Utils::isValidNameError($this->name);
if ($error !== null) {
throw new InvariantViolation("{$parentType->name}.{$this->name}: {$error->getMessage()}");
}
$type = Type::getNamedType($this->getType());
if (! $type instanceof OutputType) {
$safeType = Utils::printSafe($this->type);
throw new InvariantViolation("{$parentType->name}.{$this->name} field type must be Output Type but got: {$safeType}.");
}
// @phpstan-ignore-next-line unnecessary according to types, but can happen during runtime
if ($this->resolveFn !== null && ! is_callable($this->resolveFn)) {
$safeResolveFn = Utils::printSafe($this->resolveFn);
throw new InvariantViolation("{$parentType->name}.{$this->name} field resolver must be a function if provided, but got: {$safeResolveFn}.");
}
foreach ($this->args as $fieldArgument) {
$fieldArgument->assertValid($this, $type);
}
}
}