getExecutionContext() public method

You can access ExecutionContext to check errors and inject dependencies
public getExecutionContext ( ) : ExecutionContext
return Youshido\GraphQL\Execution\Context\ExecutionContext
Beispiel #1
0
 public function testCustomTypes()
 {
     $authorType = null;
     $userInterface = new ObjectType(['name' => 'UserInterface', 'fields' => ['name' => new StringType()], 'resolveType' => function () use($authorType) {
         return $authorType;
     }]);
     $authorType = new ObjectType(['name' => 'Author', 'fields' => ['name' => new StringType()], 'interfaces' => [$userInterface]]);
     $schema = new Schema(['query' => new ObjectType(['name' => 'QueryType', 'fields' => ['user' => ['type' => $userInterface, 'resolve' => function () {
         return ['name' => 'Alex'];
     }]]])]);
     $schema->getTypesList()->addType($authorType);
     $processor = new Processor($schema);
     $processor->processPayload('{ user { name } }');
     $this->assertEquals(['data' => ['user' => ['name' => 'Alex']]], $processor->getResponseData());
     $processor->processPayload('{
                 __schema {
                     types {
                         name
                     }
                 }
             }');
     $data = $processor->getResponseData();
     $this->assertArraySubset([11 => ['name' => 'Author']], $data['data']['__schema']['types']);
     $processor->processPayload('{ user { name { } } }');
     $result = $processor->getResponseData();
     $this->assertEquals(['errors' => [['message' => 'Unexpected token "RBRACE"', 'locations' => [['line' => 1, 'column' => 19]]]]], $result);
     $processor->getExecutionContext()->clearErrors();
     $processor->processPayload('{ user { name { invalidSelection } } }');
     $result = $processor->getResponseData();
     $this->assertEquals(['data' => ['user' => null], 'errors' => [['message' => 'You can\'t specify fields for scalar type "String"', 'locations' => [['line' => 1, 'column' => 10]]]]], $result);
 }
 public function testListInsideInputObject()
 {
     $processor = new Processor(new Schema(['query' => new ObjectType(['name' => 'RootQueryType', 'fields' => ['empty' => ['type' => new StringType(), 'resolve' => function () {
     }]]]), 'mutation' => new ObjectType(['name' => 'RootMutation', 'fields' => ['createList' => ['type' => new StringType(), 'args' => ['topArgument' => new InputObjectType(['name' => 'topArgument', 'fields' => ['postObject' => new ListType(new InputObjectType(['name' => 'postObject', 'fields' => ['title' => new NonNullType(new StringType())]]))]])], 'resolve' => function () {
         return 'success message';
     }]]])]));
     $processor->processPayload('mutation { createList(topArgument: { postObject:[ { title: null } ] })}');
     $this->assertEquals(['data' => ['createList' => null], 'errors' => [['message' => 'Not valid type for argument "topArgument" in query "createList"', 'locations' => [['line' => 1, 'column' => 23]]]]], $processor->getResponseData());
     $processor->getExecutionContext()->clearErrors();
     $processor->processPayload('mutation { createList(topArgument:{
                                     postObject:[{title: "not empty"}] })}');
     $this->assertEquals(['data' => ['createList' => 'success message']], $processor->getResponseData());
 }
Beispiel #3
0
 public function testComplexityReducer()
 {
     $schema = new Schema(['query' => new ObjectType(['name' => 'RootQuery', 'fields' => ['me' => ['type' => new ObjectType(['name' => 'User', 'fields' => ['firstName' => ['type' => new StringType(), 'args' => ['shorten' => new BooleanType()], 'resolve' => function ($value, $args) {
         return empty($args['shorten']) ? $value['firstName'] : $value['firstName'];
     }], 'lastName' => new StringType(), 'code' => new StringType(), 'likes' => ['type' => new IntType(), 'cost' => 10, 'resolve' => function () {
         return 42;
     }]]]), 'cost' => function ($args, $context, $childCost) {
         $argsCost = isset($args['cost']) ? $args['cost'] : 1;
         return 1 + $argsCost * $childCost;
     }, 'resolve' => function ($value, $args) {
         $data = ['firstName' => 'John', 'code' => '007'];
         return $data;
     }, 'args' => ['cost' => ['type' => new IntType(), 'default' => 1]]]]])]);
     $processor = new Processor($schema);
     $processor->setMaxComplexity(10);
     $processor->processPayload('{ me { firstName, lastName } }');
     $this->assertArrayNotHasKey('error', $processor->getResponseData());
     $processor->processPayload('{ me { } }');
     $this->assertEquals(['errors' => [['message' => 'Unexpected token "RBRACE"', 'locations' => [['line' => 1, 'column' => 10]]]]], $processor->getResponseData());
     $processor->getExecutionContext()->clearErrors();
     $processor->processPayload('{ me { firstName, likes } }');
     $this->assertEquals(['errors' => [['message' => 'query exceeded max allowed complexity of 10']]], $processor->getResponseData());
     $processor->getExecutionContext()->clearErrors();
     // don't let complexity reducer affect query errors
     $processor->processPayload('{ me { badfield } }');
     $this->assertArraySubset(['errors' => [['message' => 'Field "badfield" not found in type "User"']]], $processor->getResponseData());
     $processor->getExecutionContext()->clearErrors();
     foreach (range(1, 5) as $cost_multiplier) {
         $visitor = new MaxComplexityQueryVisitor(1000);
         // arbitrarily high cost
         $processor->processPayload("{ me (cost: {$cost_multiplier}) { firstName, lastName, code, likes } }", ['cost' => $cost_multiplier], [$visitor]);
         $expected = 1 + 13 * (1 + $cost_multiplier);
         $this->assertEquals($expected, $visitor->getMemo());
     }
     // TODO, variables not yet supported
     /*$query = 'query costQuery ($cost: Int) { me (cost: $cost) { firstName, lastName, code, likes } }';
       foreach (range(1,5) as $cost_multiplier) {
         $visitor = new \Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor(1000); // arbitrarily high cost
         $processor->processPayload($query, ['cost' => $cost_multiplier], [$visitor]);
         $expected = 1 + 13 * (1 + $cost_multiplier);
         $this->assertEquals($expected, $visitor->getMemo());
       }*/
 }