Youshido\GraphQL\Execution\Processor::processPayload PHP Method

processPayload() public method

public processPayload ( $payload, $variables = [], $reducers = [] )
    public function processPayload($payload, $variables = [], $reducers = [])
    {
        $this->data = [];
        try {
            $this->parseAndCreateRequest($payload, $variables);
            if ($this->maxComplexity) {
                $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity);
            }
            if ($reducers) {
                $reducer = new Reducer();
                $reducer->reduceQuery($this->executionContext, $reducers);
            }
            foreach ($this->executionContext->getRequest()->getAllOperations() as $query) {
                if ($operationResult = $this->resolveQuery($query)) {
                    $this->data = array_merge($this->data, $operationResult);
                }
            }
        } catch (\Exception $e) {
            $this->executionContext->addError($e);
        }
        return $this;
    }

Usage Example

Esempio n. 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);
 }
All Usage Examples Of Youshido\GraphQL\Execution\Processor::processPayload