graphql-php/src/GraphQL.php

92 lines
3.0 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
namespace GraphQL;
use GraphQL\Error\Error;
2016-12-03 00:11:14 +03:00
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\ExecutionResult;
2015-07-15 20:05:46 +03:00
use GraphQL\Executor\Executor;
use GraphQL\Executor\Promise\Promise;
use GraphQL\Language\AST\DocumentNode;
2015-07-15 20:05:46 +03:00
use GraphQL\Language\Parser;
use GraphQL\Language\Source;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Type\Definition\Directive;
2015-07-15 20:05:46 +03:00
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\QueryComplexity;
2015-07-15 20:05:46 +03:00
class GraphQL
{
/**
* @param Schema $schema
* @param $requestString
* @param mixed $rootValue
* @param array|null $variableValues
2015-07-15 20:05:46 +03:00
* @param string|null $operationName
* @return Promise|array
2015-07-15 20:05:46 +03:00
*/
public static function execute(Schema $schema, $requestString, $rootValue = null, $contextValue = null, $variableValues = null, $operationName = null)
{
$result = self::executeAndReturnResult($schema, $requestString, $rootValue, $contextValue, $variableValues, $operationName);
if ($result instanceof ExecutionResult) {
return $result->toArray();
}
2016-12-03 00:11:14 +03:00
if ($result instanceof Promise) {
return $result->then(function(ExecutionResult $executionResult) {
return $executionResult->toArray();
2016-12-03 00:11:14 +03:00
});
}
throw new InvariantViolation("Unexpected execution result");
}
/**
* @param Schema $schema
* @param $requestString
* @param null $rootValue
* @param null $variableValues
* @param null $operationName
* @return ExecutionResult|Promise
*/
public static function executeAndReturnResult(Schema $schema, $requestString, $rootValue = null, $contextValue = null, $variableValues = null, $operationName = null)
2015-07-15 20:05:46 +03:00
{
try {
if ($requestString instanceof DocumentNode) {
$documentNode = $requestString;
} else {
$source = new Source($requestString ?: '', 'GraphQL request');
$documentNode = Parser::parse($source);
}
/** @var QueryComplexity $queryComplexity */
$queryComplexity = DocumentValidator::getRule('QueryComplexity');
$queryComplexity->setRawVariableValues($variableValues);
$validationErrors = DocumentValidator::validate($schema, $documentNode);
2015-07-15 20:05:46 +03:00
if (!empty($validationErrors)) {
return new ExecutionResult(null, $validationErrors);
2015-07-15 20:05:46 +03:00
} else {
return Executor::execute($schema, $documentNode, $rootValue, $contextValue, $variableValues, $operationName);
2015-07-15 20:05:46 +03:00
}
} catch (Error $e) {
return new ExecutionResult(null, [$e]);
2015-07-15 20:05:46 +03:00
}
}
/**
* @return array
*/
public static function getInternalDirectives()
{
return array_values(Directive::getInternalDirectives());
}
/**
* @param PromiseAdapter|null $promiseAdapter
*/
public static function setPromiseAdapter(PromiseAdapter $promiseAdapter = null)
{
Executor::setPromiseAdapter($promiseAdapter);
}
2015-07-15 20:05:46 +03:00
}