graphql-php/src/GraphQL.php

36 lines
1.1 KiB
PHP
Raw Normal View History

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
* @param mixed $rootValue
2015-07-15 20:05:46 +03:00
* @param array <string, string>|null $variableValues
* @param string|null $operationName
* @return array
*/
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');
$documentAST = Parser::parse($source);
$validationErrors = DocumentValidator::validate($schema, $documentAST);
2015-07-15 20:05:46 +03:00
if (!empty($validationErrors)) {
return ['errors' => array_map(['GraphQL\Error', 'formatError'], $validationErrors)];
2015-07-15 20:05:46 +03:00
} else {
return Executor::execute($schema, $documentAST, $rootValue, $variableValues, $operationName)->toArray();
2015-07-15 20:05:46 +03:00
}
} catch (Error $e) {
return ['errors' => [Error::formatError($e)]];
2015-07-15 20:05:46 +03:00
}
}
}