|
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\Type\Definition\ResolveInfo; |
|
17
|
|
|
use GraphQL\Utils; |
|
18
|
|
|
|
|
19
|
|
|
class ObjectType extends BaseObjectType |
|
20
|
|
|
{ |
|
21
|
|
|
private $isTypeOf; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var FieldDefinition[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $fields; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array $config |
|
30
|
|
|
* |
|
31
|
|
|
* @todo open PR on lib to ease inheritance |
|
32
|
|
|
*/ |
|
33
|
38 |
|
public function __construct(array $config) |
|
34
|
|
|
{ |
|
35
|
38 |
|
Utils::invariant(!empty($config['name']), 'Every type is expected to have name'); |
|
36
|
|
|
|
|
37
|
38 |
|
Config::validate($config, [ |
|
38
|
38 |
|
'name' => Config::STRING | Config::REQUIRED, |
|
39
|
38 |
|
'fields' => Config::arrayOf( |
|
40
|
38 |
|
FieldDefinition::getDefinition(), |
|
41
|
38 |
|
Config::KEY_AS_NAME | Config::MAYBE_THUNK |
|
42
|
38 |
|
), |
|
43
|
38 |
|
'description' => Config::STRING, |
|
44
|
38 |
|
'interfaces' => Config::arrayOf( |
|
45
|
38 |
|
Config::INTERFACE_TYPE, |
|
46
|
|
|
Config::MAYBE_THUNK |
|
47
|
38 |
|
), |
|
48
|
38 |
|
'isTypeOf' => Config::CALLBACK, // ($value, ResolveInfo $info) => boolean |
|
49
|
38 |
|
'resolveField' => Config::CALLBACK, |
|
50
|
38 |
|
]); |
|
51
|
|
|
|
|
52
|
38 |
|
$this->name = $config['name']; |
|
53
|
38 |
|
$this->description = isset($config['description']) ? $config['description'] : null; |
|
54
|
38 |
|
$this->resolveFieldFn = isset($config['resolveField']) ? $config['resolveField'] : null; |
|
55
|
38 |
|
$this->isTypeOf = isset($config['isTypeOf']) ? $config['isTypeOf'] : null; |
|
56
|
38 |
|
$this->config = $config; |
|
57
|
|
|
|
|
58
|
38 |
|
if (isset($config['interfaces'])) { |
|
59
|
11 |
|
InterfaceType::addImplementationToInterfaces($this); |
|
60
|
11 |
|
} |
|
61
|
38 |
|
} |
|
62
|
|
|
|
|
63
|
80 |
|
public function getFields() |
|
64
|
|
|
{ |
|
65
|
80 |
|
if (null === $this->fields) { |
|
66
|
37 |
|
$fields = isset($this->config['fields']) ? $this->config['fields'] : []; |
|
67
|
37 |
|
$fields = is_callable($fields) ? call_user_func($fields) : $fields; |
|
68
|
37 |
|
$this->fields = FieldDefinition::createMap($fields); |
|
69
|
37 |
|
} |
|
70
|
|
|
|
|
71
|
80 |
|
return $this->fields; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getField($name) |
|
75
|
|
|
{ |
|
76
|
|
|
if (null === $this->fields) { |
|
77
|
|
|
$this->getFields(); |
|
78
|
|
|
} |
|
79
|
|
|
Utils::invariant(isset($this->fields[$name]), "Field '%s' is not defined for type '%s'", $name, $this->name); |
|
80
|
|
|
|
|
81
|
|
|
return $this->fields[$name]; |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
26 |
|
public function isTypeOf($value, ResolveInfo $info) |
|
85
|
|
|
{ |
|
86
|
26 |
|
return isset($this->isTypeOf) ? call_user_func($this->isTypeOf, $value, $info) : null; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.