2015-07-15 23:05:46 +06:00
|
|
|
<?php
|
|
|
|
namespace GraphQL;
|
|
|
|
|
2015-11-02 20:39:51 +06:00
|
|
|
use GraphQL\Executor\ExecutionResult;
|
2015-07-15 23:05:46 +06:00
|
|
|
use GraphQL\Executor\Executor;
|
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Language\Source;
|
|
|
|
use GraphQL\Validator\DocumentValidator;
|
|
|
|
|
|
|
|
class GraphQL
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
* @param $requestString
|
2015-08-17 20:01:55 +06:00
|
|
|
* @param mixed $rootValue
|
2015-07-15 23:05:46 +06:00
|
|
|
* @param array <string, string>|null $variableValues
|
|
|
|
* @param string|null $operationName
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-08-17 20:01:55 +06:00
|
|
|
public static function execute(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null)
|
2015-11-02 20:39:51 +06:00
|
|
|
{
|
|
|
|
return self::executeAndReturnResult($schema, $requestString, $rootValue, $variableValues, $operationName)->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
* @param $requestString
|
|
|
|
* @param null $rootValue
|
|
|
|
* @param null $variableValues
|
|
|
|
* @param null $operationName
|
|
|
|
* @return array|ExecutionResult
|
|
|
|
*/
|
|
|
|
public static function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null)
|
2015-07-15 23:05:46 +06:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$source = new Source($requestString ?: '', 'GraphQL request');
|
2015-08-17 20:01:55 +06:00
|
|
|
$documentAST = Parser::parse($source);
|
|
|
|
$validationErrors = DocumentValidator::validate($schema, $documentAST);
|
2015-07-15 23:05:46 +06:00
|
|
|
|
2015-08-17 20:01:55 +06:00
|
|
|
if (!empty($validationErrors)) {
|
2015-11-02 20:39:51 +06:00
|
|
|
return new ExecutionResult(null, $validationErrors);
|
2015-07-15 23:05:46 +06:00
|
|
|
} else {
|
2015-11-02 20:39:51 +06:00
|
|
|
return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName);
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
2015-08-17 20:01:55 +06:00
|
|
|
} catch (Error $e) {
|
2015-11-02 20:39:51 +06:00
|
|
|
return new ExecutionResult(null, [$e]);
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|