-
-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathArgument.php
More file actions
141 lines (115 loc) · 4.03 KB
/
Argument.php
File metadata and controls
141 lines (115 loc) · 4.03 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
<?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 UnnamedArgumentConfig array{
* name?: string,
* type: ArgumentType,
* defaultValue?: mixed,
* description?: string|null,
* deprecationReason?: string|null,
* directives?: array<DirectiveNode>|null,
* astNode?: InputValueDefinitionNode|null
* }
* @phpstan-type ArgumentConfig array{
* name: string,
* type: ArgumentType,
* defaultValue?: mixed,
* description?: string|null,
* deprecationReason?: string|null,
* directives?: array<DirectiveNode>|null,
* astNode?: InputValueDefinitionNode|null
* }
* @phpstan-type ArgumentListConfig iterable<ArgumentConfig|ArgumentType>|iterable<UnnamedArgumentConfig>
*/
class Argument
{
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 ArgumentConfig */
public array $config;
/** @phpstan-param ArgumentConfig $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;
}
/**
* @phpstan-param ArgumentListConfig $config
*
* @return array<int, self>
*/
public static function listFromConfig(iterable $config): array
{
$list = [];
foreach ($config as $name => $argConfig) {
if (! is_array($argConfig)) {
$argConfig = ['type' => $argConfig];
}
/** @phpstan-var ArgumentConfig $argConfigWithName */
$argConfigWithName = $argConfig + ['name' => $name];
$list[] = new self($argConfigWithName);
}
return $list;
}
/** @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(FieldDefinition $parentField, Type $parentType): void
{
$error = Utils::isValidNameError($this->name);
if ($error !== null) {
throw new InvariantViolation("{$parentType->name}.{$parentField->name}({$this->name}:) {$error->getMessage()}");
}
$type = Type::getNamedType($this->getType());
if (! $type instanceof InputType) {
$notInputType = Utils::printSafe($this->type);
throw new InvariantViolation("{$parentType->name}.{$parentField->name}({$this->name}): argument type must be Input Type but got: {$notInputType}");
}
if ($this->isRequired() && $this->isDeprecated()) {
throw new InvariantViolation("Required argument {$parentType->name}.{$parentField->name}({$this->name}:) cannot be deprecated.");
}
}
}