1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Validator\Rules; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\Error; |
8
|
|
|
use GraphQL\Language\AST\ArgumentNode; |
9
|
|
|
use GraphQL\Language\AST\DirectiveNode; |
10
|
|
|
use GraphQL\Language\AST\FieldNode; |
11
|
|
|
use GraphQL\Language\AST\Node; |
12
|
|
|
use GraphQL\Language\AST\NodeKind; |
13
|
|
|
use GraphQL\Language\AST\NodeList; |
14
|
|
|
use GraphQL\Utils\Utils; |
15
|
|
|
use GraphQL\Validator\ValidationContext; |
16
|
|
|
use function array_map; |
17
|
|
|
use function count; |
18
|
|
|
use function sprintf; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Known argument names |
22
|
|
|
* |
23
|
|
|
* A GraphQL field is only valid if all supplied arguments are defined by |
24
|
|
|
* that field. |
25
|
|
|
*/ |
26
|
|
|
class KnownArgumentNames extends ValidationRule |
27
|
|
|
{ |
28
|
127 |
|
public function getVisitor(ValidationContext $context) |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
|
|
NodeKind::ARGUMENT => static function (ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) { |
32
|
|
|
/** @var NodeList|Node[] $ancestors */ |
33
|
66 |
|
$argDef = $context->getArgument(); |
34
|
66 |
|
if ($argDef !== null) { |
35
|
60 |
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
$argumentOf = $ancestors[count($ancestors) - 1]; |
39
|
7 |
|
if ($argumentOf instanceof FieldNode) { |
40
|
5 |
|
$fieldDef = $context->getFieldDef(); |
41
|
5 |
|
$parentType = $context->getParentType(); |
42
|
5 |
|
if ($fieldDef && $parentType) { |
|
|
|
|
43
|
4 |
|
$context->reportError(new Error( |
44
|
4 |
|
self::unknownArgMessage( |
45
|
4 |
|
$node->name->value, |
46
|
4 |
|
$fieldDef->name, |
47
|
4 |
|
$parentType->name, |
48
|
4 |
|
Utils::suggestionList( |
49
|
4 |
|
$node->name->value, |
50
|
4 |
|
array_map( |
51
|
|
|
static function ($arg) { |
52
|
4 |
|
return $arg->name; |
53
|
4 |
|
}, |
54
|
4 |
|
$fieldDef->args |
55
|
|
|
) |
56
|
|
|
) |
57
|
|
|
), |
58
|
5 |
|
[$node] |
59
|
|
|
)); |
60
|
|
|
} |
61
|
2 |
|
} elseif ($argumentOf instanceof DirectiveNode) { |
62
|
2 |
|
$directive = $context->getDirective(); |
63
|
2 |
|
if ($directive) { |
|
|
|
|
64
|
2 |
|
$context->reportError(new Error( |
65
|
2 |
|
self::unknownDirectiveArgMessage( |
66
|
2 |
|
$node->name->value, |
67
|
2 |
|
$directive->name, |
68
|
2 |
|
Utils::suggestionList( |
69
|
2 |
|
$node->name->value, |
70
|
2 |
|
array_map( |
71
|
|
|
static function ($arg) { |
72
|
2 |
|
return $arg->name; |
73
|
2 |
|
}, |
74
|
2 |
|
$directive->args |
75
|
|
|
) |
76
|
|
|
) |
77
|
|
|
), |
78
|
2 |
|
[$node] |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
} |
82
|
127 |
|
}, |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string[] $suggestedArgs |
88
|
|
|
*/ |
89
|
4 |
|
public static function unknownArgMessage($argName, $fieldName, $typeName, array $suggestedArgs) |
90
|
|
|
{ |
91
|
4 |
|
$message = sprintf('Unknown argument "%s" on field "%s" of type "%s".', $argName, $fieldName, $typeName); |
92
|
4 |
|
if (! empty($suggestedArgs)) { |
93
|
1 |
|
$message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs)); |
94
|
|
|
} |
95
|
|
|
|
96
|
4 |
|
return $message; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string[] $suggestedArgs |
101
|
|
|
*/ |
102
|
2 |
|
public static function unknownDirectiveArgMessage($argName, $directiveName, array $suggestedArgs) |
103
|
|
|
{ |
104
|
2 |
|
$message = sprintf('Unknown argument "%s" on directive "@%s".', $argName, $directiveName); |
105
|
2 |
|
if (! empty($suggestedArgs)) { |
106
|
1 |
|
$message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs)); |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
return $message; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|