forked from webonyx/graphql-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirective.php
More file actions
179 lines (158 loc) · 5.7 KB
/
Directive.php
File metadata and controls
179 lines (158 loc) · 5.7 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
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation;
use GraphQL\Language\AST\DirectiveDefinitionNode;
use GraphQL\Language\DirectiveLocation;
/**
* @phpstan-import-type ArgumentListConfig from Argument
*
* @phpstan-type DirectiveConfig array{
* name: string,
* description?: string|null,
* args?: ArgumentListConfig|null,
* locations: array<string>,
* isRepeatable?: bool|null,
* astNode?: DirectiveDefinitionNode|null
* }
*/
class Directive
{
public const DEFAULT_DEPRECATION_REASON = 'No longer supported';
public const INCLUDE_NAME = 'include';
public const IF_ARGUMENT_NAME = 'if';
public const SKIP_NAME = 'skip';
public const DEPRECATED_NAME = 'deprecated';
public const REASON_ARGUMENT_NAME = 'reason';
public const ONE_OF_NAME = 'oneOf';
/**
* Lazily initialized.
*
* @var array<string, Directive>|null
*/
protected static ?array $internalDirectives = null;
public string $name;
public ?string $description;
/** @var array<int, Argument> */
public array $args;
public bool $isRepeatable;
/** @var array<string> */
public array $locations;
public ?DirectiveDefinitionNode $astNode;
/**
* @var array<string, mixed>
*
* @phpstan-var DirectiveConfig
*/
public array $config;
/**
* @param array<string, mixed> $config
*
* @phpstan-param DirectiveConfig $config
*/
public function __construct(array $config)
{
$this->name = $config['name'];
$this->description = $config['description'] ?? null;
$this->args = isset($config['args'])
? Argument::listFromConfig($config['args'])
: [];
$this->isRepeatable = $config['isRepeatable'] ?? false;
$this->locations = $config['locations'];
$this->astNode = $config['astNode'] ?? null;
$this->config = $config;
}
/**
* @throws InvariantViolation
*
* @return array<string, Directive>
*/
public static function getInternalDirectives(): array
{
return [
self::INCLUDE_NAME => self::includeDirective(),
self::SKIP_NAME => self::skipDirective(),
self::DEPRECATED_NAME => self::deprecatedDirective(),
self::ONE_OF_NAME => self::oneOfDirective(),
];
}
/** @throws InvariantViolation */
public static function includeDirective(): Directive
{
return self::$internalDirectives[self::INCLUDE_NAME] ??= new self([
'name' => self::INCLUDE_NAME,
'description' => 'Directs the executor to include this field or fragment only when the `if` argument is true.',
'locations' => [
DirectiveLocation::FIELD,
DirectiveLocation::FRAGMENT_SPREAD,
DirectiveLocation::INLINE_FRAGMENT,
],
'args' => [
self::IF_ARGUMENT_NAME => [
'type' => Type::nonNull(Type::boolean()),
'description' => 'Included when true.',
],
],
]);
}
/** @throws InvariantViolation */
public static function skipDirective(): Directive
{
return self::$internalDirectives[self::SKIP_NAME] ??= new self([
'name' => self::SKIP_NAME,
'description' => 'Directs the executor to skip this field or fragment when the `if` argument is true.',
'locations' => [
DirectiveLocation::FIELD,
DirectiveLocation::FRAGMENT_SPREAD,
DirectiveLocation::INLINE_FRAGMENT,
],
'args' => [
self::IF_ARGUMENT_NAME => [
'type' => Type::nonNull(Type::boolean()),
'description' => 'Skipped when true.',
],
],
]);
}
/** @throws InvariantViolation */
public static function deprecatedDirective(): Directive
{
return self::$internalDirectives[self::DEPRECATED_NAME] ??= new self([
'name' => self::DEPRECATED_NAME,
'description' => 'Marks an element of a GraphQL schema as no longer supported.',
'locations' => [
DirectiveLocation::FIELD_DEFINITION,
DirectiveLocation::ENUM_VALUE,
DirectiveLocation::ARGUMENT_DEFINITION,
DirectiveLocation::INPUT_FIELD_DEFINITION,
],
'args' => [
self::REASON_ARGUMENT_NAME => [
'type' => Type::string(),
'description' => 'Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/).',
'defaultValue' => self::DEFAULT_DEPRECATION_REASON,
],
],
]);
}
/** @throws InvariantViolation */
public static function oneOfDirective(): Directive
{
return self::$internalDirectives[self::ONE_OF_NAME] ??= new self([
'name' => self::ONE_OF_NAME,
'description' => 'Indicates that an input object is a oneof input object and exactly one of the input fields must be specified.',
'locations' => [
DirectiveLocation::INPUT_OBJECT,
],
'args' => [],
]);
}
/** @throws InvariantViolation */
public static function isSpecifiedDirective(Directive $directive): bool
{
return array_key_exists($directive->name, self::getInternalDirectives());
}
public static function resetCachedInstances(): void
{
self::$internalDirectives = null;
}
}