@@ -16,34 +16,34 @@ |
||
16 | 16 | |
17 | 17 | class MaxComplexityQueryVisitor extends AbstractQueryVisitor { |
18 | 18 | |
19 | - public $maxScore; |
|
19 | + public $maxScore; |
|
20 | 20 | |
21 | - protected $defaultScore = 1; |
|
21 | + protected $defaultScore = 1; |
|
22 | 22 | |
23 | - public function __construct($max) { |
|
23 | + public function __construct($max) { |
|
24 | 24 | parent::__construct(); |
25 | 25 | |
26 | 26 | $this->maxScore = $max; |
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * @param array $args |
|
31 | - * @param FieldConfig $fieldConfig |
|
32 | - * @param int $childScore |
|
33 | - * |
|
34 | - * @return int|null |
|
35 | - * @throws \Exception |
|
36 | - */ |
|
37 | - public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0) { |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * @param array $args |
|
31 | + * @param FieldConfig $fieldConfig |
|
32 | + * @param int $childScore |
|
33 | + * |
|
34 | + * @return int|null |
|
35 | + * @throws \Exception |
|
36 | + */ |
|
37 | + public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0) { |
|
38 | 38 | $cost = $fieldConfig->get('cost'); |
39 | 39 | if (is_callable($cost)) { |
40 | - $cost = $cost($args, $fieldConfig, $childScore); |
|
40 | + $cost = $cost($args, $fieldConfig, $childScore); |
|
41 | 41 | } |
42 | 42 | $cost = $cost ?: $this->defaultScore; |
43 | 43 | $this->memo += $cost; |
44 | 44 | if ($this->memo > $this->maxScore) { |
45 | - throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore); |
|
45 | + throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore); |
|
46 | 46 | } |
47 | 47 | return $cost; |
48 | - } |
|
48 | + } |
|
49 | 49 | } |
50 | 50 | \ No newline at end of file |
@@ -13,24 +13,24 @@ |
||
13 | 13 | |
14 | 14 | abstract class AbstractQueryVisitor { |
15 | 15 | |
16 | - protected $initialValue = 0; |
|
16 | + protected $initialValue = 0; |
|
17 | 17 | |
18 | - protected $memo; |
|
18 | + protected $memo; |
|
19 | 19 | |
20 | - public function __construct() { |
|
20 | + public function __construct() { |
|
21 | 21 | $this->memo = $this->initialValue; |
22 | - } |
|
22 | + } |
|
23 | 23 | |
24 | - public function getMemo() { |
|
24 | + public function getMemo() { |
|
25 | 25 | return $this->memo; |
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * @param array $args |
|
30 | - * @param FieldConfig $fieldConfig |
|
31 | - * @param int $childScore |
|
32 | - * |
|
33 | - * @return int|null |
|
34 | - */ |
|
35 | - abstract public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0); |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * @param array $args |
|
30 | + * @param FieldConfig $fieldConfig |
|
31 | + * @param int $childScore |
|
32 | + * |
|
33 | + * @return int|null |
|
34 | + */ |
|
35 | + abstract public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0); |
|
36 | 36 | } |
37 | 37 | \ No newline at end of file |
@@ -212,19 +212,19 @@ discard block |
||
212 | 212 | $this->assertEquals(['data' => ['labels' => ['one', 'two']]], $processor->getResponseData()); |
213 | 213 | |
214 | 214 | $schema->getMutationType() |
215 | - ->addField(new Field([ |
|
216 | - 'name' => 'increaseCounter', |
|
217 | - 'type' => new IntType(), |
|
218 | - 'resolve' => function ($value, $args, ResolveInfo $info) { |
|
219 | - return $this->_counter += $args['amount']; |
|
220 | - }, |
|
221 | - 'args' => [ |
|
222 | - 'amount' => [ |
|
223 | - 'type' => new IntType(), |
|
224 | - 'default' => 1 |
|
225 | - ] |
|
226 | - ] |
|
227 | - ]))->addField(new Field([ |
|
215 | + ->addField(new Field([ |
|
216 | + 'name' => 'increaseCounter', |
|
217 | + 'type' => new IntType(), |
|
218 | + 'resolve' => function ($value, $args, ResolveInfo $info) { |
|
219 | + return $this->_counter += $args['amount']; |
|
220 | + }, |
|
221 | + 'args' => [ |
|
222 | + 'amount' => [ |
|
223 | + 'type' => new IntType(), |
|
224 | + 'default' => 1 |
|
225 | + ] |
|
226 | + ] |
|
227 | + ]))->addField(new Field([ |
|
228 | 228 | 'name' => 'invalidResolveTypeMutation', |
229 | 229 | 'type' => new NonNullType(new IntType()), |
230 | 230 | 'resolve' => function () { |
@@ -482,82 +482,82 @@ discard block |
||
482 | 482 | } |
483 | 483 | |
484 | 484 | public function testComplexityReducer() { |
485 | - $schema = new Schema( |
|
486 | - [ |
|
487 | - 'query' => new ObjectType( |
|
488 | - [ |
|
489 | - 'name' => 'RootQuery', |
|
490 | - 'fields' => [ |
|
491 | - 'me' => [ |
|
492 | - 'type' => new ObjectType( |
|
493 | - [ |
|
494 | - 'name' => 'User', |
|
495 | - 'fields' => [ |
|
496 | - 'firstName' => [ |
|
497 | - 'type' => new StringType(), |
|
498 | - 'args' => [ |
|
499 | - 'shorten' => new BooleanType() |
|
500 | - ], |
|
501 | - 'resolve' => function ($value, $args) { |
|
485 | + $schema = new Schema( |
|
486 | + [ |
|
487 | + 'query' => new ObjectType( |
|
488 | + [ |
|
489 | + 'name' => 'RootQuery', |
|
490 | + 'fields' => [ |
|
491 | + 'me' => [ |
|
492 | + 'type' => new ObjectType( |
|
493 | + [ |
|
494 | + 'name' => 'User', |
|
495 | + 'fields' => [ |
|
496 | + 'firstName' => [ |
|
497 | + 'type' => new StringType(), |
|
498 | + 'args' => [ |
|
499 | + 'shorten' => new BooleanType() |
|
500 | + ], |
|
501 | + 'resolve' => function ($value, $args) { |
|
502 | 502 | return empty($args['shorten']) ? $value : $value; |
503 | - } |
|
504 | - ], |
|
505 | - 'lastName' => new StringType(), |
|
506 | - 'code' => new StringType(), |
|
507 | - 'likes' => [ |
|
508 | - 'type' => new IntType(), |
|
509 | - 'cost' => 10, |
|
510 | - 'resolve' => function () { |
|
503 | + } |
|
504 | + ], |
|
505 | + 'lastName' => new StringType(), |
|
506 | + 'code' => new StringType(), |
|
507 | + 'likes' => [ |
|
508 | + 'type' => new IntType(), |
|
509 | + 'cost' => 10, |
|
510 | + 'resolve' => function () { |
|
511 | 511 | return 42; |
512 | - } |
|
513 | - ] |
|
514 | - ] |
|
515 | - ] |
|
516 | - ), |
|
517 | - 'cost' => function ($args, $context, $childCost) { |
|
512 | + } |
|
513 | + ] |
|
514 | + ] |
|
515 | + ] |
|
516 | + ), |
|
517 | + 'cost' => function ($args, $context, $childCost) { |
|
518 | 518 | $argsCost = isset($args['cost']) ? $args['cost'] : 1; |
519 | 519 | return 1 + $argsCost * $childCost; |
520 | - }, |
|
521 | - 'resolve' => function ($value, $args) { |
|
520 | + }, |
|
521 | + 'resolve' => function ($value, $args) { |
|
522 | 522 | $data = ['firstName' => 'John', 'code' => '007']; |
523 | 523 | |
524 | 524 | return $data; |
525 | - }, |
|
526 | - 'args' => [ |
|
527 | - 'cost' => [ |
|
528 | - 'type' => new IntType(), |
|
529 | - 'default' => 1 |
|
530 | - ] |
|
531 | - ] |
|
532 | - ] |
|
533 | - ] |
|
534 | - ] |
|
535 | - ) |
|
536 | - ] |
|
537 | - ); |
|
538 | - $processor = new Processor($schema); |
|
539 | - |
|
540 | - $processor->setMaxComplexity(10); |
|
541 | - |
|
542 | - $processor->processPayload('{ me { firstName, lastName } }'); |
|
543 | - $this->assertArrayNotHasKey('error', $processor->getResponseData()); |
|
544 | - |
|
545 | - $processor->processPayload('{ me { firstName, likes } }'); |
|
546 | - $this->assertEquals(['errors' => [['message' => 'query exceeded max allowed complexity of 10']]], $processor->getResponseData()); |
|
547 | - |
|
548 | - // don't let complexity reducer affect query errors |
|
549 | - $processor->processPayload('{ me { badfield } }'); |
|
550 | - $this->assertArraySubset(['errors' => [['message' => 'Field "badfield" not found in type "User"']]], $processor->getResponseData()); |
|
525 | + }, |
|
526 | + 'args' => [ |
|
527 | + 'cost' => [ |
|
528 | + 'type' => new IntType(), |
|
529 | + 'default' => 1 |
|
530 | + ] |
|
531 | + ] |
|
532 | + ] |
|
533 | + ] |
|
534 | + ] |
|
535 | + ) |
|
536 | + ] |
|
537 | + ); |
|
538 | + $processor = new Processor($schema); |
|
551 | 539 | |
552 | - foreach (range(1,5) as $cost_multiplier) { |
|
540 | + $processor->setMaxComplexity(10); |
|
541 | + |
|
542 | + $processor->processPayload('{ me { firstName, lastName } }'); |
|
543 | + $this->assertArrayNotHasKey('error', $processor->getResponseData()); |
|
544 | + |
|
545 | + $processor->processPayload('{ me { firstName, likes } }'); |
|
546 | + $this->assertEquals(['errors' => [['message' => 'query exceeded max allowed complexity of 10']]], $processor->getResponseData()); |
|
547 | + |
|
548 | + // don't let complexity reducer affect query errors |
|
549 | + $processor->processPayload('{ me { badfield } }'); |
|
550 | + $this->assertArraySubset(['errors' => [['message' => 'Field "badfield" not found in type "User"']]], $processor->getResponseData()); |
|
551 | + |
|
552 | + foreach (range(1,5) as $cost_multiplier) { |
|
553 | 553 | $visitor = new \Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor(1000); // arbitrarily high cost |
554 | 554 | $processor->processPayload("{ me (cost: $cost_multiplier) { firstName, lastName, code, likes } }", ['cost' => $cost_multiplier], [$visitor]); |
555 | 555 | $expected = 1 + 13 * (1 + $cost_multiplier); |
556 | 556 | $this->assertEquals($expected, $visitor->getMemo()); |
557 | - } |
|
557 | + } |
|
558 | 558 | |
559 | - // TODO, variables not yet supported |
|
560 | - /*$query = 'query costQuery ($cost: Int) { me (cost: $cost) { firstName, lastName, code, likes } }'; |
|
559 | + // TODO, variables not yet supported |
|
560 | + /*$query = 'query costQuery ($cost: Int) { me (cost: $cost) { firstName, lastName, code, likes } }'; |
|
561 | 561 | foreach (range(1,5) as $cost_multiplier) { |
562 | 562 | $visitor = new \Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor(1000); // arbitrarily high cost |
563 | 563 | $processor->processPayload($query, ['cost' => $cost_multiplier], [$visitor]); |