graphql-php/src/GraphQL.php

182 lines
6.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
{
/**
* This is the primary entry point function for fulfilling GraphQL operations
* by parsing, validating, and executing a GraphQL document along side a
* GraphQL schema.
*
* More sophisticated GraphQL servers, such as those which persist queries,
* may wish to separate the validation and execution phases to a static time
* tooling step, and a server runtime step.
*
* schema:
* The GraphQL type system to use when validating and executing a query.
* requestString:
* A GraphQL language formatted string representing the requested operation.
* rootValue:
* The value provided as the first argument to resolver functions on the top
* level type (e.g. the query object type).
* variableValues:
* A mapping of variable name to runtime value to use for all variables
* defined in the requestString.
* operationName:
* The name of the operation to use if requestString contains multiple
* possible operations. Can be omitted if requestString contains only
* one operation.
* fieldResolver:
* A resolver function to use when one is not provided by the schema.
* If not provided, the default field resolver is used (which looks for a
* value or method on the source value with the field's name).
* validationRules:
* A set of rules for query validation step. Default value is all available rules.
* Empty array would allow to skip query validation (may be convenient for persisted
* queries which are validated before persisting and assumed valid during execution)
*
2015-07-15 20:05:46 +03:00
* @param Schema $schema
2017-07-05 12:26:02 +03:00
* @param string|DocumentNode $source
* @param mixed $rootValue
* @param array $contextValue
* @param array|null $variableValues
2015-07-15 20:05:46 +03:00
* @param string|null $operationName
* @param callable $fieldResolver
* @param array $validationRules
* @param PromiseAdapter $promiseAdapter
*
* @return Promise|array
2015-07-15 20:05:46 +03:00
*/
public static function execute(
Schema $schema,
2017-07-05 12:26:02 +03:00
$source,
$rootValue = null,
$contextValue = null,
$variableValues = null,
$operationName = null,
callable $fieldResolver = null,
array $validationRules = null,
PromiseAdapter $promiseAdapter = null
)
{
$result = self::executeAndReturnResult(
$schema,
2017-07-05 12:26:02 +03:00
$source,
$rootValue,
$contextValue,
$variableValues,
$operationName,
$fieldResolver,
$validationRules,
$promiseAdapter
);
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");
}
/**
* Same as `execute`, but returns instance of ExecutionResult instead of array,
* which can be used for custom error formatting or adding extensions to response
*
* @param Schema $schema
2017-07-05 12:26:02 +03:00
* @param string|DocumentNode $source
* @param mixed $rootValue
* @param mixed $contextValue
* @param array|null $variableValues
* @param string|null $operationName
* @param callable $fieldResolver
* @param array $validationRules
* @param PromiseAdapter $promiseAdapter
*
* @return ExecutionResult|Promise
*/
public static function executeAndReturnResult(
Schema $schema,
2017-07-05 12:26:02 +03:00
$source,
$rootValue = null,
$contextValue = null,
$variableValues = null,
$operationName = null,
callable $fieldResolver = null,
array $validationRules = null,
PromiseAdapter $promiseAdapter = null
)
2015-07-15 20:05:46 +03:00
{
try {
2017-07-05 12:26:02 +03:00
if ($source instanceof DocumentNode) {
$documentNode = $source;
} else {
2017-07-05 12:26:02 +03:00
$documentNode = Parser::parse(new Source($source ?: '', 'GraphQL'));
}
/** @var QueryComplexity $queryComplexity */
$queryComplexity = DocumentValidator::getRule('QueryComplexity');
$queryComplexity->setRawVariableValues($variableValues);
$validationErrors = DocumentValidator::validate($schema, $documentNode, $validationRules);
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,
$fieldResolver,
$promiseAdapter
);
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 callable $fn
*/
public static function setDefaultFieldResolver(callable $fn)
{
Executor::setDefaultFieldResolver($fn);
}
/**
* @param PromiseAdapter|null $promiseAdapter
*/
public static function setPromiseAdapter(PromiseAdapter $promiseAdapter = null)
{
Executor::setPromiseAdapter($promiseAdapter);
}
2015-07-15 20:05:46 +03:00
}