@@ -42,8 +42,8 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | 44 | * @param AbstractObjectType $objectType |
| 45 | - * @param Mutation|Query|AstField $field |
|
| 46 | - * @return null |
|
| 45 | + * @param AstField $field |
|
| 46 | + * @return boolean |
|
| 47 | 47 | */ |
| 48 | 48 | public function objectHasField($objectType, $field) |
| 49 | 49 | { |
@@ -58,6 +58,7 @@ discard block |
||
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | 60 | * @inheritdoc |
| 61 | + * @param Query $query |
|
| 61 | 62 | */ |
| 62 | 63 | public function validateArguments(AbstractField $field, $query, Request $request) |
| 63 | 64 | { |
@@ -20,7 +20,6 @@ |
||
| 20 | 20 | use Youshido\GraphQL\Parser\Ast\Mutation; |
| 21 | 21 | use Youshido\GraphQL\Parser\Ast\Query; |
| 22 | 22 | use Youshido\GraphQL\Type\AbstractType; |
| 23 | -use Youshido\GraphQL\Type\CompositeTypeInterface; |
|
| 24 | 23 | use Youshido\GraphQL\Type\InputObject\AbstractInputObjectType; |
| 25 | 24 | use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType; |
| 26 | 25 | use Youshido\GraphQL\Type\ListType\AbstractListType; |
@@ -27,7 +27,6 @@ |
||
| 27 | 27 | use Youshido\GraphQL\Schema\AbstractSchema; |
| 28 | 28 | use Youshido\GraphQL\Type\AbstractType; |
| 29 | 29 | use Youshido\GraphQL\Type\Object\AbstractObjectType; |
| 30 | -use Youshido\GraphQL\Type\Scalar\AbstractScalarType; |
|
| 31 | 30 | use Youshido\GraphQL\Type\TypeInterface; |
| 32 | 31 | use Youshido\GraphQL\Type\TypeMap; |
| 33 | 32 | use Youshido\GraphQL\Type\TypeService; |
@@ -236,7 +236,7 @@ |
||
| 236 | 236 | |
| 237 | 237 | $value = $listValue; |
| 238 | 238 | } else { |
| 239 | - $value = $this->getOutputValue($field->getType(), $preResolvedValue);//$this->getFieldValidatedValue($field, $preResolvedValue); |
|
| 239 | + $value = $this->getOutputValue($field->getType(), $preResolvedValue); //$this->getFieldValidatedValue($field, $preResolvedValue); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | return $value; |
@@ -43,12 +43,12 @@ discard block |
||
| 43 | 43 | $authors = []; |
| 44 | 44 | while (count($authors) < rand(1, 4)) { |
| 45 | 45 | $authors[] = [ |
| 46 | - 'name' => 'Author ' . substr(md5(time()), 0, 4) |
|
| 46 | + 'name' => 'Author '.substr(md5(time()), 0, 4) |
|
| 47 | 47 | ]; |
| 48 | 48 | } |
| 49 | 49 | $data[] = [ |
| 50 | 50 | 'id' => $i, |
| 51 | - 'title' => 'Title of ' . $i, |
|
| 51 | + 'title' => 'Title of '.$i, |
|
| 52 | 52 | 'authors' => $authors, |
| 53 | 53 | ]; |
| 54 | 54 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | // return true; |
| 70 | 70 | $p->processPayload('{ posts { id, title, authors { name } } }'); |
| 71 | 71 | $res = $p->getResponseData(); |
| 72 | - echo "Count: " . count($res['data']['posts']) . "\n"; |
|
| 72 | + echo "Count: ".count($res['data']['posts'])."\n"; |
|
| 73 | 73 | printf("Test Time: %04f\n", microtime(true) - $time); |
| 74 | 74 | // $p->getResponseData(); |
| 75 | 75 | } |
@@ -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" is 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" is 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]); |