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\Type; |
16
|
|
|
use Overblog\GraphQLBundle\Definition\Builder\MappingInterface; |
17
|
|
|
|
18
|
|
|
class NodeField implements MappingInterface |
19
|
|
|
{ |
20
|
11 |
|
public function toMappingDefinition(array $config) |
21
|
|
|
{ |
22
|
11 |
|
Config::validate($config, [ |
23
|
11 |
|
'name' => Config::STRING | Config::REQUIRED, |
24
|
11 |
|
'idFetcher' => Config::CALLBACK | Config::REQUIRED, |
25
|
11 |
|
'nodeInterfaceType' => Config::OBJECT_TYPE | Config::CALLBACK | Config::REQUIRED, |
26
|
11 |
|
]); |
27
|
|
|
|
28
|
11 |
|
$name = $config['name']; |
29
|
11 |
|
$idFetcher = $config['idFetcher']; |
30
|
11 |
|
$nodeInterfaceType = $config['nodeInterfaceType']; |
31
|
|
|
|
32
|
|
|
return [ |
33
|
11 |
|
'name' => $name, |
34
|
11 |
|
'description' => 'Fetches an object given its ID', |
35
|
11 |
|
'type' => $nodeInterfaceType, |
36
|
|
|
'args' => [ |
37
|
11 |
|
'id' => ['type' => Type::nonNull(Type::id()), 'description' => 'The ID of an object'], |
38
|
11 |
|
], |
39
|
11 |
|
'resolve' => function ($obj, $args, $info) use ($idFetcher) { |
40
|
8 |
|
return call_user_func_array($idFetcher, [$args['id'], $info]); |
41
|
11 |
|
}, |
42
|
11 |
|
]; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|