1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is a part of GraphQL project. |
4
|
|
|
* |
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
6
|
|
|
* created: 12/6/15 11:15 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Youshido\Tests\StarWars\Schema; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Youshido\GraphQL\Type\Config\Object\InterfaceTypeConfig; |
13
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
14
|
|
|
use Youshido\GraphQL\Type\Object\AbstractInterfaceType; |
15
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
16
|
|
|
|
17
|
|
|
class CharacterInterface extends AbstractInterfaceType |
18
|
|
|
{ |
19
|
|
|
protected function build(InterfaceTypeConfig $config) |
20
|
|
|
{ |
21
|
|
|
$config |
22
|
|
|
->addField('id', TypeMap::TYPE_ID, ['required' => true]) |
23
|
|
|
->addField('name', TypeMap::TYPE_STRING, ['required' => true]) |
24
|
|
|
->addField('friends', new ListType(new CharacterInterface()), [ |
25
|
|
|
'resolve' => function($value) { |
26
|
|
|
return $value['friends']; |
27
|
|
|
} |
28
|
|
|
]) |
29
|
|
|
->addField('appearsIn', new ListType(new EpisodeEnum())); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getDescription() |
33
|
|
|
{ |
34
|
|
|
return 'A character in the Star Wars Trilogy'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getName() |
38
|
|
|
{ |
39
|
|
|
return 'Character'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function resolveType($object) |
43
|
|
|
{ |
44
|
|
|
$humans = StarWarsData::humans(); |
45
|
|
|
$droids = StarWarsData::droids(); |
46
|
|
|
|
47
|
|
|
$id = isset($object['id']) ? $object['id'] : $object; |
48
|
|
|
|
49
|
|
|
if (isset($humans[$id])) { |
50
|
|
|
return new HumanType(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (isset($droids[$id])) { |
54
|
|
|
return new DroidType(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|