forked from webonyx/graphql-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryComplexityTest.php
More file actions
210 lines (157 loc) · 6.63 KB
/
QueryComplexityTest.php
File metadata and controls
210 lines (157 loc) · 6.63 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php declare(strict_types=1);
namespace GraphQL\Tests\Validator;
use GraphQL\Error\Error;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\Parser;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\CustomValidationRule;
use GraphQL\Validator\Rules\QueryComplexity;
use GraphQL\Validator\ValidationContext;
final class QueryComplexityTest extends QuerySecurityTestCase
{
private static QueryComplexity $rule;
public function testSimpleQueries(): void
{
$query = 'query MyQuery { human { firstName } }';
$this->assertDocumentValidators($query, 2, 3);
}
/**
* @throws \Exception
* @throws \GraphQL\Error\SyntaxError
*/
private function assertDocumentValidators(string $query, int $queryComplexity, int $startComplexity): void
{
for ($maxComplexity = $startComplexity; $maxComplexity >= 0; --$maxComplexity) {
$positions = [];
if ($maxComplexity < $queryComplexity && $maxComplexity !== QueryComplexity::DISABLED) {
$positions = [self::createFormattedError($maxComplexity, $queryComplexity)];
}
$this->assertDocumentValidator($query, $maxComplexity, $positions);
}
}
public function testInlineFragmentQueries(): void
{
$query = 'query MyQuery { human { ... on Human { firstName } } }';
$this->assertDocumentValidators($query, 2, 3);
}
public function testTypelessInlineFragmentQueries(): void
{
$query = 'query MyQuery { human { ... { firstName } } }';
$this->assertDocumentValidators($query, 2, 3);
}
public function testFragmentQueries(): void
{
$query = 'query MyQuery { human { ...F1 } } fragment F1 on Human { firstName}';
$this->assertDocumentValidators($query, 2, 3);
}
public function testAliasesQueries(): void
{
$query = 'query MyQuery { thomas: human(name: "Thomas") { firstName } jeremy: human(name: "Jeremy") { firstName } }';
$this->assertDocumentValidators($query, 4, 5);
}
public function testCustomComplexityQueries(): void
{
$query = 'query MyQuery { human { dogs { name } } }';
$this->assertDocumentValidators($query, 12, 13);
}
public function testCustomComplexityWithArgsQueries(): void
{
$query = 'query MyQuery { human { dogs(name: "Root") { name } } }';
$this->assertDocumentValidators($query, 3, 4);
}
public function testCustomComplexityWithVariablesQueries(): void
{
$query = 'query MyQuery($dog: String!) { human { dogs(name: $dog) { name } } }';
$this->getRule()->setRawVariableValues(['dog' => 'Roots']);
$this->assertDocumentValidators($query, 3, 4);
}
/** @throws \InvalidArgumentException */
protected function getRule(int $max = 0): QueryComplexity
{
self::$rule ??= new QueryComplexity($max);
self::$rule->setMaxQueryComplexity($max);
return self::$rule;
}
public function testQueryWithEnabledIncludeDirectives(): void
{
$query = 'query MyQuery($withDogs: Boolean!) { human { dogs(name: "Root") @include(if:$withDogs) { name } } }';
$this->getRule()->setRawVariableValues(['withDogs' => true]);
$this->assertDocumentValidators($query, 3, 4);
}
public function testQueryWithDisabledIncludeDirectives(): void
{
$query = 'query MyQuery($withDogs: Boolean!) { human { dogs(name: "Root") @include(if:$withDogs) { name } } }';
$this->getRule()->setRawVariableValues(['withDogs' => false]);
$this->assertDocumentValidators($query, 1, 2);
}
public function testQueryWithEnabledSkipDirectives(): void
{
$query = 'query MyQuery($withoutDogs: Boolean!) { human { dogs(name: "Root") @skip(if:$withoutDogs) { name } } }';
$this->getRule()->setRawVariableValues(['withoutDogs' => true]);
$this->assertDocumentValidators($query, 1, 2);
}
public function testQueryWithDisabledSkipDirectives(): void
{
$query = 'query MyQuery($withoutDogs: Boolean!) { human { dogs(name: "Root") @skip(if:$withoutDogs) { name } } }';
$this->getRule()->setRawVariableValues(['withoutDogs' => false]);
$this->assertDocumentValidators($query, 3, 4);
}
public function testQueryWithMultipleDirectives(): void
{
$query = 'query MyQuery($withDogs: Boolean!, $withoutDogName: Boolean!) { human { dogs(name: "Root") @include(if:$withDogs) { name @skip(if:$withoutDogName) } } }';
$this->getRule()->setRawVariableValues([
'withDogs' => true,
'withoutDogName' => true,
]);
$this->assertDocumentValidators($query, 2, 3);
}
public function testQueryWithCustomDirective(): void
{
$query = 'query MyQuery { human { ... on Human { firstName @foo(bar: false) } } }';
$this->assertDocumentValidators($query, 2, 3);
}
public function testQueryWithCustomAndSkipDirective(): void
{
$query = 'query MyQuery($withoutDogs: Boolean!) { human { dogs(name: "Root") @skip(if:$withoutDogs) { name @foo(bar: true) } } }';
$this->getRule()->setRawVariableValues(['withoutDogs' => true]);
$this->assertDocumentValidators($query, 1, 2);
}
public function testComplexityIntrospectionQuery(): void
{
$this->assertIntrospectionQuery(188);
}
public function testIntrospectionTypeMetaFieldQuery(): void
{
$this->assertIntrospectionTypeMetaFieldQuery(2);
}
public function testTypeNameMetaFieldQuery(): void
{
$this->assertTypeNameMetaFieldQuery(3);
}
public function testSkippedWhenThereAreOtherValidationErrors(): void
{
$query = 'query MyQuery { human(name: INVALID_VALUE) { dogs {name} } }';
$reportedError = new Error('OtherValidatorError');
$otherRule = new CustomValidationRule(
'otherRule',
static fn (ValidationContext $context): array => [
NodeKind::OPERATION_DEFINITION => [
'leave' => static function () use ($context, $reportedError): void {
$context->reportError($reportedError);
},
],
]
);
$errors = DocumentValidator::validate(
QuerySecuritySchema::buildSchema(),
Parser::parse($query),
[$otherRule, $this->getRule(1)]
);
self::assertCount(1, $errors);
self::assertSame($reportedError, $errors[0]);
}
protected static function getErrorMessage(int $max, int $count): string
{
return QueryComplexity::maxQueryComplexityErrorMessage($max, $count);
}
}