2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL;
|
|
|
|
|
2015-11-02 17:39:51 +03:00
|
|
|
use GraphQL\Executor\ExecutionResult;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Executor\Executor;
|
2016-05-02 00:42:05 +03:00
|
|
|
use GraphQL\Language\AST\Document;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Language\Source;
|
2016-10-18 21:34:46 +03:00
|
|
|
use GraphQL\Type\Definition\Directive;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Validator\DocumentValidator;
|
2016-04-09 11:04:14 +03:00
|
|
|
use GraphQL\Validator\Rules\QueryComplexity;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
class GraphQL
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
* @param $requestString
|
2015-08-17 17:01:55 +03:00
|
|
|
* @param mixed $rootValue
|
2015-07-15 20:05:46 +03:00
|
|
|
* @param array <string, string>|null $variableValues
|
|
|
|
* @param string|null $operationName
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-05-02 00:42:05 +03:00
|
|
|
public static function execute(Schema $schema, $requestString, $rootValue = null, $contextValue = null, $variableValues = null, $operationName = null)
|
2015-11-02 17:39:51 +03:00
|
|
|
{
|
2016-05-02 00:42:05 +03:00
|
|
|
return self::executeAndReturnResult($schema, $requestString, $rootValue, $contextValue, $variableValues, $operationName)->toArray();
|
2015-11-02 17:39:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Schema $schema
|
|
|
|
* @param $requestString
|
|
|
|
* @param null $rootValue
|
|
|
|
* @param null $variableValues
|
|
|
|
* @param null $operationName
|
|
|
|
* @return array|ExecutionResult
|
|
|
|
*/
|
2016-05-02 00:42:05 +03:00
|
|
|
public static function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $contextValue = null, $variableValues = null, $operationName = null)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
try {
|
2016-05-02 00:42:05 +03:00
|
|
|
if ($requestString instanceof Document) {
|
|
|
|
$documentAST = $requestString;
|
|
|
|
} else {
|
|
|
|
$source = new Source($requestString ?: '', 'GraphQL request');
|
|
|
|
$documentAST = Parser::parse($source);
|
|
|
|
}
|
2016-04-09 11:04:14 +03:00
|
|
|
|
|
|
|
/** @var QueryComplexity $queryComplexity */
|
|
|
|
$queryComplexity = DocumentValidator::getRule('QueryComplexity');
|
|
|
|
$queryComplexity->setRawVariableValues($variableValues);
|
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
$validationErrors = DocumentValidator::validate($schema, $documentAST);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
if (!empty($validationErrors)) {
|
2015-11-02 17:39:51 +03:00
|
|
|
return new ExecutionResult(null, $validationErrors);
|
2015-07-15 20:05:46 +03:00
|
|
|
} else {
|
2016-05-02 00:42:05 +03:00
|
|
|
return Executor::execute($schema, $documentAST, $rootValue, $contextValue, $variableValues, $operationName);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
} catch (Error $e) {
|
2015-11-02 17:39:51 +03:00
|
|
|
return new ExecutionResult(null, [$e]);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-18 21:34:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getInternalDirectives()
|
|
|
|
{
|
|
|
|
return array_values(Directive::getInternalDirectives());
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|