|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is a part of graphql-youshido project. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
|
6
|
|
|
* created: 11/30/15 1:30 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Type\Config\Field; |
|
10
|
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Type\Config\Config; |
|
12
|
|
|
use Youshido\GraphQL\Type\Config\Traits\ArgumentsAwareTrait; |
|
13
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
|
14
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
|
15
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
|
16
|
|
|
|
|
17
|
|
|
class FieldConfig extends Config |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
use ArgumentsAwareTrait { |
|
21
|
|
|
getArguments as traitGetArguments; |
|
22
|
|
|
getArgument as traitGetArgument; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
22 |
|
public function getRules() |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
22 |
|
'name' => ['type' => TypeMap::TYPE_STRING, 'required' => true], |
|
29
|
22 |
|
'type' => ['type' => TypeMap::TYPE_ANY, 'required' => true], |
|
30
|
22 |
|
'args' => ['type' => TypeMap::TYPE_ARRAY], |
|
31
|
22 |
|
'required' => ['type' => TypeMap::TYPE_BOOLEAN], |
|
32
|
22 |
|
'description' => ['type' => TypeMap::TYPE_STRING], |
|
33
|
22 |
|
'resolve' => ['type' => TypeMap::TYPE_FUNCTION], |
|
34
|
22 |
|
'isDeprecated' => ['type' => TypeMap::TYPE_BOOLEAN], |
|
35
|
22 |
|
'deprecationReason' => ['type' => TypeMap::TYPE_STRING], |
|
36
|
22 |
|
]; |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
22 |
|
protected function build() |
|
40
|
|
|
{ |
|
41
|
22 |
|
$this->buildArguments(); |
|
42
|
22 |
|
} |
|
43
|
|
|
|
|
44
|
17 |
|
public function resolve($value = null, $args = []) |
|
45
|
|
|
{ |
|
46
|
17 |
|
if ($this->issetResolve()) { |
|
47
|
11 |
|
$resolveFunc = $this->getResolveFunction(); |
|
48
|
|
|
|
|
49
|
11 |
|
return $resolveFunc($value, $args); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
return $this->getType()->resolve($value, $args); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return null|callable |
|
57
|
|
|
*/ |
|
58
|
17 |
|
public function getResolveFunction() |
|
59
|
|
|
{ |
|
60
|
17 |
|
return $this->get('resolve', null); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
17 |
|
public function issetResolve() |
|
64
|
|
|
{ |
|
65
|
17 |
|
$resolveFunction = $this->getResolveFunction(); |
|
66
|
|
|
|
|
67
|
17 |
|
return $resolveFunction && is_callable($resolveFunction); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return TypeInterface|AbstractObjectType |
|
72
|
|
|
*/ |
|
73
|
21 |
|
public function getType() |
|
74
|
|
|
{ |
|
75
|
21 |
|
return $this->data['type']; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
6 |
|
public function getArgument($name) |
|
79
|
|
|
{ |
|
80
|
6 |
|
return $this->traitGetArgument($name) ?: $this->getType()->getConfig()->getArgument($name); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return \Youshido\GraphQL\Type\Field\InputField[] |
|
85
|
|
|
*/ |
|
86
|
19 |
|
public function getArguments() |
|
87
|
|
|
{ |
|
88
|
19 |
|
return $this->traitGetArguments() ?: $this->getType()->getConfig()->getArguments(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: