GraphQL\Executor\Executor::execute PHP Method

execute() public static method

public static execute ( Schema $schema, DocumentNode $ast, $rootValue = null, $contextValue = null, array | ArrayAccess $variableValues = null, null $operationName = null ) : ExecutionResult | GraphQL\Executor\Promise\Promise
$schema GraphQL\Schema
$ast GraphQL\Language\AST\DocumentNode
$rootValue
$contextValue
$variableValues array | ArrayAccess
$operationName null
return ExecutionResult | GraphQL\Executor\Promise\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);
    }

Usage Example

Example #1
0
 /**
  * @it isTypeOf used to resolve runtime type for Union
  */
 public function testIsTypeOfUsedToResolveRuntimeTypeForUnion()
 {
     $dogType = new ObjectType(['name' => 'Dog', 'isTypeOf' => function ($obj) {
         return $obj instanceof Dog;
     }, 'fields' => ['name' => ['type' => Type::string()], 'woofs' => ['type' => Type::boolean()]]]);
     $catType = new ObjectType(['name' => 'Cat', 'isTypeOf' => function ($obj) {
         return $obj instanceof Cat;
     }, 'fields' => ['name' => ['type' => Type::string()], 'meows' => ['type' => Type::boolean()]]]);
     $petType = new UnionType(['name' => 'Pet', 'types' => [$dogType, $catType]]);
     $schema = new Schema(['query' => new ObjectType(['name' => 'Query', 'fields' => ['pets' => ['type' => Type::listOf($petType), 'resolve' => function () {
         return [new Dog('Odie', true), new Cat('Garfield', false)];
     }]]])]);
     $query = '{
       pets {
         name
         ... on Dog {
           woofs
         }
         ... on Cat {
           meows
         }
       }
     }';
     $expected = new ExecutionResult(['pets' => [['name' => 'Odie', 'woofs' => true], ['name' => 'Garfield', 'meows' => false]]]);
     $this->assertEquals($expected, Executor::execute($schema, Parser::parse($query)));
 }
All Usage Examples Of GraphQL\Executor\Executor::execute