|
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\Object\AbstractObjectType; |
|
13
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
|
14
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
|
15
|
|
|
|
|
16
|
|
|
class FieldConfig extends Config |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
7 |
|
public function getRules() |
|
20
|
|
|
{ |
|
21
|
|
|
return [ |
|
22
|
7 |
|
'name' => ['type' => TypeMap::TYPE_STRING, 'required' => true], |
|
23
|
7 |
|
'type' => ['type' => TypeMap::TYPE_ANY, 'required' => true], |
|
24
|
7 |
|
'args' => ['type' => TypeMap::TYPE_ARRAY], |
|
25
|
7 |
|
'required' => ['type' => TypeMap::TYPE_BOOLEAN], |
|
26
|
7 |
|
'description' => ['type' => TypeMap::TYPE_STRING], |
|
27
|
7 |
|
'resolve' => ['type' => TypeMap::TYPE_FUNCTION], |
|
28
|
7 |
|
'isDeprecated' => ['type' => TypeMap::TYPE_BOOLEAN], |
|
29
|
7 |
|
'deprecationReason' => ['type' => TypeMap::TYPE_STRING], |
|
30
|
7 |
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
public function resolve($value = null, $args = []) |
|
34
|
|
|
{ |
|
35
|
4 |
|
$resolveFunction = $this->get('resolve', null); |
|
36
|
|
|
|
|
37
|
4 |
|
if($resolveFunction && is_callable($resolveFunction)) { |
|
38
|
1 |
|
return $resolveFunction($value, $args); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
return $this->getType()->resolve($value, $args); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return TypeInterface|AbstractObjectType |
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function getType() |
|
48
|
|
|
{ |
|
49
|
5 |
|
return $this->data['type']; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
} |
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: