1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Validation\Rule; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Type\Definition\TypeInterface; |
6
|
|
|
use function Digia\GraphQL\Util\quotedOrList; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @param string $definitionName |
10
|
|
|
* @return string |
11
|
|
|
*/ |
12
|
|
|
function nonExecutableDefinitionMessage(string $definitionName): string |
13
|
|
|
{ |
14
|
|
|
return sprintf('The %s definition is not executable.', $definitionName); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $fieldName |
19
|
|
|
* @param string $type |
20
|
|
|
* @param array $suggestedTypeNames |
21
|
|
|
* @param array $suggestedFieldNames |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
function undefinedFieldMessage( |
25
|
|
|
string $fieldName, |
26
|
|
|
string $type, |
27
|
|
|
array $suggestedTypeNames, |
28
|
|
|
array $suggestedFieldNames |
29
|
|
|
): string { |
30
|
|
|
$message = sprintf('Cannot query field "%s" on type "%s".', $fieldName, $type); |
31
|
|
|
if (!empty($suggestedTypeNames)) { |
32
|
|
|
return $message . ' ' . sprintf( |
33
|
|
|
'Did you mean to use an inline fragment on %s?', |
34
|
|
|
quotedOrList($suggestedTypeNames) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
if (!empty($suggestedFieldNames)) { |
38
|
|
|
return $message . ' ' . sprintf('Did you mean %s?', quotedOrList($suggestedFieldNames)); |
39
|
|
|
} |
40
|
|
|
return $message; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param TypeInterface $type |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
function inlineFragmentOnNonCompositeMessage(string $typeName): string |
48
|
|
|
{ |
49
|
|
|
return sprintf('Fragment cannot condition on non composite type "%s".', $typeName); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $fragmentName |
54
|
|
|
* @param string $typeName |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
function fragmentOnNonCompositeMessage(string $fragmentName, string $typeName): string |
58
|
|
|
{ |
59
|
|
|
return sprintf('Fragment "%s" cannot condition on non composite type "%s".', $fragmentName, $typeName); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $argumentName |
64
|
|
|
* @param string $fieldName |
65
|
|
|
* @param string $typeName |
66
|
|
|
* @param array $suggestedArguments |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
function unknownArgumentMessage( |
70
|
|
|
string $argumentName, |
71
|
|
|
string $fieldName, |
72
|
|
|
string $typeName, |
73
|
|
|
array $suggestedArguments |
74
|
|
|
): string { |
75
|
|
|
$message = sprintf('Unknown argument "%s" on field "%s" of type "%s".', $argumentName, $fieldName, $typeName); |
76
|
|
|
if (!empty($suggestedArguments)) { |
77
|
|
|
return $message . ' ' . sprintf('Did you mean %s', quotedOrList($suggestedArguments)); |
78
|
|
|
} |
79
|
|
|
return $message; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $argumentName |
84
|
|
|
* @param string $directiveName |
85
|
|
|
* @param array $suggestedArguments |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
function unknownDirectiveArgumentMessage(string $argumentName, string $directiveName, array $suggestedArguments): string |
89
|
|
|
{ |
90
|
|
|
$message = sprintf('Unknown argument "%s" on directive "@%s".', $argumentName, $directiveName); |
91
|
|
|
if (!empty($suggestedArguments)) { |
92
|
|
|
return $message . ' ' . sprintf('Did you mean %s', quotedOrList($suggestedArguments)); |
93
|
|
|
} |
94
|
|
|
return $message; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $directiveName |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
function unknownDirectiveMessage(string $directiveName): string |
102
|
|
|
{ |
103
|
|
|
return sprintf('Unknown directive "%s".', $directiveName); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $directiveName |
108
|
|
|
* @param string $location |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
function misplacedDirectiveMessage(string $directiveName, string $location): string |
112
|
|
|
{ |
113
|
|
|
return sprintf('Directive "%s" may not be used on %s.', $directiveName, $location); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $fragmentName |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
function unknownFragmentMessage(string $fragmentName): string |
121
|
|
|
{ |
122
|
|
|
return sprintf('Unknown fragment "%s".', $fragmentName); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $typeName |
127
|
|
|
* @param array $suggestedTypes |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
function unknownTypeMessage(string $typeName, array $suggestedTypes): string |
131
|
|
|
{ |
132
|
|
|
$message = sprintf('Unknown type "%s".', $typeName); |
133
|
|
|
if (!empty($suggestedTypes)) { |
134
|
|
|
return $message . ' ' . sprintf('Did you mean %s?', quotedOrList($suggestedTypes)); |
135
|
|
|
} |
136
|
|
|
return $message; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
function anonymousOperationNotAloneMessage(): string |
143
|
|
|
{ |
144
|
|
|
return 'This anonymous operation must be the only defined operation.'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $fragmentName |
149
|
|
|
* @param array $spreadNames |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
function fragmentCycleMessage(string $fragmentName, array $spreadNames): string |
153
|
|
|
{ |
154
|
|
|
$via = !empty($spreadNames) ? ' via ' . implode(', ', $spreadNames) : ''; |
155
|
|
|
return sprintf('Cannot spread fragment "%s" within itself%s.', $fragmentName, $via); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $variableName |
160
|
|
|
* @param null|string $operationName |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
function undefinedVariableMessage(string $variableName, ?string $operationName = null): string |
164
|
|
|
{ |
165
|
|
|
/** @noinspection IsEmptyFunctionUsageInspection */ |
166
|
|
|
return !empty($operationName) |
167
|
|
|
? sprintf('Variable "$%s" is not defined by operation "%s".', $variableName, $operationName) |
168
|
|
|
: sprintf('Variable "$%s" is not defined.', $variableName); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $fragmentName |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
function unusedFragmentMessage(string $fragmentName): string |
176
|
|
|
{ |
177
|
|
|
return sprintf('Fragment "%s" is never used.', $fragmentName); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $variableName |
182
|
|
|
* @param null|string $operationName |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
function unusedVariableMessage(string $variableName, ?string $operationName = null): string |
186
|
|
|
{ |
187
|
|
|
/** @noinspection IsEmptyFunctionUsageInspection */ |
188
|
|
|
return !empty($operationName) |
189
|
|
|
? sprintf('Variable "$%s" is never used in operation "%s".', $variableName, $operationName) |
190
|
|
|
: sprintf('Variable "$%s" is never used.', $variableName); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $reasonName |
195
|
|
|
* @param mixed $reason |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
function fieldsConflictMessage(string $responseName, $reason): string |
199
|
|
|
{ |
200
|
|
|
return sprintf( |
201
|
|
|
'Fields "%s" conflict because %s. Use different aliases on the fields to fetch both if this was intentional.', |
202
|
|
|
$responseName, |
203
|
|
|
conflictReasonMessage($reason) |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param array|string $reason |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
function conflictReasonMessage($reason): string |
212
|
|
|
{ |
213
|
|
|
if (\is_array($reason)) { |
214
|
|
|
return implode(' and ', array_map(function ($subreason) { |
215
|
|
|
[$fieldName, $message] = $subreason; |
216
|
|
|
return sprintf('subfields "%s" conflict because %s', $fieldName, conflictReasonMessage($message)); |
217
|
|
|
}, $reason)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $reason; |
221
|
|
|
} |
222
|
|
|
|