@@ -36,7 +36,7 @@ |
||
| 36 | 36 | public static function resolveOfType(AbstractType $value) |
| 37 | 37 | { |
| 38 | 38 | if ($value instanceof CompositeTypeInterface) { |
| 39 | - return $value->getTypeOf(); |
|
| 39 | + return $value->getTypeOf(); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | return null; |
@@ -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]); |