2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL;
|
|
|
|
|
|
|
|
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 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
|
|
|
|
*/
|
2015-08-17 17:01:55 +03:00
|
|
|
public static function execute(Schema $schema, $requestString, $rootValue = null, $variableValues = null, $operationName = null)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$source = new Source($requestString ?: '', 'GraphQL request');
|
2015-08-17 17:01:55 +03:00
|
|
|
$documentAST = Parser::parse($source);
|
|
|
|
$validationErrors = DocumentValidator::validate($schema, $documentAST);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
if (!empty($validationErrors)) {
|
|
|
|
return ['errors' => array_map(['GraphQL\Error', 'formatError'], $validationErrors)];
|
2015-07-15 20:05:46 +03:00
|
|
|
} else {
|
2015-08-17 17:01:55 +03:00
|
|
|
return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName)->toArray();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
} catch (Error $e) {
|
|
|
|
return ['errors' => [Error::formatError($e)]];
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|