Example #1
0
 public static function execute(Schema $schema, $root, Document $ast, $operationName = null, array $args = null)
 {
     if (!self::$UNDEFINED) {
         self::$UNDEFINED = new \stdClass();
     }
     try {
         $errors = new \ArrayObject();
         $exeContext = self::buildExecutionContext($schema, $root, $ast, $operationName, $args, $errors);
         $data = self::executeOperation($exeContext, $root, $exeContext->operation);
     } catch (\Exception $e) {
         $errors[] = $e;
     }
     $result = ['data' => isset($data) ? $data : null];
     if (count($errors) > 0) {
         $result['errors'] = array_map(['GraphQL\\Error', 'formatError'], $errors->getArrayCopy());
     }
     return $result;
 }
Example #2
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);
 }
Example #3
0
 /**
  * @param Schema $schema
  * @param Document $ast
  * @param $rootValue
  * @param $contextValue
  * @param array|\ArrayAccess $variableValues
  * @param null $operationName
  * @return ExecutionResult
  */
 public static function execute(Schema $schema, Document $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);
     try {
         $data = self::executeOperation($exeContext, $exeContext->operation, $rootValue);
     } catch (Error $e) {
         $exeContext->addError($e);
         $data = null;
     }
     return new ExecutionResult((array) $data, $exeContext->errors);
 }