-
Notifications
You must be signed in to change notification settings - Fork 5
/
MyResolverMap.php
83 lines (75 loc) · 3 KB
/
MyResolverMap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
// src/Resolver/ResolverMap.php
namespace App\Resolver;
use GraphQL\Error\Error;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Utils;
use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Resolver\ResolverMap;
class MyResolverMap extends ResolverMap
{
public function map()
{
return [
'Query' => [
self::RESOLVE_FIELD => function ($value, Argument $args, \ArrayObject $context, ResolveInfo $info) {
if ('character' === $info->fieldName) {
$characters = Characters::getCharacters();
$id = (int) $args['id'];
if (isset($characters[$id])) {
return $characters[$id];
}
}
return null;
},
'findHumansByDateOfBirth' => function ($value, Argument $args) {
$years = $args['years'];
return array_filter(Characters::getHumans(), function ($human) use ($years) {
return in_array($human['dateOfBirth'], $years);
});
},
'humans' => [Characters::class, 'getHumans'],
'direwolves' => [Characters::class, 'getDirewolves'],
],
'Character' => [
self::RESOLVE_TYPE => function ($value) {
return Characters::TYPE_HUMAN === $value['type'] ? 'Human' : 'Direwolf';
},
],
'Human' => [
'direwolf' => function ($value) {
$direwolves = Characters::getDirewolves();
if (isset($direwolves[$value['direwolf']])) {
return $direwolves[$value['direwolf']];
} else {
return null;
}
},
],
// enum internal values
'Status' => [
'ALIVE' => 1,
'DECEASED' => 0,
],
// custom scalar
'Year' => [
self::SERIALIZE => function ($value) {
return sprintf('%s AC', $value);
},
self::PARSE_VALUE => function ($value) {
if (!is_string($value)) {
throw new Error(sprintf('Cannot represent following value as a valid year: %s.', Utils::printSafeJson($value)));
}
return (int) str_replace(' AC', '', $value);
},
self::PARSE_LITERAL => function ($valueNode) {
if (!$valueNode instanceof StringValueNode) {
throw new Error('Query error: Can only parse strings got: '.$valueNode->kind, [$valueNode]);
}
return (int) str_replace(' AC', '', $valueNode->value);
},
],
];
}
}