@@ -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 | } |
@@ -7,34 +7,34 @@ |
||
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 | - } |
|
19 | - |
|
20 | - /** |
|
21 | - * @param array $args |
|
22 | - * @param FieldConfig $fieldConfig |
|
23 | - * @param int $childScore |
|
24 | - * |
|
25 | - * @return mixed |
|
26 | - * @throws \Exception |
|
27 | - */ |
|
28 | - public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0) { |
|
18 | + } |
|
19 | + |
|
20 | + /** |
|
21 | + * @param array $args |
|
22 | + * @param FieldConfig $fieldConfig |
|
23 | + * @param int $childScore |
|
24 | + * |
|
25 | + * @return mixed |
|
26 | + * @throws \Exception |
|
27 | + */ |
|
28 | + public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0) { |
|
29 | 29 | $cost = $fieldConfig->get('cost'); |
30 | 30 | if (is_callable($cost)) { |
31 | - $cost = $cost($args, $fieldConfig, $childScore); |
|
31 | + $cost = $cost($args, $fieldConfig, $childScore); |
|
32 | 32 | } |
33 | 33 | $cost = $cost ?: $this->defaultScore; |
34 | 34 | $this->memo += $cost; |
35 | 35 | if ($this->memo > $this->maxScore) { |
36 | - throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore); |
|
36 | + throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore); |
|
37 | 37 | } |
38 | 38 | return $cost; |
39 | - } |
|
39 | + } |
|
40 | 40 | } |
41 | 41 | \ No newline at end of file |
@@ -7,24 +7,24 @@ |
||
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 | - } |
|
21 | - |
|
22 | - /** |
|
23 | - * @param array $args |
|
24 | - * @param FieldConfig $fieldConfig |
|
25 | - * @param int $childScore |
|
26 | - * |
|
27 | - * @return mixed |
|
28 | - */ |
|
29 | - abstract public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0); |
|
20 | + } |
|
21 | + |
|
22 | + /** |
|
23 | + * @param array $args |
|
24 | + * @param FieldConfig $fieldConfig |
|
25 | + * @param int $childScore |
|
26 | + * |
|
27 | + * @return mixed |
|
28 | + */ |
|
29 | + abstract public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0); |
|
30 | 30 | } |
31 | 31 | \ No newline at end of file |