wait($promise); } /** * Same as executeQuery(), but requires PromiseAdapter and always returns a Promise. * Useful for Async PHP platforms. * * @api * @param PromiseAdapter $promiseAdapter * @param \GraphQL\Type\Schema $schema * @param string|DocumentNode $source * @param mixed $rootValue * @param mixed $context * @param array|null $variableValues * @param string|null $operationName * @param callable $fieldResolver * @param array $validationRules * * @return Promise */ public static function promiseToExecute( PromiseAdapter $promiseAdapter, \GraphQL\Type\Schema $schema, $source, $rootValue = null, $context = null, $variableValues = null, $operationName = null, callable $fieldResolver = null, array $validationRules = null ) { try { if ($source instanceof DocumentNode) { $documentNode = $source; } else { $documentNode = Parser::parse(new Source($source ?: '', 'GraphQL')); } // FIXME if (!empty($validationRules)) { foreach ($validationRules as $rule) { if ($rule instanceof QueryComplexity) { $rule->setRawVariableValues($variableValues); } } } else { /** @var QueryComplexity $queryComplexity */ $queryComplexity = DocumentValidator::getRule(QueryComplexity::class); $queryComplexity->setRawVariableValues($variableValues); } $validationErrors = DocumentValidator::validate($schema, $documentNode, $validationRules); if (!empty($validationErrors)) { return $promiseAdapter->createFulfilled( new ExecutionResult(null, $validationErrors) ); } else { return Executor::promiseToExecute( $promiseAdapter, $schema, $documentNode, $rootValue, $context, $variableValues, $operationName, $fieldResolver ); } } catch (Error $e) { return $promiseAdapter->createFulfilled( new ExecutionResult(null, [$e]) ); } } /** * @deprecated Use executeQuery()->toArray() instead * * @param \GraphQL\Type\Schema $schema * @param string|DocumentNode $source * @param mixed $rootValue * @param mixed $contextValue * @param array|null $variableValues * @param string|null $operationName * @return Promise|array */ public static function execute( \GraphQL\Type\Schema $schema, $source, $rootValue = null, $contextValue = null, $variableValues = null, $operationName = null ) { trigger_error( __METHOD__ . ' is deprecated, use GraphQL::executeQuery()->toArray() as a quick replacement', E_USER_DEPRECATED ); $result = self::promiseToExecute( $promiseAdapter = Executor::getPromiseAdapter(), $schema, $source, $rootValue, $contextValue, $variableValues, $operationName ); if ($promiseAdapter instanceof SyncPromiseAdapter) { $result = $promiseAdapter->wait($result)->toArray(); } else { $result = $result->then(function(ExecutionResult $r) { return $r->toArray(); }); } return $result; } /** * @deprecated renamed to executeQuery() * * @param \GraphQL\Type\Schema $schema * @param string|DocumentNode $source * @param mixed $rootValue * @param mixed $contextValue * @param array|null $variableValues * @param string|null $operationName * * @return ExecutionResult|Promise */ public static function executeAndReturnResult( \GraphQL\Type\Schema $schema, $source, $rootValue = null, $contextValue = null, $variableValues = null, $operationName = null ) { trigger_error( __METHOD__ . ' is deprecated, use GraphQL::executeQuery() as a quick replacement', E_USER_DEPRECATED ); $result = self::promiseToExecute( $promiseAdapter = Executor::getPromiseAdapter(), $schema, $source, $rootValue, $contextValue, $variableValues, $operationName ); if ($promiseAdapter instanceof SyncPromiseAdapter) { $result = $promiseAdapter->wait($result); } return $result; } /** * Returns directives defined in GraphQL spec * * @api * @return Directive[] */ public static function getStandardDirectives() { return array_values(Directive::getInternalDirectives()); } /** * Returns types defined in GraphQL spec * * @api * @return Type[] */ public static function getStandardTypes() { return array_values(Type::getInternalTypes()); } /** * Returns standard validation rules implementing GraphQL spec * * @api * @return AbstractValidationRule[] */ public static function getStandardValidationRules() { return array_values(DocumentValidator::defaultRules()); } /** * @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); } /** * Returns directives defined in GraphQL spec * * @deprecated Renamed to getStandardDirectives * @return Directive[] */ public static function getInternalDirectives() { return self::getStandardDirectives(); } }