GraphQL\Executor\Promise\PromiseAdapter::createPromise PHP Method

createPromise() public method

Creates a Promise
public createPromise ( callable $resolver ) : GraphQL\Executor\Promise\Promise
$resolver callable
return GraphQL\Executor\Promise\Promise
    public function createPromise(callable $resolver);

Usage Example

Example #1
0
 /**
  * @param Schema $schema
  * @param DocumentNode $ast
  * @param $rootValue
  * @param $contextValue
  * @param array|\ArrayAccess $variableValues
  * @param null $operationName
  * @return ExecutionResult|Promise
  */
 public static function execute(Schema $schema, DocumentNode $ast, $rootValue = null, $contextValue = null, $variableValues = null, $operationName = null)
 {
     if (!self::$UNDEFINED) {
         self::$UNDEFINED = new \stdClass();
     }
     if (null !== $variableValues) {
         Utils::invariant(is_array($variableValues) || $variableValues instanceof \ArrayAccess, "Variable values are expected to be array or instance of ArrayAccess, got " . Utils::getVariableType($variableValues));
     }
     if (null !== $operationName) {
         Utils::invariant(is_string($operationName), "Operation name is supposed to be string, got " . Utils::getVariableType($operationName));
     }
     $exeContext = self::buildExecutionContext($schema, $ast, $rootValue, $contextValue, $variableValues, $operationName);
     if (null === self::$promiseAdapter) {
         static::setPromiseAdapter(new GenericPromiseAdapter());
     }
     try {
         $data = self::$promiseAdapter->createPromise(function (callable $resolve) use($exeContext, $rootValue) {
             return $resolve(self::executeOperation($exeContext, $exeContext->operation, $rootValue));
         });
         if (self::$promiseAdapter->isPromise($data)) {
             // Return a Promise that will eventually resolve to the data described by
             // The "Response" section of the GraphQL specification.
             //
             // If errors are encountered while executing a GraphQL field, only that
             // field and its descendants will be omitted, and sibling fields will still
             // be executed. An execution which encounters errors will still result in a
             // resolved Promise.
             return $data->then(null, function ($error) use($exeContext) {
                 // Errors from sub-fields of a NonNull type may propagate to the top level,
                 // at which point we still log the error and null the parent field, which
                 // in this case is the entire response.
                 $exeContext->addError($error);
                 return null;
             })->then(function ($data) use($exeContext) {
                 return new ExecutionResult((array) $data, $exeContext->errors);
             });
         }
     } catch (Error $e) {
         $exeContext->addError($e);
         $data = null;
     }
     return new ExecutionResult((array) $data, $exeContext->errors);
 }