@@ -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 () { |
@@ -462,82 +462,82 @@ discard block |
||
462 | 462 | } |
463 | 463 | |
464 | 464 | public function testComplexityReducer() { |
465 | - $schema = new Schema( |
|
466 | - [ |
|
467 | - 'query' => new ObjectType( |
|
468 | - [ |
|
469 | - 'name' => 'RootQuery', |
|
470 | - 'fields' => [ |
|
471 | - 'me' => [ |
|
472 | - 'type' => new ObjectType( |
|
473 | - [ |
|
474 | - 'name' => 'User', |
|
475 | - 'fields' => [ |
|
476 | - 'firstName' => [ |
|
477 | - 'type' => new StringType(), |
|
478 | - 'args' => [ |
|
479 | - 'shorten' => new BooleanType() |
|
480 | - ], |
|
481 | - 'resolve' => function ($value, $args) { |
|
465 | + $schema = new Schema( |
|
466 | + [ |
|
467 | + 'query' => new ObjectType( |
|
468 | + [ |
|
469 | + 'name' => 'RootQuery', |
|
470 | + 'fields' => [ |
|
471 | + 'me' => [ |
|
472 | + 'type' => new ObjectType( |
|
473 | + [ |
|
474 | + 'name' => 'User', |
|
475 | + 'fields' => [ |
|
476 | + 'firstName' => [ |
|
477 | + 'type' => new StringType(), |
|
478 | + 'args' => [ |
|
479 | + 'shorten' => new BooleanType() |
|
480 | + ], |
|
481 | + 'resolve' => function ($value, $args) { |
|
482 | 482 | return empty($args['shorten']) ? $value : $value; |
483 | - } |
|
484 | - ], |
|
485 | - 'lastName' => new StringType(), |
|
486 | - 'code' => new StringType(), |
|
487 | - 'likes' => [ |
|
488 | - 'type' => new IntType(), |
|
489 | - 'cost' => 10, |
|
490 | - 'resolve' => function () { |
|
483 | + } |
|
484 | + ], |
|
485 | + 'lastName' => new StringType(), |
|
486 | + 'code' => new StringType(), |
|
487 | + 'likes' => [ |
|
488 | + 'type' => new IntType(), |
|
489 | + 'cost' => 10, |
|
490 | + 'resolve' => function () { |
|
491 | 491 | return 42; |
492 | - } |
|
493 | - ] |
|
494 | - ] |
|
495 | - ] |
|
496 | - ), |
|
497 | - 'cost' => function ($args, $context, $childCost) { |
|
492 | + } |
|
493 | + ] |
|
494 | + ] |
|
495 | + ] |
|
496 | + ), |
|
497 | + 'cost' => function ($args, $context, $childCost) { |
|
498 | 498 | $argsCost = isset($args['cost']) ? $args['cost'] : 1; |
499 | 499 | return 1 + $argsCost * $childCost; |
500 | - }, |
|
501 | - 'resolve' => function ($value, $args) { |
|
500 | + }, |
|
501 | + 'resolve' => function ($value, $args) { |
|
502 | 502 | $data = ['firstName' => 'John', 'code' => '007']; |
503 | 503 | |
504 | 504 | return $data; |
505 | - }, |
|
506 | - 'args' => [ |
|
507 | - 'cost' => [ |
|
508 | - 'type' => new IntType(), |
|
509 | - 'default' => 1 |
|
510 | - ] |
|
511 | - ] |
|
512 | - ] |
|
513 | - ] |
|
514 | - ] |
|
515 | - ) |
|
516 | - ] |
|
517 | - ); |
|
518 | - $processor = new Processor($schema); |
|
519 | - |
|
520 | - $processor->setMaxComplexity(10); |
|
521 | - |
|
522 | - $processor->processPayload('{ me { firstName, lastName } }'); |
|
523 | - $this->assertArrayNotHasKey('error', $processor->getResponseData()); |
|
524 | - |
|
525 | - $processor->processPayload('{ me { firstName, likes } }'); |
|
526 | - $this->assertEquals(['errors' => [['message' => 'query exceeded max allowed complexity of 10']]], $processor->getResponseData()); |
|
527 | - |
|
528 | - // don't let complexity reducer affect query errors |
|
529 | - $processor->processPayload('{ me { badfield } }'); |
|
530 | - $this->assertArraySubset(['errors' => [['message' => 'Field "badfield" not found in type "User"']]], $processor->getResponseData()); |
|
505 | + }, |
|
506 | + 'args' => [ |
|
507 | + 'cost' => [ |
|
508 | + 'type' => new IntType(), |
|
509 | + 'default' => 1 |
|
510 | + ] |
|
511 | + ] |
|
512 | + ] |
|
513 | + ] |
|
514 | + ] |
|
515 | + ) |
|
516 | + ] |
|
517 | + ); |
|
518 | + $processor = new Processor($schema); |
|
531 | 519 | |
532 | - foreach (range(1,5) as $cost_multiplier) { |
|
520 | + $processor->setMaxComplexity(10); |
|
521 | + |
|
522 | + $processor->processPayload('{ me { firstName, lastName } }'); |
|
523 | + $this->assertArrayNotHasKey('error', $processor->getResponseData()); |
|
524 | + |
|
525 | + $processor->processPayload('{ me { firstName, likes } }'); |
|
526 | + $this->assertEquals(['errors' => [['message' => 'query exceeded max allowed complexity of 10']]], $processor->getResponseData()); |
|
527 | + |
|
528 | + // don't let complexity reducer affect query errors |
|
529 | + $processor->processPayload('{ me { badfield } }'); |
|
530 | + $this->assertArraySubset(['errors' => [['message' => 'Field "badfield" not found in type "User"']]], $processor->getResponseData()); |
|
531 | + |
|
532 | + foreach (range(1,5) as $cost_multiplier) { |
|
533 | 533 | $visitor = new \Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor(1000); // arbitrarily high cost |
534 | 534 | $processor->processPayload("{ me (cost: $cost_multiplier) { firstName, lastName, code, likes } }", ['cost' => $cost_multiplier], [$visitor]); |
535 | 535 | $expected = 1 + 13 * (1 + $cost_multiplier); |
536 | 536 | $this->assertEquals($expected, $visitor->getMemo()); |
537 | - } |
|
537 | + } |
|
538 | 538 | |
539 | - // TODO, variables not yet supported |
|
540 | - /*$query = 'query costQuery ($cost: Int) { me (cost: $cost) { firstName, lastName, code, likes } }'; |
|
539 | + // TODO, variables not yet supported |
|
540 | + /*$query = 'query costQuery ($cost: Int) { me (cost: $cost) { firstName, lastName, code, likes } }'; |
|
541 | 541 | foreach (range(1,5) as $cost_multiplier) { |
542 | 542 | $visitor = new \Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor(1000); // arbitrarily high cost |
543 | 543 | $processor->processPayload($query, ['cost' => $cost_multiplier], [$visitor]); |
@@ -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 |