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

__construct() public method

ObjectType constructor.
public __construct ( array $config )
$config array
    public function __construct(array $config)
    {
        if (!isset($config['name'])) {
            $config['name'] = $this->tryInferName();
        }
        Utils::invariant(!empty($config['name']), 'Every type is expected to have name');
        // Note: this validation is disabled by default, because it is resource-consuming
        // TODO: add bin/validate script to check if schema is valid during development
        Config::validate($config, ['name' => Config::NAME | Config::REQUIRED, 'fields' => Config::arrayOf(FieldDefinition::getDefinition(), Config::KEY_AS_NAME | Config::MAYBE_THUNK | Config::MAYBE_TYPE), 'description' => Config::STRING, 'interfaces' => Config::arrayOf(Config::INTERFACE_TYPE, Config::MAYBE_THUNK), 'isTypeOf' => Config::CALLBACK, 'resolveField' => Config::CALLBACK]);
        $this->name = $config['name'];
        $this->description = isset($config['description']) ? $config['description'] : null;
        $this->resolveFieldFn = isset($config['resolveField']) ? $config['resolveField'] : null;
        $this->config = $config;
    }

Usage Example

Example #1
0
 public function __construct()
 {
     $config = ['name' => 'Query', 'fields' => ['user' => ['type' => Types::user(), 'description' => 'Returns user by id (in range of 1-5)', 'args' => ['id' => Types::nonNull(Types::id())]], 'viewer' => ['type' => Types::user(), 'description' => 'Represents currently logged-in user (for the sake of example - simply returns user with id == 1)'], 'stories' => ['type' => Types::listOf(Types::story()), 'description' => 'Returns subset of stories posted for this blog', 'args' => ['after' => ['type' => Types::id(), 'description' => 'Fetch stories listed after the story with this ID'], 'limit' => ['type' => Types::int(), 'description' => 'Number of stories to be returned', 'defaultValue' => 10]]], 'lastStoryPosted' => ['type' => Types::story(), 'description' => 'Returns last story posted for this blog'], 'deprecatedField' => ['type' => Types::string(), 'deprecationReason' => 'This field is deprecated!'], 'fieldWithException' => ['type' => Types::string(), 'resolve' => function () {
         throw new \Exception("Exception message thrown in field resolver");
     }], 'hello' => Type::string()], 'resolveField' => function ($val, $args, $context, ResolveInfo $info) {
         return $this->{$info->fieldName}($val, $args, $context, $info);
     }];
     parent::__construct($config);
 }
All Usage Examples Of GraphQL\Type\Definition\ObjectType::__construct