GraphQL\Type\Definition\UnionType::__construct PHP Method

__construct() public method

UnionType constructor.
public __construct ( $config )
$config
    public function __construct($config)
    {
        if (!isset($config['name'])) {
            $config['name'] = $this->tryInferName();
        }
        Config::validate($config, ['name' => Config::NAME | Config::REQUIRED, 'types' => Config::arrayOf(Config::OBJECT_TYPE, Config::MAYBE_THUNK | Config::REQUIRED), 'resolveType' => Config::CALLBACK, 'description' => Config::STRING]);
        /**
         * Optionally provide a custom type resolver function. If one is not provided,
         * the default implemenation will call `isTypeOf` on each implementing
         * Object type.
         */
        $this->name = $config['name'];
        $this->description = isset($config['description']) ? $config['description'] : null;
        $this->config = $config;
    }

Usage Example

コード例 #1
0
 public function __construct()
 {
     $config = ['name' => 'SearchResultType', 'types' => function () {
         return [Types::story(), Types::user()];
     }, 'resolveType' => function ($value) {
         if ($value instanceof Story) {
             return Types::story();
         } else {
             if ($value instanceof User) {
                 return Types::user();
             }
         }
     }];
     parent::__construct($config);
 }