-
-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathInputObjectField.php
More file actions
122 lines (100 loc) · 3.56 KB
/
InputObjectField.php
File metadata and controls
122 lines (100 loc) · 3.56 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
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Type\Schema;
use GraphQL\Utils\Utils;
/**
* @phpstan-type ArgumentType (Type&InputType)|callable(): (Type&InputType)
* @phpstan-type InputObjectFieldConfig array{
* name: string,
* type: ArgumentType,
* defaultValue?: mixed,
* description?: string|null,
* deprecationReason?: string|null,
* directives?: array<DirectiveNode>|null,
* astNode?: InputValueDefinitionNode|null
* }
* @phpstan-type UnnamedInputObjectFieldConfig array{
* name?: string,
* type: ArgumentType,
* defaultValue?: mixed,
* description?: string|null,
* deprecationReason?: string|null,
* directives?: array<DirectiveNode>|null,
* astNode?: InputValueDefinitionNode|null
* }
*/
class InputObjectField
{
public string $name;
/** @var mixed */
public $defaultValue;
public ?string $description;
public ?string $deprecationReason;
/** @var Type&InputType */
private Type $type;
public ?InputValueDefinitionNode $astNode;
/** @var array<DirectiveNode> */
public array $directives;
/** @phpstan-var InputObjectFieldConfig */
public array $config;
/** @phpstan-param InputObjectFieldConfig $config */
public function __construct(array $config)
{
$this->name = $config['name'];
$this->defaultValue = $config['defaultValue'] ?? null;
$this->description = $config['description'] ?? null;
$this->deprecationReason = $config['deprecationReason'] ?? null;
// Do nothing for type, it is lazy loaded in getType()
$this->astNode = $config['astNode'] ?? null;
$this->directives = $config['directives'] ?? [];
$this->config = $config;
}
/** @return Type&InputType */
public function getType(): Type
{
if (! isset($this->type)) {
$this->type = Schema::resolveType($this->config['type']);
}
return $this->type;
}
public function defaultValueExists(): bool
{
return array_key_exists('defaultValue', $this->config);
}
public function isRequired(): bool
{
return $this->getType() instanceof NonNull
&& ! $this->defaultValueExists();
}
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 InputType) {
$notInputType = Utils::printSafe($this->type);
throw new InvariantViolation("{$parentType->name}.{$this->name} field type must be Input Type but got: {$notInputType}");
}
// @phpstan-ignore-next-line should not happen if used properly
if (array_key_exists('resolve', $this->config)) {
throw new InvariantViolation("{$parentType->name}.{$this->name} field has a resolve property, but Input Types cannot define resolvers.");
}
if ($this->isRequired() && $this->isDeprecated()) {
throw new InvariantViolation("Required input field {$parentType->name}.{$this->name} cannot be deprecated.");
}
}
}