1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Definition; |
13
|
|
|
|
14
|
|
|
use GraphQL\Type\Definition\Config; |
15
|
|
|
use GraphQL\Type\Definition\ObjectType as BaseObjectType; |
16
|
|
|
use GraphQL\Utils; |
17
|
|
|
|
18
|
|
|
class ObjectType extends BaseObjectType |
19
|
|
|
{ |
20
|
|
|
private $_isTypeOf; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $config |
24
|
|
|
* |
25
|
|
|
* @todo open PR on lib to ease inheritance |
26
|
|
|
*/ |
27
|
38 |
|
public function __construct(array $config) |
28
|
|
|
{ |
29
|
38 |
|
Utils::invariant(!empty($config['name']), 'Every type is expected to have name'); |
30
|
|
|
|
31
|
38 |
|
Config::validate($config, [ |
32
|
38 |
|
'name' => Config::STRING | Config::REQUIRED, |
33
|
38 |
|
'fields' => Config::arrayOf( |
34
|
38 |
|
FieldDefinition::getDefinition(), |
35
|
38 |
|
Config::KEY_AS_NAME | Config::MAYBE_THUNK |
36
|
38 |
|
), |
37
|
38 |
|
'description' => Config::STRING, |
38
|
38 |
|
'interfaces' => Config::arrayOf( |
39
|
38 |
|
Config::INTERFACE_TYPE, |
40
|
|
|
Config::MAYBE_THUNK |
41
|
38 |
|
), |
42
|
38 |
|
'isTypeOf' => Config::CALLBACK, // ($value, ResolveInfo $info) => boolean |
|
|
|
|
43
|
38 |
|
'resolveField' => Config::CALLBACK, |
44
|
38 |
|
]); |
45
|
|
|
|
46
|
38 |
|
$this->name = $config['name']; |
47
|
38 |
|
$this->description = isset($config['description']) ? $config['description'] : null; |
48
|
38 |
|
$this->resolveFieldFn = isset($config['resolveField']) ? $config['resolveField'] : null; |
49
|
38 |
|
$this->_isTypeOf = isset($config['isTypeOf']) ? $config['isTypeOf'] : null; |
50
|
38 |
|
$this->config = $config; |
51
|
|
|
|
52
|
38 |
|
if (isset($config['interfaces'])) { |
53
|
11 |
|
InterfaceType::addImplementationToInterfaces($this); |
54
|
11 |
|
} |
55
|
38 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var FieldDefinition[] |
59
|
|
|
*/ |
60
|
|
|
private $_fields; |
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return FieldDefinition[] |
64
|
|
|
*/ |
65
|
80 |
|
public function getFields() |
66
|
|
|
{ |
67
|
80 |
|
if (null === $this->_fields) { |
68
|
37 |
|
$fields = isset($this->config['fields']) ? $this->config['fields'] : []; |
69
|
37 |
|
$fields = is_callable($fields) ? call_user_func($fields) : $fields; |
70
|
37 |
|
$this->_fields = FieldDefinition::createMap($fields); |
71
|
37 |
|
} |
72
|
|
|
|
73
|
80 |
|
return $this->_fields; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $name |
78
|
|
|
* |
79
|
|
|
* @return FieldDefinition |
80
|
|
|
* |
81
|
|
|
* @throws \Exception |
82
|
|
|
*/ |
83
|
|
|
public function getField($name) |
84
|
|
|
{ |
85
|
|
|
if (null === $this->_fields) { |
86
|
|
|
$this->getFields(); |
87
|
|
|
} |
88
|
|
|
Utils::invariant(isset($this->_fields[$name]), "Field '%s' is not defined for type '%s'", $name, $this->getName()); |
|
|
|
|
89
|
|
|
|
90
|
|
|
return $this->_fields[$name]; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|