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\Relay\Node; |
13
|
|
|
|
14
|
|
|
use GraphQL\Type\Definition\Config; |
15
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
16
|
|
|
use Overblog\GraphQLBundle\Definition\Builder\MappingInterface; |
17
|
|
|
use Overblog\GraphQLBundle\Definition\Type; |
18
|
|
|
use Overblog\GraphQLBundle\Resolver\Resolver; |
19
|
|
|
|
20
|
|
|
class GlobalIdField implements MappingInterface |
21
|
|
|
{ |
22
|
2 |
|
public function toMappingDefinition(array $config) |
23
|
|
|
{ |
24
|
2 |
|
Config::validate($config, [ |
25
|
2 |
|
'name' => Config::STRING | Config::REQUIRED, |
26
|
2 |
|
'typeName' => Config::STRING, |
27
|
2 |
|
'idFetcher' => Config::CALLBACK, |
28
|
2 |
|
]); |
29
|
|
|
|
30
|
2 |
|
$name = $config['name']; |
31
|
2 |
|
$typeName = isset($config['typeName']) ? $config['typeName'] : null; |
32
|
2 |
|
$idFetcher = isset($config['idFetcher']) ? $config['idFetcher'] : null; |
33
|
|
|
|
34
|
|
|
return [ |
35
|
2 |
|
'name' => $name, |
36
|
2 |
|
'description' => 'The ID of an object', |
37
|
2 |
|
'type' => Type::nonNull(Type::id()), |
38
|
2 |
|
'resolve' => function ($obj, $args, ResolveInfo $info) use ($idFetcher, $typeName) { |
39
|
|
|
return GlobalId::toGlobalId( |
40
|
|
|
!empty($typeName) ? $typeName : $info->parentType->name, |
41
|
|
|
is_callable($idFetcher) ? call_user_func_array($idFetcher, [$obj, $info]) : Resolver::valueFromObjectOrArray($obj, 'id') |
42
|
|
|
); |
43
|
2 |
|
}, |
44
|
2 |
|
]; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|