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