@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
112 | - * @param Query|Field $query |
|
112 | + * @param Query $query |
|
113 | 113 | * @param AbstractObjectType $currentLevelSchema |
114 | 114 | * @return array|bool|mixed |
115 | 115 | */ |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | } |
312 | 312 | |
313 | 313 | /** |
314 | - * @param $query Query|FragmentInterface |
|
314 | + * @param Query $query Query|FragmentInterface |
|
315 | 315 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
316 | 316 | * @param $resolvedValue mixed |
317 | 317 | * @param $value array |
@@ -414,6 +414,9 @@ discard block |
||
414 | 414 | return $result; |
415 | 415 | } |
416 | 416 | |
417 | + /** |
|
418 | + * @param integer $max |
|
419 | + */ |
|
417 | 420 | public function setMaxComplexity($max) { |
418 | 421 | $this->maxComplexity = $max; |
419 | 422 | } |
@@ -432,7 +435,7 @@ discard block |
||
432 | 435 | } |
433 | 436 | |
434 | 437 | /** |
435 | - * @param Query|Field $query |
|
438 | + * @param Query $query |
|
436 | 439 | * @param AbstractObjectType $currentLevelSchema |
437 | 440 | * @param AbstractQueryVisitor $reducer |
438 | 441 | */ |
@@ -7,17 +7,17 @@ |
||
7 | 7 | |
8 | 8 | abstract class AbstractQueryVisitor { |
9 | 9 | |
10 | - protected $initialValue = 0; |
|
10 | + protected $initialValue = 0; |
|
11 | 11 | |
12 | - protected $memo; |
|
12 | + protected $memo; |
|
13 | 13 | |
14 | - public function __construct() { |
|
14 | + public function __construct() { |
|
15 | 15 | $this->memo = $this->initialValue; |
16 | - } |
|
16 | + } |
|
17 | 17 | |
18 | - public function getMemo() { |
|
18 | + public function getMemo() { |
|
19 | 19 | return $this->memo; |
20 | - } |
|
20 | + } |
|
21 | 21 | |
22 | - abstract public function visit(AbstractField $field); |
|
22 | + abstract public function visit(AbstractField $field); |
|
23 | 23 | } |
24 | 24 | \ No newline at end of file |
@@ -7,25 +7,25 @@ |
||
7 | 7 | |
8 | 8 | class MaxComplexityQueryVisitor extends AbstractQueryVisitor { |
9 | 9 | |
10 | - public $maxScore; |
|
10 | + public $maxScore; |
|
11 | 11 | |
12 | - protected $defaultScore = 1; |
|
12 | + protected $defaultScore = 1; |
|
13 | 13 | |
14 | - public function __construct($max) { |
|
14 | + public function __construct($max) { |
|
15 | 15 | parent::__construct(); |
16 | 16 | |
17 | 17 | $this->maxScore = $max; |
18 | - } |
|
18 | + } |
|
19 | 19 | |
20 | - public function visit(AbstractField $field) { |
|
20 | + public function visit(AbstractField $field) { |
|
21 | 21 | $cost = $field->getConfig()->get('cost'); |
22 | 22 | if (is_callable($cost)) { |
23 | - $cost = $cost(); |
|
23 | + $cost = $cost(); |
|
24 | 24 | } |
25 | 25 | $this->memo += $cost ?: $this->defaultScore; |
26 | 26 | if ($this->memo > $this->maxScore) { |
27 | - throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore); |
|
27 | + throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore); |
|
28 | 28 | } |
29 | 29 | return $this->memo; |
30 | - } |
|
30 | + } |
|
31 | 31 | } |
32 | 32 | \ No newline at end of file |
@@ -24,7 +24,7 @@ |
||
24 | 24 | } |
25 | 25 | $this->memo += $cost ?: $this->defaultScore; |
26 | 26 | if ($this->memo > $this->maxScore) { |
27 | - throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore); |
|
27 | + throw new \Exception('query exceeded max allowed complexity of '.$this->maxScore); |
|
28 | 28 | } |
29 | 29 | return $this->memo; |
30 | 30 | } |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | |
56 | 56 | } |
57 | 57 | |
58 | - public function testNestedVariables() { |
|
58 | + public function testNestedVariables() { |
|
59 | 59 | $processor = new Processor(new TestSchema()); |
60 | 60 | $noArgsQuery = '{ me { echo(value:"foo") } }'; |
61 | 61 | $expectedData = ['data' => ['me' => ['echo' => 'foo']]]; |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | }'; |
82 | 82 | $processor->processPayload($parameterizedQueryQuery, ['value' => 1]); |
83 | 83 | $this->assertArrayNotHasKey('errors', $processor->getResponseData()); |
84 | - } |
|
84 | + } |
|
85 | 85 | |
86 | 86 | public function testListNullResponse() |
87 | 87 | { |
@@ -192,19 +192,19 @@ discard block |
||
192 | 192 | $this->assertEquals(['data' => ['me' => ['firstName' => 'JOHN']]], $processor->getResponseData()); |
193 | 193 | |
194 | 194 | $schema->getMutationType() |
195 | - ->addField(new Field([ |
|
196 | - 'name' => 'increaseCounter', |
|
197 | - 'type' => new IntType(), |
|
198 | - 'resolve' => function ($value, $args, ResolveInfo $info) { |
|
199 | - return $this->_counter += $args['amount']; |
|
200 | - }, |
|
201 | - 'args' => [ |
|
202 | - 'amount' => [ |
|
203 | - 'type' => new IntType(), |
|
204 | - 'default' => 1 |
|
205 | - ] |
|
206 | - ] |
|
207 | - ]))->addField(new Field([ |
|
195 | + ->addField(new Field([ |
|
196 | + 'name' => 'increaseCounter', |
|
197 | + 'type' => new IntType(), |
|
198 | + 'resolve' => function ($value, $args, ResolveInfo $info) { |
|
199 | + return $this->_counter += $args['amount']; |
|
200 | + }, |
|
201 | + 'args' => [ |
|
202 | + 'amount' => [ |
|
203 | + 'type' => new IntType(), |
|
204 | + 'default' => 1 |
|
205 | + ] |
|
206 | + ] |
|
207 | + ]))->addField(new Field([ |
|
208 | 208 | 'name' => 'invalidResolveTypeMutation', |
209 | 209 | 'type' => new NonNullType(new IntType()), |
210 | 210 | 'resolve' => function () { |
@@ -449,68 +449,68 @@ discard block |
||
449 | 449 | } |
450 | 450 | |
451 | 451 | public function testComplexityReducer() { |
452 | - $schema = new Schema( |
|
453 | - [ |
|
454 | - 'query' => new ObjectType( |
|
455 | - [ |
|
456 | - 'name' => 'RootQuery', |
|
457 | - 'fields' => [ |
|
458 | - 'me' => [ |
|
459 | - 'type' => new ObjectType( |
|
460 | - [ |
|
461 | - 'name' => 'User', |
|
462 | - 'fields' => [ |
|
463 | - 'firstName' => [ |
|
464 | - 'type' => new StringType(), |
|
465 | - 'args' => [ |
|
466 | - 'shorten' => new BooleanType() |
|
467 | - ], |
|
468 | - 'resolve' => function ($value, $args) { |
|
452 | + $schema = new Schema( |
|
453 | + [ |
|
454 | + 'query' => new ObjectType( |
|
455 | + [ |
|
456 | + 'name' => 'RootQuery', |
|
457 | + 'fields' => [ |
|
458 | + 'me' => [ |
|
459 | + 'type' => new ObjectType( |
|
460 | + [ |
|
461 | + 'name' => 'User', |
|
462 | + 'fields' => [ |
|
463 | + 'firstName' => [ |
|
464 | + 'type' => new StringType(), |
|
465 | + 'args' => [ |
|
466 | + 'shorten' => new BooleanType() |
|
467 | + ], |
|
468 | + 'resolve' => function ($value, $args) { |
|
469 | 469 | return empty($args['shorten']) ? $value : $value; |
470 | - } |
|
471 | - ], |
|
472 | - 'lastName' => new StringType(), |
|
473 | - 'code' => new StringType(), |
|
474 | - 'likes' => [ |
|
475 | - 'type' => new IntType(), |
|
476 | - 'cost' => 10, |
|
477 | - 'resolve' => function () { |
|
470 | + } |
|
471 | + ], |
|
472 | + 'lastName' => new StringType(), |
|
473 | + 'code' => new StringType(), |
|
474 | + 'likes' => [ |
|
475 | + 'type' => new IntType(), |
|
476 | + 'cost' => 10, |
|
477 | + 'resolve' => function () { |
|
478 | 478 | return 42; |
479 | - } |
|
480 | - ] |
|
481 | - ] |
|
482 | - ] |
|
483 | - ), |
|
484 | - 'resolve' => function ($value, $args) { |
|
479 | + } |
|
480 | + ] |
|
481 | + ] |
|
482 | + ] |
|
483 | + ), |
|
484 | + 'resolve' => function ($value, $args) { |
|
485 | 485 | $data = ['firstName' => 'John', 'code' => '007']; |
486 | 486 | if (!empty($args['upper'])) { |
487 | - foreach ($data as $key => $value) { |
|
487 | + foreach ($data as $key => $value) { |
|
488 | 488 | $data[$key] = strtoupper($value); |
489 | - } |
|
489 | + } |
|
490 | 490 | } |
491 | 491 | |
492 | 492 | return $data; |
493 | - }, |
|
494 | - 'args' => [ |
|
495 | - 'upper' => [ |
|
496 | - 'type' => new BooleanType(), |
|
497 | - 'default' => false |
|
498 | - ] |
|
499 | - ] |
|
500 | - ] |
|
501 | - ] |
|
502 | - ] |
|
503 | - ) |
|
504 | - ] |
|
505 | - ); |
|
506 | - $processor = new Processor($schema); |
|
507 | - |
|
508 | - $processor->setMaxComplexity(10); |
|
509 | - |
|
510 | - $processor->processPayload('{ me { firstName, lastName } }'); |
|
511 | - $this->assertArrayNotHasKey('error', $processor->getResponseData()); |
|
512 | - |
|
513 | - $processor->processPayload('{ me { firstname likes { count } } }'); |
|
514 | - $this->assertEquals(['errors' => [['message' => 'query exceeded max allowed complexity of 10']]], $processor->getResponseData()); |
|
493 | + }, |
|
494 | + 'args' => [ |
|
495 | + 'upper' => [ |
|
496 | + 'type' => new BooleanType(), |
|
497 | + 'default' => false |
|
498 | + ] |
|
499 | + ] |
|
500 | + ] |
|
501 | + ] |
|
502 | + ] |
|
503 | + ) |
|
504 | + ] |
|
505 | + ); |
|
506 | + $processor = new Processor($schema); |
|
507 | + |
|
508 | + $processor->setMaxComplexity(10); |
|
509 | + |
|
510 | + $processor->processPayload('{ me { firstName, lastName } }'); |
|
511 | + $this->assertArrayNotHasKey('error', $processor->getResponseData()); |
|
512 | + |
|
513 | + $processor->processPayload('{ me { firstname likes { count } } }'); |
|
514 | + $this->assertEquals(['errors' => [['message' => 'query exceeded max allowed complexity of 10']]], $processor->getResponseData()); |
|
515 | 515 | } |
516 | 516 | } |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | |
125 | 125 | public function testSchemaOperations() |
126 | 126 | { |
127 | - $schema = new Schema([ |
|
127 | + $schema = new Schema([ |
|
128 | 128 | 'query' => new ObjectType([ |
129 | 129 | 'name' => 'RootQuery', |
130 | 130 | 'fields' => [ |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | 'args' => [ |
138 | 138 | 'shorten' => new BooleanType() |
139 | 139 | ], |
140 | - 'resolve' => function ($value, $args) { |
|
140 | + 'resolve' => function($value, $args) { |
|
141 | 141 | return empty($args['shorten']) ? $value : $value; |
142 | 142 | } |
143 | 143 | ], |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | 'code' => new StringType(), |
146 | 146 | ] |
147 | 147 | ]), |
148 | - 'resolve' => function ($value, $args) { |
|
148 | + 'resolve' => function($value, $args) { |
|
149 | 149 | $data = ['firstName' => 'John', 'code' => '007']; |
150 | 150 | if (!empty($args['upper'])) { |
151 | 151 | foreach ($data as $key => $value) { |
@@ -164,13 +164,13 @@ discard block |
||
164 | 164 | ], |
165 | 165 | 'randomUser' => [ |
166 | 166 | 'type' => new TestObjectType(), |
167 | - 'resolve' => function () { |
|
167 | + 'resolve' => function() { |
|
168 | 168 | return ['invalidField' => 'John']; |
169 | 169 | } |
170 | 170 | ], |
171 | 171 | 'invalidValueQuery' => [ |
172 | 172 | 'type' => new TestObjectType(), |
173 | - 'resolve' => function () { |
|
173 | + 'resolve' => function() { |
|
174 | 174 | return 'stringValue'; |
175 | 175 | } |
176 | 176 | ], |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | ->addField(new Field([ |
196 | 196 | 'name' => 'increaseCounter', |
197 | 197 | 'type' => new IntType(), |
198 | - 'resolve' => function ($value, $args, ResolveInfo $info) { |
|
198 | + 'resolve' => function($value, $args, ResolveInfo $info) { |
|
199 | 199 | return $this->_counter += $args['amount']; |
200 | 200 | }, |
201 | 201 | 'args' => [ |
@@ -207,13 +207,13 @@ discard block |
||
207 | 207 | ]))->addField(new Field([ |
208 | 208 | 'name' => 'invalidResolveTypeMutation', |
209 | 209 | 'type' => new NonNullType(new IntType()), |
210 | - 'resolve' => function () { |
|
210 | + 'resolve' => function() { |
|
211 | 211 | return null; |
212 | 212 | } |
213 | 213 | ]))->addField(new Field([ |
214 | 214 | 'name' => 'interfacedMutation', |
215 | 215 | 'type' => new TestInterfaceType(), |
216 | - 'resolve' => function () { |
|
216 | + 'resolve' => function() { |
|
217 | 217 | return ['name' => 'John']; |
218 | 218 | } |
219 | 219 | ])); |
@@ -259,43 +259,43 @@ discard block |
||
259 | 259 | 'fields' => [ |
260 | 260 | 'listQuery' => [ |
261 | 261 | 'type' => new ListType(new TestEnumType()), |
262 | - 'resolve' => function () { |
|
262 | + 'resolve' => function() { |
|
263 | 263 | return 'invalid list'; |
264 | 264 | } |
265 | 265 | ], |
266 | 266 | 'listEnumQuery' => [ |
267 | 267 | 'type' => new ListType(new TestEnumType()), |
268 | - 'resolve' => function () { |
|
268 | + 'resolve' => function() { |
|
269 | 269 | return ['invalid enum']; |
270 | 270 | } |
271 | 271 | ], |
272 | 272 | 'invalidEnumQuery' => [ |
273 | 273 | 'type' => new TestEnumType(), |
274 | - 'resolve' => function () { |
|
274 | + 'resolve' => function() { |
|
275 | 275 | return 'invalid enum'; |
276 | 276 | } |
277 | 277 | ], |
278 | 278 | 'enumQuery' => [ |
279 | 279 | 'type' => new TestEnumType(), |
280 | - 'resolve' => function () { |
|
280 | + 'resolve' => function() { |
|
281 | 281 | return 1; |
282 | 282 | } |
283 | 283 | ], |
284 | 284 | 'invalidNonNullQuery' => [ |
285 | 285 | 'type' => new NonNullType(new IntType()), |
286 | - 'resolve' => function () { |
|
286 | + 'resolve' => function() { |
|
287 | 287 | return null; |
288 | 288 | } |
289 | 289 | ], |
290 | 290 | 'invalidNonNullInsideQuery' => [ |
291 | 291 | 'type' => new NonNullType(new IntType()), |
292 | - 'resolve' => function () { |
|
292 | + 'resolve' => function() { |
|
293 | 293 | return 'hello'; |
294 | 294 | } |
295 | 295 | ], |
296 | 296 | 'objectQuery' => [ |
297 | 297 | 'type' => new TestObjectType(), |
298 | - 'resolve' => function () { |
|
298 | + 'resolve' => function() { |
|
299 | 299 | return ['name' => 'John']; |
300 | 300 | } |
301 | 301 | ], |
@@ -307,7 +307,7 @@ discard block |
||
307 | 307 | 'enum' => new TestEnumType(), |
308 | 308 | ], |
309 | 309 | ]), |
310 | - 'resolve' => function () { |
|
310 | + 'resolve' => function() { |
|
311 | 311 | return [ |
312 | 312 | 'object' => [ |
313 | 313 | 'name' => 'John' |
@@ -376,10 +376,10 @@ discard block |
||
376 | 376 | ] |
377 | 377 | ]); |
378 | 378 | |
379 | - $union = new UnionType([ |
|
379 | + $union = new UnionType([ |
|
380 | 380 | 'name' => 'TestUnion', |
381 | 381 | 'types' => [$object1, $object2], |
382 | - 'resolveType' => function ($object) use ($object1, $object2) { |
|
382 | + 'resolveType' => function($object) use ($object1, $object2) { |
|
383 | 383 | if (isset($object['id'])) { |
384 | 384 | return $object1; |
385 | 385 | } |
@@ -390,11 +390,11 @@ discard block |
||
390 | 390 | $invalidUnion = new UnionType([ |
391 | 391 | 'name' => 'TestUnion', |
392 | 392 | 'types' => [$object1, $object2], |
393 | - 'resolveType' => function ($object) use ($object3) { |
|
393 | + 'resolveType' => function($object) use ($object3) { |
|
394 | 394 | return $object3; |
395 | 395 | } |
396 | 396 | ]); |
397 | - $processor = new Processor(new Schema([ |
|
397 | + $processor = new Processor(new Schema([ |
|
398 | 398 | 'query' => new ObjectType([ |
399 | 399 | 'name' => 'RootQuery', |
400 | 400 | 'fields' => [ |
@@ -403,7 +403,7 @@ discard block |
||
403 | 403 | 'args' => [ |
404 | 404 | 'type' => ['type' => 'string'] |
405 | 405 | ], |
406 | - 'resolve' => function ($value, $args) { |
|
406 | + 'resolve' => function($value, $args) { |
|
407 | 407 | if ($args['type'] == 'object1') { |
408 | 408 | return [ |
409 | 409 | 'id' => 43 |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | ], |
418 | 418 | 'invalidUnion' => [ |
419 | 419 | 'type' => $invalidUnion, |
420 | - 'resolve' => function () { |
|
420 | + 'resolve' => function() { |
|
421 | 421 | return ['name' => 'name resolved']; |
422 | 422 | } |
423 | 423 | ], |
@@ -465,7 +465,7 @@ discard block |
||
465 | 465 | 'args' => [ |
466 | 466 | 'shorten' => new BooleanType() |
467 | 467 | ], |
468 | - 'resolve' => function ($value, $args) { |
|
468 | + 'resolve' => function($value, $args) { |
|
469 | 469 | return empty($args['shorten']) ? $value : $value; |
470 | 470 | } |
471 | 471 | ], |
@@ -474,14 +474,14 @@ discard block |
||
474 | 474 | 'likes' => [ |
475 | 475 | 'type' => new IntType(), |
476 | 476 | 'cost' => 10, |
477 | - 'resolve' => function () { |
|
477 | + 'resolve' => function() { |
|
478 | 478 | return 42; |
479 | 479 | } |
480 | 480 | ] |
481 | 481 | ] |
482 | 482 | ] |
483 | 483 | ), |
484 | - 'resolve' => function ($value, $args) { |
|
484 | + 'resolve' => function($value, $args) { |
|
485 | 485 | $data = ['firstName' => 'John', 'code' => '007']; |
486 | 486 | if (!empty($args['upper'])) { |
487 | 487 | foreach ($data as $key => $value) { |